description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
char = set()
n = len(s)
i = 0
j = n - 1
fin = ""
lis = []
while j >= i + 3:
if s[i] == s[j]:
lis.append(i)
i += 1
j -= 1
elif s[i] == s[j - 1]:
lis.append(i)
i += 1
j -= 2
elif s[i + 1] == s[j]:
lis.append(i + 1)
i += 2
j -= 1
else:
lis.append(i + 1)
i += 2
j -= 2
fin = "".join([s[k] for k in lis])
fin_rev = fin[::-1]
if j >= i:
fin += s[i]
print(fin + fin_rev) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = list(input().rstrip())
n = len(s)
i = 0
j = n - 1
d = []
while j - i > 1:
if s[i] == s[j]:
d.append(s[i])
i += 1
j -= 1
elif s[i] == s[j - 1]:
d.append(s[i])
j -= 2
i += 1
else:
i += 1
if i <= j:
d.append(s[i])
d += d[: len(d) - 1][::-1]
else:
d += d[::-1]
print("".join(d)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | import sys
def rl(proc=None):
if proc is not None:
return proc(sys.stdin.readline())
else:
return sys.stdin.readline().rstrip()
def srl(proc=None):
if proc is not None:
return list(map(proc, rl().split()))
else:
return rl().split()
def main():
s = rl()
i = 0
j = len(s) - 2
r = ""
while i + 2 <= j:
if s[i] in s[j : j + 2]:
r += s[i]
else:
r += s[i + 1]
i += 2
j -= 2
if len(s) & 3:
m = s[i]
else:
m = ""
print(r + m + "".join(reversed(r)))
main() | IMPORT FUNC_DEF NONE IF VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE IF VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING WHILE BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | l1 = []
l2 = []
s = input()
n = len(s)
if n == 2 or n == 3:
print(s[0])
exit(0)
p = 0
q = n - 1
i = 0
while p + 1 < q - 1:
a = s[p]
b = s[p + 1]
c = s[q]
d = s[q - 1]
if a == c:
l1.append(a)
l2.append(c)
p += 1
q -= 1
elif a == d:
l1.append(a)
l2.append(d)
p += 1
q -= 2
elif b == c:
l1.append(b)
l2.append(c)
p += 2
q -= 1
elif b == d:
l1.append(b)
l2.append(d)
p += 2
q -= 2
l2.reverse()
if p <= q:
print("".join(l1), end="")
print(s[p], end="")
print("".join(l2))
else:
print("".join(l1), end="")
print("".join(l2)) | ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
remainingLength = len(s)
i = 0
j = remainingLength - 1
palindrome = ""
middleChar = ""
while remainingLength > 0:
if remainingLength >= 4:
firstPart = {s[i], s[i + 1]}
lastPart = {s[j], s[j - 1]}
palindromeCharacter = firstPart.intersection(lastPart).pop()
palindrome = palindrome + palindromeCharacter
else:
middleChar = s[i]
remainingLength -= 4
i += 2
j -= 2
totalPalindrome = palindrome + middleChar + palindrome[::-1]
print(totalPalindrome) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
l = 0
n = len(s) - 1
r = n
ans = ""
cent = ""
while r > l:
if s[l] == s[r]:
ans += s[l]
l += 1
r -= 1
elif s[l + 1] == s[r - 1] and r - l > 2:
ans += s[l + 1]
l += 2
r -= 2
elif r - l > 1:
if l < n - r:
if s[l + 1] == s[r]:
ans += s[l + 1]
l += 2
r -= 1
elif s[l] == s[r - 1]:
ans += s[l]
l += 1
r -= 2
elif l < n - r:
l += 1
else:
r -= 1
elif s[l] == s[r - 1]:
ans += s[l]
l += 1
r -= 2
elif s[l + 1] == s[r]:
ans += s[l + 1]
l += 2
r -= 1
elif l < n - r:
l += 1
else:
r -= 1
elif l < n - r:
l += 1
else:
r -= 1
if r == l:
cent = s[l]
if len(ans) * 2 + len(cent) < len(s) // 2:
print("IMPOSSIBLE")
else:
print(ans + cent + ans[::-1]) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
n = len(s)
def sol(s, n):
center = ""
if n % 2 == 0:
if n == 2:
return s[0]
elif n % 4 == 0:
second, third = n // 2 - 1, n // 2
else:
center = s[n // 2]
second, third = n // 2 - 2, n // 2 + 1
elif n == 1 or n == 3:
return s[0]
else:
center = s[n // 2]
second, third = n // 2 - 1, n // 2 + 1
first, fourth = second - 1, third + 1
ans_half = [(0) for i in range(n)]
half_ans_len = 0
i_curr = 0
while first >= 0 and fourth < n:
if s[first] == s[third] or s[first] == s[fourth]:
ans_half[i_curr] = s[first]
i_curr += 1
half_ans_len += 1
else:
ans_half[i_curr] = s[second]
i_curr += 1
half_ans_len += 1
first -= 2
second -= 2
third += 2
fourth += 2
return (
"".join(ans_half[:half_ans_len][::-1])
+ center
+ "".join(ans_half[:half_ans_len])
)
print(sol(s, n)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL STRING VAR VAR NUMBER VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
t = ""
n = len(s)
for i in range(0, n, 2):
if n - i - 2 <= i + 1:
break
s1 = s[i : i + 2]
s2 = s[n - i - 1] + s[n - i - 2]
if "a" in s1 and "a" in s2:
t += "a"
elif "b" in s1 and "b" in s2:
t += "b"
else:
t += "c"
if n % 4:
fin = t + s[n // 2] + t[::-1]
else:
fin = t + t[::-1]
print(fin) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF STRING VAR STRING VAR VAR STRING IF STRING VAR STRING VAR VAR STRING VAR STRING IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
i, j = 0, len(s) - 1
half = []
while j - i + 1 > 3:
if s[i] == s[j]:
half.append(s[i])
i += 1
j -= 1
elif s[i + 1] == s[j]:
half.append(s[i + 1])
i += 2
j -= 1
elif s[i] == s[j - 1]:
half.append(s[i])
i += 1
j -= 2
else:
half.append(s[i + 1])
i += 2
j -= 2
res = "".join(half)
if j > i:
res += s[i]
res += "".join(half[::-1])
print(res) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR VAR VAR VAR VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
n = len(s)
ans = ""
for i in range(0, n // 2, 2):
if n - i - 2 <= i + 1:
break
else:
l = set(s[i : i + 2])
r = set(s[n - i - 2 : n - i])
temp = l.intersection(r)
ans += [i for i in temp][0]
print(ans + s[n // 2] + ans[::-1] if n % 4 != 0 else ans + ans[::-1]) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | import sys
input = sys.stdin.readline
s = list(input())
if s[-1] != "a" and s[-1] != "b" and s[-1] != "c":
s = s[:-1]
ans = []
i = 0
while i + 1 < len(s) - i - 2:
st = s[i] + s[i + 1] + s[len(s) - i - 1] + s[len(s) - i - 2]
if st.count("a") >= 2:
ans.append("a")
elif st.count("b") >= 2:
ans.append("b")
else:
ans.append("c")
i += 2
if len(s) % 4 != 0:
ans.append(s[i])
if len(s) % 4 == 0:
sys.stdout.write("".join(ans))
ans.reverse()
sys.stdout.write("".join(ans) + "\n")
else:
sys.stdout.write("".join(ans))
ans.reverse()
sys.stdout.write("".join(ans[1:]) + "\n") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR NUMBER STRING |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | def Input():
tem = input().split()
ans = []
for it in tem:
ans.append(int(it))
return ans
s = input()
n = len(s)
if n < 4:
print(s[0])
else:
m = n // 2
l, r = 0, n - 1
ans = [[] for i in range(2)]
while l + 1 < r - 1:
if s[l + 1] == s[r]:
ans[0].append(s[l + 1])
ans[1].append(s[r])
elif s[l + 1] == s[r - 1]:
ans[0].append(s[l + 1])
ans[1].append(s[r - 1])
elif s[l] == s[r]:
ans[0].append(s[l])
ans[1].append(s[r])
else:
ans[0].append(s[l])
ans[1].append(s[r - 1])
l += 2
r -= 2
for i in range(len(ans[0])):
print(ans[0][i], end="")
if l < r:
print(s[l], end="")
for i in range(len(ans[1]) - 1, -1, -1):
print(ans[1][i], end="")
print() | FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | word = input()
n = len(word)
ln = n // 2
letters = [[], [], []]
dic = {"a": 0, "b": 1, "c": 2}
for i in range(n):
letters[dic[word[i]]].append(i)
ln2 = ln // 2
ans = ""
lb = -1
rb = n
for i in range(ln2):
for a, b in [
(lb + 1, rb - 1),
(lb + 2, rb - 1),
(lb + 1, rb - 2),
(lb + 2, rb - 2),
]:
if word[a] == word[b]:
ans += word[a]
lb = a
rb = b
break
if len(ans) != ln2:
[1, 2][3]
print(ans, end=word[lb + 1])
ans2 = list(ans)
ans2.reverse()
print("".join(ans2)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST LIST LIST ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR EXPR LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = str(input())
lst = []
l = 0
h = len(s) - 1
while h >= l:
if s[l] == s[h]:
lst.append(s[l])
l = l + 1
h = h - 1
elif s[l] == s[h - 1]:
h = h - 1
elif s[l + 1] == s[h]:
l = l + 1
else:
l = l + 1
h = h - 1
lst1 = lst[::-1]
for i in range(len(lst)):
print(lst[i], end="")
for i in range(1, len(lst)):
print(lst1[i], end="")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | import sys
input = lambda: sys.stdin.readline().strip()
s = list(input())
i = 0
j = len(s) - 1
res = [""]
while i < j - 2:
if s[i] == s[j] or s[i] == s[j - 1]:
res.append(s[i])
else:
res.append(s[i + 1])
i += 2
j -= 2
print("".join(res + ([s[i]] if len(s) % 4 else [""]) + list(reversed(res)))) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST STRING WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER LIST VAR VAR LIST STRING FUNC_CALL VAR FUNC_CALL VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
n = len(s)
i, j = 0, n - 1
ans = []
t = n // 4
for i in range(t):
x = i * 2
y = n - 2 - i * 2
if s[x] == s[y] or s[x] == s[y + 1]:
ans.append(s[x])
else:
ans.append(s[x + 1])
if n % 4 >= 2:
ans = ans + [s[n // 2]] + ans[::-1]
else:
ans = ans + ans[::-1]
print("".join(ans)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR LIST VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
p = ""
for i in range(len(s) // 4):
if s[i * 2] == s[-i * 2 - 1]:
p += s[i * 2]
elif s[i * 2 + 1] == s[-i * 2 - 1]:
p += s[i * 2 + 1]
elif s[i * 2] == s[-i * 2 - 2]:
p += s[i * 2]
else:
p += s[i * 2 + 1]
l = len(p)
if len(s) % 4 != 0:
p += s[len(s) // 2]
for i in range(l):
p += p[l - i - 1]
print(p) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
left = 0
right = len(s) - 1
out = ""
needed = len(s) // 2
if len(s) <= 3:
print(s[0])
else:
while right - left >= 3 and len(out) < needed:
if s[left] == s[right]:
out += s[left]
left += 1
right -= 1
elif s[left] == s[right - 1]:
out += s[left]
left += 1
right -= 2
else:
out += s[left + 1]
if s[left + 1] == s[right]:
right -= 1
else:
right -= 2
left += 2
if left <= right:
print(out + s[left] + out[::-1])
else:
print(out + out[::-1]) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | import sys
for line in sys.stdin:
line = line[:-1]
ans = ["a"] * (len(line) // 4)
for i in range(len(line) // 4):
if line[2 * i] in {line[-2 * i - 1], line[-2 * i - 2]}:
ans[i] = line[2 * i]
else:
ans[i] = line[2 * i + 1]
if len(line) % 2 == 1 or len(line) % 4 == 2:
print("".join(ans) + line[len(line) // 2] + "".join(ans[::-1]))
else:
print("".join(ans) + "".join(ans[::-1]))
break | IMPORT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL STRING VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING VAR NUMBER |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | import sys
input = sys.stdin.readline
s = input().rstrip()
n = len(s)
ans = []
l = 0
r = n - 1
while l <= r:
if s[l] == s[r]:
ans.append(s[l])
l += 1
r -= 1
elif s[l + 1] == s[r]:
l += 1
elif s[l] == s[r - 1]:
r -= 1
else:
l += 1
r -= 1
res = ans + ans[: len(ans) - 1][::-1]
print("".join(res)) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | from sys import stdin, stdout
s = input()
N = len(s)
required = N // 2
front = []
tail = []
left = 0
right = N - 1
while left <= right and required > 0:
A = left
B = left + 1
C = right - 1
D = right
if required == 1:
front.append(s[A])
required -= 1
break
if s[A] == s[D]:
front.append(s[A])
tail.append(s[A])
left = A + 1
right = D - 1
elif s[A] == s[C]:
front.append(s[A])
tail.append(s[A])
left = A + 1
right = C - 1
elif s[B] == s[D]:
front.append(s[B])
tail.append(s[B])
left = B + 1
right = D - 1
elif s[B] == s[C]:
front.append(s[B])
tail.append(s[B])
left = B + 1
right = C - 1
required -= 2
if required == 0:
tail = tail[::-1]
print(*front, *tail, sep="")
else:
print("IMPOSSIBLE") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
i = 0
j = len(s) - 1
t = []
middle = None
while i <= j:
if j - i + 1 <= 3:
middle = s[i]
elif s[i] == s[j] or s[i] == s[j - 1]:
t.append(s[i])
else:
t.append(s[i + 1])
i += 2
j -= 2
a = "".join(t)
b = "".join(t[::-1])
if middle:
print(f"{a}{middle}{b}")
else:
print(f"{a}{b}") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NONE WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
if len(s) == 3:
print(s[0])
else:
ans = ["a"] * (len(s) // 4)
for i in range(len(s) // 4):
if s[2 * i + 1] == s[-2 * i - 1]:
ans[i] = s[2 * i + 1]
elif s[2 * i + 1] == s[-2 * i - 2]:
ans[i] = s[2 * i + 1]
elif s[2 * i] == s[-2 * i - 1]:
ans[i] = s[2 * i]
elif s[2 * i] == s[-2 * i - 2]:
ans[i] = s[2 * i]
else:
print("What???????")
if len(s) % 4 != 0:
ans = "".join(ans) + s[len(s) // 2] + "".join(ans[::-1])
else:
ans = "".join(ans) + "".join(ans[::-1])
print(ans) | ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR IF VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL STRING VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL STRING VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
n = len(s)
f, b = [], []
i, j = 0, n - 1
while i < j:
if s[i] != s[j]:
if s[i] == s[j - 1]:
j -= 1
elif s[i + 1] == s[j]:
i += 1
else:
i += 1
j -= 1
if s[i] == s[j]:
f.append(s[i])
if i < j:
b.append(s[j])
i += 1
j -= 1
print("".join(f + list(reversed(b)))) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | S = input()
N = len(S)
l, r = 0, N - 1
res = ""
mid = ""
while l <= r:
if l == r:
mid = S[l]
break
elif S[l] == S[r]:
res += S[l]
l += 1
r -= 1
elif S[l + 1] == S[r]:
l += 1
elif S[r - 1] == S[l]:
r -= 1
elif l > N - r:
r -= 1
else:
l += 1
ans = res + mid + res[::-1]
if len(ans) >= N // 2:
print(ans)
else:
print("IMPOSSIBLE") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | import sys
input = lambda: sys.stdin.readline().strip()
s = list(input())
i = 0
j = len(s) - 1
res = [""]
while i < j - 2:
if s[i] == s[j] or s[i] == s[j - 1]:
res += s[i]
else:
res.append(s[i + 1])
i += 2
j -= 2
n = len(res)
if len(s) % 4:
res.append(s[i])
for i in range(n - 1, -1, -1):
res.append(res[i])
print("".join(res)) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST STRING WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
n = len(s)
i = 0
j = n - 1
ans = [-1] * n
while i < j:
if s[i] != s[j]:
if s[i + 1] == s[j - 1]:
ans[i + 1] = ans[j - 1] = s[i + 1]
i += 2
j -= 2
elif s[i] == s[j - 1]:
ans[i] = ans[j - 1] = s[i]
i += 1
j -= 2
elif s[i + 1] == s[j]:
ans[i + 1] = ans[j] = s[j]
i += 2
j -= 1
else:
ans[i] = ans[j] = s[i]
i += 1
j -= 1
a = ""
for i in ans:
if i != -1:
a += i
if len(ans) < n // 2:
print("IMPOSSIBLE")
exit()
print(a) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR IF VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | line = input()
n = len(line)
def f(left, right):
d = [(0) for _ in range(n + 1)]
cnt = 0
if left + 2 == right:
d[left + 1] = 1
cnt += 1
while left >= 0 and right < n:
if line[left] == line[right]:
d[left] = 1
d[right] = 1
left -= 1
right += 1
cnt += 2
elif left - 1 >= 0 and right + 1 < n and line[left - 1] == line[right + 1]:
d[left - 1] = 1
d[right + 1] = 1
left -= 2
right += 2
cnt += 2
elif right + 1 < n and line[left] == line[right + 1]:
d[left] = 1
d[right + 1] = 1
left -= 1
right += 2
cnt += 2
elif left - 1 >= 0 and line[left - 1] == line[right]:
d[left - 1] = 1
d[right] = 1
left -= 2
right += 1
cnt += 2
else:
left -= 1
right += 1
if cnt >= n // 2:
ans = []
for idx, c in enumerate(line):
if d[idx]:
ans.append(c)
return True, "".join(ans)
return False, ""
if n <= 3:
print(line[0])
if n > 3:
if n % 2:
left = n // 2 - 1
right = n // 2 + 1
else:
left = n // 2 - 1
right = n // 2
flag, ans = f(left, right)
while not flag:
for i in range(n):
flag, ans = f(i, i + 2)
if flag:
break
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_CALL STRING VAR RETURN NUMBER STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
i = 0
j = len(s) - 1
m = len(s) // 2
if s.count("a") >= m:
print("a" * m)
elif s.count("b") >= m:
print("b" * m)
elif s.count("c") >= m:
print("c" * m)
else:
i = 0
j = len(s) - 1
r = []
while j - i > 2:
if s[i] == s[j]:
r.append(s[i])
i += 1
j -= 1
elif s[i] == s[j - 1]:
r.append(s[i])
i += 1
j -= 2
elif s[i + 1] == s[j]:
r.append(s[j])
i += 2
j -= 1
else:
r.append(s[i + 1])
i += 2
j -= 2
if j >= i:
x = r + [s[i]] + r[::-1]
else:
x = r + r[::-1]
if len(x) < m:
print("IMPOSSIBLE")
else:
print("".join(x)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR LIST VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | def find(s):
if len(s) == 1:
return s
if len(s) == 2:
return s[0]
if len(s) == 3:
return s[0]
left = 0
right = len(s) - 1
construct = []
left_get = left
right_get = right
while left < right:
if s[left] == s[right]:
construct += [s[left]]
left_get = left
right_get = right
left += 1
right -= 1
elif s[left] == s[right - 1]:
if left < right - 1:
construct += [s[left]]
left_get = left
right_get = right - 1
left += 1
right = right - 2
else:
break
elif s[left] == s[right - 2]:
if left < right - 2:
construct += [s[left]]
left_get = left
right_get = right - 2
left += 1
right = right - 3
else:
break
elif s[left + 1] == s[right]:
if left + 1 < right:
construct += [s[left + 1]]
left_get = left + 1
right_get = right
left = left + 2
right -= 1
else:
break
elif s[left + 2] == s[right]:
if left + 2 < right:
construct += [s[left + 2]]
left_get = left + 2
right_get = right
left = left + 3
right -= 1
else:
break
elif s[left + 1] == s[right - 1]:
if left + 1 < right - 1:
construct += [s[left + 1]]
left_get = left + 1
right_get = right - 1
left = left + 2
right = right - 2
else:
break
else:
break
leftside = construct
rightside = construct[::-1]
if left_get + 1 <= right_get - 1:
leftside += [s[left_get + 1]]
ans = leftside + rightside
if len(ans) >= len(s) // 2:
return "".join(leftside + rightside)
else:
return "IMPOSSIBLE"
print(find(input())) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL STRING BIN_OP VAR VAR RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
n = len(s)
l = 0
h = n - 1
ans = []
while h >= l:
if s[l] == s[h]:
ans.append(s[l])
l += 1
h -= 1
elif s[l + 1] == s[h]:
l += 1
elif s[l] == s[h - 1]:
h -= 1
else:
h -= 1
l += 1
n = len(ans)
for i in range(n):
print(ans[i], end="")
for i in range(n - 2, -1, -1):
print(ans[i], end="")
print() | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | s = input()
n = len(s)
b = []
for j in s:
b.append(j)
x = []
y = []
j = 0
i = n - 1
while j < i:
if b[j] == b[i]:
if i != j:
x.append(b[j])
y.append(b[i])
else:
x.append(b[j])
i += -1
j += 1
elif b[j] == b[i - 1]:
if i - 1 != j:
x.append(b[j])
y.append(b[i - 1])
else:
x.append(b[j])
j += 1
i += -2
elif b[j + 1] == b[i]:
if i != j + 1:
x.append(b[j + 1])
y.append(b[i])
elif i == j + 1:
x.append(b[j + 1])
i += -1
j += 2
else:
if i - 1 != j + 1:
x.append(b[j + 1])
y.append(b[i - 1])
elif i - 1 == j + 1:
x.append(b[j + 1])
i += -2
j += 2
res = x + y[::-1]
print("".join(res)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string s (2 β€ |s| β€ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome t that is a subsequence of s and |t| β₯ β (|s|)/(2) β.
If there are multiple solutions, you may print any of them. You don't have to maximise the length of t.
If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include "cacac", "caac", "aca" and "ccc". | t = 1
for _ in range(t):
s = input()
n = len(s)
if n <= 3:
print(s[0])
exit(0)
finl_Str = ""
for i in range(0, n, 2):
if n - i - 2 <= i + 1:
break
if "a" in s[i : i + 2] and "a" in s[n - i - 2 : n - i]:
finl_Str += "a"
elif "b" in s[i : i + 2] and "b" in s[n - i - 2 : n - i]:
finl_Str += "b"
elif "c" in s[i : i + 2] and "c" in s[n - i - 2 : n - i]:
finl_Str += "c"
ans = ""
if n % 4:
ans = finl_Str + s[n // 2] + finl_Str[::-1]
else:
ans = finl_Str + finl_Str[::-1]
print(ans) | ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF STRING VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR STRING IF STRING VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR STRING IF STRING VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR STRING ASSIGN VAR STRING IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
x = r - l + 1
t = [i for i in range(1, x + 1)]
b = True
if sum(t) > s:
b = False
print(-1)
else:
s -= sum(t)
to_add = s // x
for i in range(x):
t[i] += to_add
to_add = s % x
i = x - 1
while to_add:
t[i] += 1
to_add -= 1
i -= 1
for i in t:
if i > n:
b = False
break
if len(set(t)) < len(t):
b = False
if not b:
print(-1)
else:
ans = [0] * n
c = 0
v = set([i for i in range(1, n + 1)])
for i in range(l - 1, r):
ans[i] = t[c]
c += 1
v.discard(ans[i])
v = list(v)
for i in range(n):
if ans[i] == 0:
ans[i] = v.pop()
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
while t > 0:
t -= 1
n, l, r, s = map(int, input().split())
k = r - l + 1
smin = k * (k + 1) // 2
smax = smin + k * (n - k)
if smin <= s <= smax:
dif = s - smin
q1 = dif // k
q2 = dif % k
a = [(i + 1) for i in range(k)]
for i in range(k - 1, -1, -1):
if q2 > 0:
a[i] += q1 + 1
q2 -= 1
else:
a[i] += q1
b = set()
for i in range(k):
b.add(a[i])
c = []
for i in range(1, n + 1):
if i not in b:
c.append(i)
ans = []
y1 = 0
y2 = 0
for i in range(1, n + 1):
if i < l or i > r:
ans.append(c[y1])
y1 += 1
else:
ans.append(a[y2])
y2 += 1
for i in range(n):
print(ans[i], end=" ")
print()
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def get(sum, dist, n):
if not (dist + 1) * dist / 2 <= sum <= (2 * n - dist + 1) * dist / 2:
return None
ans = []
for cur in range(n, 0, -1):
if dist * (dist - 1) / 2 + cur <= sum:
ans.append(cur)
sum -= cur
dist -= 1
return ans
t = int(input())
for _ in range(t):
n, l, r, s = [int(x) for x in input().split()]
dist = r - l + 1
ans = get(s, dist, n)
if ans is None:
print(-1)
else:
rest = list(set(range(1, n + 1)) - set(ans))
total = [str(x) for x in rest[: l - 1] + ans + rest[l - 1 :]]
print(" ".join(total)) | FUNC_DEF IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN NONE ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def fun(length, s, n):
sum1 = 0
lis = []
for i in range(1, length + 1):
lis.append(i)
sum1 += i
limit = n
j = len(lis) - 1
left = s - sum1
while j >= 0:
if left == 0:
se.remove(lis[j])
j -= 1
continue
if limit - lis[j] >= left:
lis[j] += left
se.remove(lis[j])
j -= 1
left = 0
else:
x = limit - lis[j]
lis[j] += x
left -= x
se.remove(lis[j])
j -= 1
limit -= 1
return lis
for _ in range(int(input())):
n, l, r, s = map(int, input().split())
se = set()
for i in range(1, n + 1):
se.add(i)
length = r - l + 1
min1 = 0
max1 = 0
n2 = n
j = 1
for i in range(1, length + 1):
min1 += j
j += 1
for j in range(length, 0, -1):
max1 += n2
n2 -= 1
if s >= min1 and s <= max1:
temp = fun(length, s, n)
else:
print(-1)
continue
l1 = []
l2 = []
for i in range(1, l):
l1.append(se.pop())
for i in range(r + 1, n + 1):
l2.append(se.pop())
f = l1 + temp + l2
for i in f:
print(i, end=" ")
print() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | tc = int(input())
for _ in range(tc):
n, l, r, s = list(map(int, input().split()))
needed = r - l + 1
minsum = 0
sum_left = s
max_left = n
lis = []
taken = [(False) for _ in range(n + 3)]
for i in range(needed):
minsum += i + 1
works = True
while needed > 0:
if minsum > sum_left:
works = False
break
if minsum == sum_left:
minsum -= needed
sum_left -= needed
lis.append(needed)
taken[needed] = True
needed -= 1
continue
minsum -= needed
if sum_left >= max_left + minsum:
lis.append(max_left)
else:
num = sum_left - minsum
if 0 < num <= n:
lis.append(num)
max_left = num
else:
break
sum_left -= lis[-1]
needed -= 1
taken[lis[-1]] = True
max_left = max_left - 1
if works and sum(lis) == s and needed == 0:
i = 1
j = 1
while j <= n:
if l <= j <= r:
print(lis[j - l], end=" ")
else:
while taken[i]:
i += 1
print(i, end=" ")
i += 1
j += 1
print()
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | import sys
input = sys.stdin.readline
def solve():
n, l, r, s = map(int, input().split())
k = r - l + 1
z = list(range(1, k + 1))
t = sum(z)
j = 0
for i in range(k - 1, -1, -1):
w = min(n - j - z[i], s - t)
if w > 0:
t += w
z[i] += w
j += 1
if t != s:
print(-1)
else:
w = [False] * (n + 1)
for i in z:
w[i] = True
a = [None] * n
for i in range(k):
a[l - 1 + i] = z[i]
j = 1
for i in range(n):
if a[i] is None:
while w[j]:
j += 1
w[j] = True
a[i] = j
print(" ".join(map(str, a)))
for i in range(int(input())):
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | (T,) = map(int, input().split())
for _ in range(T):
n, l, r, s = map(int, input().split())
x = list(range(1, r - l + 2))
mx = sum(range(n - (r - l), n + 1))
mn = sum(x)
if s > mx or s < mn:
print(-1)
continue
c = -1
for _ in range(s - mn):
if c == -1:
if x[c] != n:
x[c] += 1
continue
else:
c -= 1
x[c] += 1
continue
if x[c] != x[c + 1] - 1:
x[c] += 1
else:
c -= 1
x[c] += 1
tmp = [0] * (n + 1)
for c in x:
tmp[c] += 1
R = [0] * (n + 1)
c = 1
for i in range(1, n + 1):
while c < n + 1 and tmp[c] == 1:
c += 1
if i < l or r < i:
R[i] = c
tmp[c] = 1
else:
R[i] = x[i - l]
print(*R[1:]) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
c = r - l + 1
sp = []
f = range(n - c + 1, n + 1)
h = range(1, c + 1)
max_s = sum(f)
min_s = sum(h)
if s > max_s or s < min_s:
print(-1)
elif s == min_s:
c = list(range(1, n + 1))
for el in h:
c.remove(el)
j = 0
k = 0
for i in range(n):
if i < l - 1:
sp.append(c[j])
j += 1
elif i > r - 1:
sp.append(c[j])
j += 1
else:
sp.append(h[k])
k += 1
print(*sp)
elif s == max_s:
c = list(range(1, n + 1))
for el in f:
c.remove(el)
j = 0
k = 0
for i in range(n):
if i < l - 1:
sp.append(c[j])
j += 1
elif i > r - 1:
sp.append(c[j])
j += 1
else:
sp.append(f[k])
k += 1
print(*sp)
else:
g = max_s - s
f = list(f)
while sum(f) > s:
for i in range(c):
f[i] -= 1
i = -1
while sum(f) < s:
f[i] += 1
i -= 1
c = list(range(1, n + 1))
for el in f:
c.remove(el)
j = 0
k = 0
for i in range(n):
if i < l - 1:
sp.append(c[j])
j += 1
elif i > r - 1:
sp.append(c[j])
j += 1
else:
sp.append(f[k])
k += 1
print(*sp) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for i in range(int(input())):
n, l, r, s = [int(n) for n in input().split()]
length = r - l + 1
max_sum = length * (2 * n + 1 - length) // 2
min_sum = length * (length + 1) // 2
if s > max_sum or s < min_sum:
print("-1")
else:
left_part, right_part = [], []
for j in range(n, 0, -1):
if (
length > 0
and length * (2 * j + 1 - length) // 2 >= s
and s - j >= (length - 1) * length // 2
):
right_part.append(j)
length = length - 1
s = s - j
else:
left_part.append(j)
if s > 0:
print("-1")
else:
for j in right_part:
left_part.insert(l - 1, j)
l += 1
print(*left_part) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
for _ in range(t):
n, l, r, s = map(int, input().split())
d = r - l + 1
k = n - d
mini = d * (d + 1) // 2
maxi = d * n - d * (d - 1) // 2
if s not in range(mini, maxi + 1):
print(-1)
continue
if k == 0:
print(" ".join(map(str, range(1, n + 1))))
continue
i = s - mini
rr = i // k
ll = d - rr
c = ll + i % k
T = list(range(1, ll)) + ([c] if s != maxi else []) + list(range(n - rr + 1, n + 1))
Tt = list(range(ll, c)) + list(range(c + 1, n - rr + 1))
TT = Tt[: l - 1] + T + Tt[l - 1 :]
print(" ".join(map(str, TT))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR LIST VAR LIST FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def solve(n: int, l: int, r: int, s: int) -> str:
max_sum = 0
for i in range(n, n - (r - l + 1), -1):
max_sum += i
if s > max_sum:
return "-1"
arr = [i for i in range(1, r - l + 2)]
total = sum(arr)
if s < total:
return "-1"
nums = [i for i in range(1, n + 1)]
max_index = n - 1
curr_index = r - l
while total < s:
new_val = min(arr[curr_index] + s - total, nums[max_index])
change = new_val - arr[curr_index]
total += change
arr[curr_index] = new_val
curr_index -= 1
max_index -= 1
good_nums = set(arr)
garbage_nums = []
for i in range(1, n + 1):
if i not in good_nums:
garbage_nums.append(i)
good_index = 0
garbage_index = 0
ans = []
for i in range(n):
if i >= l - 1 and i < r:
ans.append(arr[good_index])
good_index += 1
else:
ans.append(garbage_nums[garbage_index])
garbage_index += 1
ans_str = ""
for x in ans:
ans_str += str(x) + " "
return ans_str
test_cases = int(input())
for _ in range(test_cases):
n, l, r, s = list(map(int, input().split()))
print(solve(n, l, r, s)) | FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR IF VAR VAR RETURN STRING ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN STRING ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def high(i, k):
return k * (2 * i + 1 - k) // 2
def low(k):
return k * (k + 1) // 2
for z in range(int(input())):
n, le, r, s = map(int, input().split())
s2 = s
k = r - le + 1
l = []
for i in range(n, 0, -1):
if k > 0 and high(i, k) >= s and s - i >= low(k - 1):
s -= i
l.append(i)
k -= 1
if k == 0:
break
if sum(l) != s2:
print(-1)
else:
c = [0] * (n + 1)
for i in range(len(l)):
c[l[i]] += 1
co = 0
i = 1
while co < le - 1:
if c[i] == 0:
print(i, end=" ")
co += 1
i += 1
for x in range(len(l)):
print(l[x], end=" ")
co = r
while co < n:
if c[i] == 0:
print(i, end=" ")
co += 1
i += 1
print() | FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
for _ in range(t):
def solve():
n, l, r, s = map(int, input().split())
sz = r - l + 1
lb = n - sz + 1
def cal(x, y):
return (x + y) * (y - x + 1) // 2
if cal(1, sz) <= s and s <= cal(lb, n):
ans, vis = [0] * n, [False] * (n + 1)
now, left = n, sz
for i in range(l - 1, r):
while now + cal(1, left - 1) > s:
now -= 1
ans[i] = now
s -= now
vis[now] = True
now -= 1
left -= 1
now = 1
for i in range(n):
if ans[i]:
continue
while vis[now]:
now += 1
ans[i] = now
vis[now] = True
print(" ".join(str(x) for x in ans))
else:
print(-1)
solve() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def main():
T = int(input())
while T:
n, l, r, s = map(int, input().split())
if s < (r - l + 1) * (r - l + 2) // 2 or s > (r - l + 1) * (r - l + 2) // 2 + (
r - l + 1
) * (n - (r - l + 1)):
print(-1)
else:
LR = [i for i in range(1, r - l + 2)]
s -= (r - l + 1) * (r - l + 2) // 2
i = r - l
while s:
LR[i] += min(s, n - (r - l + 1))
s -= min(s, n - (r - l + 1))
i -= 1
already_used = set(LR)
result = [0] * (l - 1) + LR + [0] * (n - r)
i = 1
for j in range(n):
if result[j] == 0:
while i in already_used:
i += 1
result[j] = i
i += 1
for i in result:
print(i, end=" ")
print()
T -= 1
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split(" "))
t = r - l + 1
l = l - 1
r = r - 1
lo = t * (t + 1) // 2
hi = t * (2 * n - t + 1) // 2
if s > hi or s < lo:
print(-1)
else:
res = [i for i in range(1, t + 1)]
res.reverse()
curr = lo
p = n
for i in range(t):
if curr + p - res[i] > s:
x = s - curr
res[i] = res[i] + x
break
elif curr + p - res[i] == s:
res[i] = p
break
else:
curr = curr + p - res[i]
res[i] = p
p = p - 1
n1 = l
n2 = n - 1 - r
ans = [0] * n
ans[l : r + 1] = res
arr2 = []
for item in range(1, n + 1):
if item not in res:
arr2.append(item)
for i in range(n):
if ans[i] == 0:
ans[i] = arr2.pop()
for item in ans:
print(item, end=" ")
print("") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = list(map(int, input().split()))
l -= 1
r -= 1
k = r - l + 1
a = [(0) for _ in range(n)]
j = 1
for i in range(l, r + 1):
a[i] = j
j += 1
s1 = k * (k + 1) // 2
s2 = k * (2 * n - k + 1) // 2
if s < s1 or s > s2:
print(-1)
else:
q = (s - s1) // k
x = (s - s1) % k
b = [(1) for _ in range(n)]
for i in range(l, r + 1):
a[i] += q
i = r
while x:
a[i] += 1
x -= 1
i -= 1
for i in range(l, r + 1):
b[a[i] - 1] = 0
j = 0
for i in range(n):
if a[i] == 0:
while b[j] == 0:
j += 1
a[i] = j + 1
j += 1
print(*a) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def answer():
m = r - l + 1
v1 = n * (n + 1) // 2 - (n - m) * (n - m + 1) // 2
v2 = m * (m + 1) // 2
if v1 < s or v2 > s:
return [-1]
req = []
taken = [False] * (n + 1)
for i in range(1, r - l + 2):
taken[i] = True
req.append(i)
need = s - sum(req)
for i in range(len(req)):
for v in range(n, 0, -1):
if not taken[v] and need + req[i] - v >= 0:
taken[v] = True
taken[req[i]] = False
need += req[i] - v
req[i] = v
break
if need == 0:
break
ans = []
for i in range(1, n + 1):
if len(ans) == l - 1:
ans.extend(req)
if i not in req:
ans.append(i)
if len(ans) == l - 1:
ans.extend(req)
return ans
for T in range(int(input())):
n, l, r, s = map(int, input().split())
print(*answer()) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN LIST NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
n, l, r, s = map(int, input().split())
k = r - l + 1
vals = [i for i in range(1, k + 1)][::-1]
sigma = sum(vals)
if sigma > s:
print(-1)
continue
ptr = -1
last = n
while sigma < s:
ptr += 1
if ptr == k:
break
delta = min(s - sigma, last - vals[ptr])
vals[ptr] += delta
sigma += delta
last -= 1
if sigma < s:
print(-1)
continue
ans = [(-1) for i in range(n)]
ans[l - 1 : r] = vals
left = []
vals = set(vals)
for i in range(1, n + 1):
if i not in vals:
left.append(i)
ptr = -1
for i in range(n):
if i < l - 1 or i >= r:
ptr += 1
ans[i] = left[ptr]
print(*ans)
assert sum(ans[l - 1 : r]) == s
assert len(ans) == n
assert sorted(ans) == [i for i in range(1, n + 1)] | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def solve(n, l, r, s):
sol = list(range(1, r - l + 2))
sol.reverse()
somme = sum(sol)
i = 0
while somme < s:
if i >= len(sol):
print(-1)
return
if sol[i] < n and (i == 0 or sol[i] < sol[i - 1] - 1):
sol[i] += 1
somme += 1
else:
i += 1
if somme < s or somme > s:
print(-1)
return
reste = [x for x in range(1, n + 1) if x not in sol]
result = reste[: l - 1] + sol + reste[l - 1 :]
print(*result)
t = int(input())
for i in range(t):
n, l, r, s = list(map(int, input().split()))
solve(n, l, r, s) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
for _ in range(t):
n, l, r, s = list(map(int, input().split()))
els = []
sels = 0
nb = r - l + 1
els = [(i + 1) for i in range(nb)]
sels = (nb + 1) * nb // 2
if sels > s:
print(-1)
continue
i = len(els) - 1
while sels < s and i >= 0:
if sels + (n - nb) < s:
els[i] += n - nb
sels += n - nb
else:
delta = s - sels
els[i] += delta
sels = s
i -= 1
if sels < s:
print(-1)
continue
j = 1
k = 0
for i in range(1, n + 1):
if i < l or i > r:
while j in els:
j += 1
print(j, end=" ")
j += 1
else:
print(els[k], end=" ")
k += 1
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def Low(k):
return k * (k + 1) // 2
def High(n, k):
return k * (2 * n - k + 1) // 2
for _ in range(int(input())):
n, l, r, s = map(int, input().split())
k = r - l + 1
r, l = r - 1, l - 1
if not High(n, k) >= s >= Low(k):
print(-1)
continue
L = [0] * n
lp, rp = l, l - 1
for i in range(n, 0, -1):
if High(i, k) >= s and s - i >= Low(k - 1) and k:
L[lp] = i
lp += 1
s -= i
k -= 1
else:
L[rp] = i
rp -= 1
if k:
print(-1)
else:
print(*L) | FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
l -= 1
k = r - l
if sum(range(1, 1 + k)) <= s <= sum(range(n + 1 - k, n + 1)):
togo = s - sum(range(1, 1 + k))
A = list(range(1 + togo // k, 1 + togo // k + k))
togo %= k
for i in range(k - togo, k):
A[i] += 1
trash = list(set(range(1, n + 1)) - set(A))
print(*(trash[:l] + A + trash[l:]))
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | import sys
N = int(200000.0 + 5)
sys.setrecursionlimit(N)
def charming():
n, l, r, s = map(int, input().split())
len = r - l + 1
def cal(l, r) -> int:
return (r - l + 1) * (r + l) // 2
if cal(n - len + 1, n) < s or cal(1, len) > s:
print(-1)
return
cur = n
used = set()
ans = [0] * (n + 1)
for i in range(l, r + 1):
while cur + cal(1, r - i) > s:
cur -= 1
s -= cur
ans[i] = cur
used.add(cur)
cur -= 1
cur = 1
for i in range(1, n + 1):
while cur in used:
cur += 1
if ans[i]:
continue
ans[i] = cur
cur += 1
for i in range(1, n + 1):
print(ans[i], end=" ")
print()
for t in range(int(input())):
charming() | IMPORT ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
k = [0] * n
p = r - l + 1
t = n * (n + 1) // 2
q = (n - p) * (n - p + 1) // 2
if p * (p + 1) // 2 > s or t - q < s:
print(-1)
continue
a = 1
p = 0
t = set()
for i in range(l - 1, r):
k[i] = a
p += a
t.add(a)
a += 1
while s - p:
for i in range(l - 1, r):
if k[i] + s - p <= n and k[i] + s - p not in t:
t.add(k[i] + s - p)
t.discard(k[i])
k[i] = k[i] + s - p
p = s
break
elif k[i] + s - p > n:
a = n
while a in t:
a -= 1
t.remove(k[i])
t.add(a)
p -= k[i]
k[i] = a
p += k[i]
q = []
for i in range(1, n + 1):
if i not in t:
q.append(i)
for i in range(n):
if k[i] == 0:
k[i] = q.pop()
print(*k) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
c = 1
f = []
for i in range(l, r + 1):
f.append(c)
c += 1
ll = r - l + 1
k = 0
i = ll - 1
while True:
if sum(f) == s:
break
if f[i] > n:
k = 1
break
f[i] += 1
i -= 1
i %= ll
if k:
print(-1)
else:
ans = []
c = 0
aa = []
kk = 0
for i in range(1, n + 1):
if i not in f:
ans.append(i)
for i in range(n):
if i >= l - 1 and i < r:
aa.append(f[c])
c += 1
else:
aa.append(ans[kk])
kk += 1
k = 0
for i in range(n):
if aa[i] > n:
k = 1
if k:
print(-1)
continue
print(*aa) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
w = r - l + 1
m = sum(range(n, n - w, -1))
if s > m or s < sum(range(1, w + 1)):
print(-1)
continue
j = n - w + 1
arr = set()
for i in range(1, n + 1):
if m - j + i >= s:
arr.add(i)
m -= j
m += i
j += 1
s1 = {*range(1, n + 1)} - arr
ans = list(s1)[: l - 1] + list(arr) + list(s1)[l - 1 :]
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, total = map(int, input().split())
mini, maxi, a, b = 0, 0, 1, n
s = total
for i in range(l - 1, r):
mini += a
maxi += b
b -= 1
a += 1
if total > maxi or total < mini:
print(-1)
continue
a = [i for i in range(1, n + 1)]
stack = []
length = r - l + 1
now = True
for i in range(l - 1, r):
if i == r - 1:
sumleft = total - sum(stack)
if sumleft in a:
stack.append(sumleft)
a.remove(sumleft)
else:
now = False
else:
for j in range(len(a) - 1, -1, -1):
ele = a[j]
tmp = length - len(stack) - 1
val = tmp * (tmp + 1) // 2
if total - sum(stack) - ele >= val:
stack.append(ele)
a.remove(ele)
break
if len(stack) != i - (l - 1) + 1:
now = False
if now == False:
print(-1)
continue
ans = [-1] * n
pos = 0
for i in range(l - 1):
ans[i] = a[pos]
pos += 1
for i in range(r, n):
ans[i] = a[pos]
pos += 1
pos = 0
for i in range(l - 1, r):
ans[i] = stack[pos]
pos += 1
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
c = r - l + 1
if s >= c * (c + 1) // 2 and s <= c * (2 * n - c + 1) // 2:
sett = set()
ss = 0
cnt = c - 1
for i in range(1, n + 1):
if ss + i <= s and ss + i + cnt * (2 * n - cnt + 1) // 2 >= s:
ss += i
cnt -= 1
sett.add(i)
lst = [0] * (n + 1)
i = l
for j in sett:
lst[i] = j
i += 1
i = v = 1
while i <= n:
if i < l or i > r:
if v not in sett:
lst[i] = v
v += 1
i += 1
else:
v += 1
else:
i += c
print(*lst[1:])
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in " " * int(input()):
n, l, r, s = map(int, input().split())
if n * (n + 1) // 2 - (n - r + l - 1) * (n - r + l) // 2 < s:
print(-1)
continue
if (r - l + 1) * (r - l + 2) // 2 > s:
print(-1)
continue
x = n
add = [(i + 1) for i in range(r - l + 1)]
sa = sum(add)
ind = 0
while True:
if sa == s:
break
if add[r - l - ind] != n - ind:
add[r - l - ind] += 1
sa += 1
else:
ind += 1
if ind >= n:
break
if sum(add) != s:
print(-1)
continue
al = [(i + 1) for i in range(n)]
puti = sorted(add)[::-1]
perm = [0] * n
for i in range(l, r + 1):
perm[i - 1] = puti.pop(0)
del al[perm[i - 1] - 1]
for i in range(n):
if not perm[i]:
perm[i] = al.pop()
print(*perm) | FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
for _ in range(t):
n, l, r, s = map(int, input().split())
l -= 1
r -= 1
c = r - l + 1
if c * (c + 1) // 2 > s or c * (2 * n + 1 - c) // 2 < s:
print(-1)
continue
sec = [i for i in range(1, c + 1)]
sec.reverse()
s -= c * (c + 1) // 2
cnt = 0
while s > 0:
s -= 1
sec[cnt] += 1
cnt = (cnt + 1) % c
used = [False] * n
ans = [0] * n
ans[l : r + 1] = sec
for i in sec:
used[i - 1] = True
iter = 0
for i in range(n):
if ans[i]:
continue
while used[iter]:
iter += 1
used[iter] = True
ans[i] = iter + 1
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = list(map(int, input().split()))
l -= 1
r -= 1
k = r - l + 1
s1 = k * (k + 1) // 2
s2 = k * (2 * n - k + 1) // 2
if s < s1 or s > s2:
print(-1)
else:
a = [(i + 1) for i in range(n)]
last = n
mid = []
while k:
poss = s - k * (k - 1) // 2
t = min(poss, last)
s = s - t
mid.append(t)
last = last - 1
k = k - 1
rest = set(a) - set(mid)
rest = list(rest)
ans = rest[:l] + mid + rest[l:]
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
for i in range(t):
n, l, r, s = map(int, input().split())
ans = 0
s1 = 0
k = 1
a = []
for j in range(r - l + 1):
s1 = s1 + k
a.append(k)
k = k + 1
s2 = 0
k = n
for j in range(r - l + 1):
s2 = s2 + k
k = k - 1
if s <= s2 and s >= s1:
k = r - l + 1
o = n
for j in range(r - l + 1):
if s1 == s:
break
elif s - s1 <= o - k:
a[k - 1] = a[k - 1] + (s - s1)
s1 = s1 + (s - s1)
else:
a[k - 1] = o
s1 = s1 + (o - k)
k = k - 1
o = o - 1
j = 1
pn = 1
while j < l:
if pn not in a:
print(pn, end=" ")
j = j + 1
pn = pn + 1
for j in a:
print(j, end=" ")
j = r + 1
while j <= n:
if pn not in a:
print(pn, end=" ")
j = j + 1
pn = pn + 1
print()
else:
print("-1") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
for _ in range(t):
n, l, r, s = [int(num) for num in input().strip().split(" ")]
seq = [i for i in range(1, n + 1)]
k = r - l + 1
low = sum(seq[:k])
high = sum(seq[n - k :])
if s < low or s > high:
print(-1)
continue
used = set()
for i in reversed(seq):
ns = s - i
if ns < sum(seq[: k - len(used) - 1]):
continue
s = ns
used.add(i)
res = [0] * n
l -= 1
r -= 1
unused = {i for i in range(1, n + 1)} - used
for i in range(n):
if l <= i and i <= r:
res[i] = used.pop()
else:
res[i] = unused.pop()
for i in res:
print(i, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | from sys import stdin
input = stdin.buffer.readline
t = int(input())
for i in range(t):
n, l, r, s = map(int, input().split())
k = r - l + 1
if s < k * (k + 1) // 2 or s > k * (2 * n + 1 - k) // 2:
print(-1)
continue
arr = [0] * (n + 1)
for i in range(1, k + 1):
arr[i] = 1
s = s - k * (k + 1) // 2
i = k
j = n
while s > 0:
x = min(s, j - i)
arr[i + x] = 1
arr[i] = 0
s = s - x
i = i - 1
j = j - 1
left = []
mid = []
right = []
for i in range(1, n + 1):
if arr[i] == 1:
mid.append(i)
elif len(left) == l - 1:
right.append(i)
else:
left.append(i)
print(*left, *mid, *right, sep=" ") | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for test in range(int(input())):
n, l, r, s = map(int, input().split())
lst = [i for i in range(n, 0, -1)]
x = r - l + 1
store = set()
for i in range(n):
if x * (x - 1) // 2 <= s - lst[i]:
store.add(lst[i])
x -= 1
s -= lst[i]
if len(store) == r - l + 1:
arr = [i for i in range(1, n + 1) if i not in store]
arr[l - 1 : l - 1] = store
print(*arr)
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
l -= 1
r -= 1
x = r - l + 1
a = [(n - i) for i in range(x)]
temps = sum(a)
if temps < s:
print(-1)
continue
elif temps == s:
b = [0] * n
j = 1
for i in range(n):
if i >= l and i <= r:
b[i] = a[i - l]
else:
b[i] = j
j += 1
print(*b)
continue
else:
d = temps - s
a.reverse()
m = 1
for i in range(x):
if d != 0:
num2 = max(m, a[i] - d)
d -= a[i] - num2
a[i] = num2
m += 1
else:
break
if d != 0:
print(-1)
continue
b = [0] * n
c = [(i + 1) for i in range(n)]
for num in a:
c.remove(num)
j = 0
for i in range(n):
if i >= l and i <= r:
b[i] = a[i - l]
else:
b[i] = c[j]
j += 1
print(*b) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for w in range(int(input())):
n, left, right, s = tuple(map(int, input().split()))
length = right - left + 1
l = [(i + 1) for i in range(n)]
temp = 0
keys = []
ans = [-1] * n
for i in range(length):
temp += l[i]
keys.append(i)
pointer = length - 1
k = keys[pointer]
final = n
while temp < s and pointer >= 0:
while temp < s and k + 1 < final:
keys[pointer] += 1
k += 1
temp += 1
if temp == s:
break
final = k
pointer -= 1
if pointer >= 0:
k = keys[pointer]
if left == right:
if s > n:
print(-1)
else:
ans[left - 1] = s
count = 1
for i in range(n):
if i != left - 1:
if count == s:
count += 1
ans[i] = count
count += 1
for i in range(n):
print(ans[i], end=" ")
print()
elif temp < s or temp > s:
print(-1)
else:
count = 0
d = {}
for i in range(left - 1, right):
ans[i] = l[keys[count]]
d[l[keys[count]]] = 1
count += 1
count = 1
for i in range(n):
if i < left - 1 or i >= right:
if count not in d:
ans[i] = count
count += 1
else:
while count in d:
count += 1
ans[i] = count
count += 1
for i in range(n):
print(ans[i], end=" ")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in " " * int(input()):
n, l, r, s = map(int, input().split())
p, a, f = [], [0] * n, [0] * 5000
for i in range(1, n + 1):
p.append(i)
w = list(p[: r - l + 1])
b = sum(w)
if not b <= s <= sum(p[-(r - l + 1) :]):
print(-1)
else:
c = s - b
m = c % (r - l + 1)
w[-1] = p[r - l] + c // (r - l + 1)
for i in range(len(w) - 2, -1, -1):
w[i] = w[i + 1] - 1
for i in range(len(w) - 1, -1, -1):
if not m:
break
m -= 1
w[i] += 1
for i in range(l, r + 1):
f[w[-1]] = 1
a[i - 1] = w.pop()
for i in range(n):
if not a[i]:
for j in range(n):
if not f[p[j]]:
a[i] = p.pop(j)
break
print(*a) | FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | I = lambda: [*map(int, input().split())]
def out(x):
print(str(x))
def solve():
n, l, r, s = I()
c = [i for i in range(1, r - l + 2)]
sm = sum(c)
if sm > s:
out(-1)
return
ci = len(c) - 1
free = set(i for i in range(r - l + 2, n + 1))
used = set(c)
while ci >= 0 and sm < s:
if c[ci] + 1 not in free:
ci -= 1
continue
used.remove(c[ci])
free.add(c[ci])
used.add(c[ci] + 1)
free.remove(c[ci] + 1)
c[ci] += 1
sm += 1
if sm != s:
out(-1)
return
result = [f"{i}" for i in free]
result = result[: l - 1] + list(map(str, c)) + result[l - 1 :]
out(" ".join(result))
(t,) = I()
for i in range(t):
solve() | ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for i in range(int(input())):
n, l, r, s = map(int, input().split())
d = r - l + 1
mi = d * (d + 1) // 2
ma = n * (n + 1) // 2 - (n - d) * (n - d + 1) // 2
if s < mi or s > ma:
print(-1)
else:
li = []
for jj in range(d):
li.append(jj + 1)
s -= jj + 1
zz = n
for jj in range(d - 1, -1, -1):
if s == 0:
break
if s < zz - li[jj]:
li[jj] += s
s = 0
else:
s -= zz - li[jj]
li[jj] = zz
zz -= 1
rr = []
for jj in range(1, n + 1):
if jj not in li:
rr.append(jj)
p, q = 0, 0
for jj in range(1, n + 1):
if jj >= l and jj <= r:
print(li[p], end=" ")
p += 1
else:
print(rr[q], end=" ")
q += 1
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | T = int(input())
for _ in range(T):
n, l, r, k = map(int, input().split())
_len = r - l + 1
lb = (_len + 1) * _len // 2
ub = (n + n - _len + 1) * _len // 2
ans = [0] * n
used = []
if lb <= k <= ub:
dis = k - lb
step = dis // _len
rem = dis % _len
for i in range(_len):
start = 1 + step
if i > _len - rem - 1:
used.append(i + start + 1)
else:
used.append(i + start)
cnt = 0
for i in range(1, n + 1):
if l <= i <= r:
ans[i - 1] = used[cnt]
cnt += 1
continue
for num in range(1, n + 1):
if num not in used:
used.append(num)
ans[i - 1] = num
break
print(*ans)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def solve(n, lft, r, s):
l = r - lft + 1
arr = [i for i in range(1, l + 1)]
sm = l * (l + 1) // 2
if s < sm:
return -1
if s > sm:
x = (s - sm) // l
xm = s - (x * l + sm)
if x + arr[-1] > n:
return -1
if x + arr[-1] == n:
if xm != 0:
return -1
for i in range(l):
arr[i] += x
sm += x
for i in range(l - 1, -1, -1):
if s == sm:
break
arr[i] += 1
sm += 1
la = []
for i in range(1, n + 1):
if i not in arr:
la.append(i)
lst = []
cn = 0
ca = 0
b = 0
for i in range(1, n + 1):
if i == lft:
b = 1
if b == 0:
lst.append(la[cn])
cn += 1
else:
lst.append(arr[ca])
ca += 1
if i == r:
b = 0
return lst
t = int(input())
for i in range(t):
n, l, r, s = map(int, input().split(" "))
x = solve(n, l, r, s)
if x == -1:
print(-1)
else:
print(*x) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
x = r - l + 1
if x * (x + 1) // 2 > s:
print(-1)
elif n * (n + 1) / 2 - (n - x) * (n - x + 1) / 2 < s:
print(-1)
else:
ans = [(0) for i in range(n)]
i = n
j = l - 1
while j < r:
tt = x * (x - 1) / 2 + i
if tt < s:
s -= i
ans[j] = i
x -= 1
i -= 1
j += 1
elif tt == s:
ans[j] = i
i = 1
j += 1
while j < r:
ans[j] = i
j += 1
i += 1
else:
i -= 1
s = set(ans)
i = 0
j = 1
while i < n:
if ans[i] != 0:
i += 1
elif j in s:
j += 1
else:
ans[i] = j
i += 1
j += 1
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def out(ans, n):
num = [i for i in range(1, n + 1) if i not in ans]
for i in range(n):
if ans[i] == 0:
ans[i] = num.pop()
print(" ".join(map(str, ans)))
t = int(input())
for _ in range(t):
n, l, r, s = [int(x) for x in input().split(" ")]
ans = [0] * n
ex = r - l + 1
low = ex * (ex + 1) // 2
high = 0
lst = n
for i in range(ex):
high += lst
lst -= 1
if s < low or s > high:
print(-1)
continue
lst = 1
tot = 0
for i in range(l - 1, r):
ans[i] = lst
lst += 1
tot += ans[i]
pos = r - 1
nxt = n
while tot != s:
if ans[pos] == nxt:
nxt -= 1
pos -= 1
ans[pos] += 1
tot += 1
out(ans, n) | FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | import sys
def main():
t = int(input())
allans = []
for _ in range(t):
n, l, r, s = readIntArr()
l -= 1
r -= 1
requiredCnt = r - l + 1
minPossible = sum(range(1, requiredCnt + 1))
maxPossible = sum(range(n, n - requiredCnt, -1))
if s < minPossible or s > maxPossible:
allans.append([-1])
else:
v = minPossible
subArray = list(range(1, requiredCnt + 1))
topNumberForUse = n
while v < s:
for i in range(requiredCnt):
if (
topNumberForUse > subArray[i]
and v - subArray[i] + topNumberForUse <= s
):
v = v - subArray[i] + topNumberForUse
subArray[i] = topNumberForUse
break
topNumberForUse -= 1
unusedNumbers = set(range(1, n + 1))
for x in subArray:
unusedNumbers.remove(x)
unusedNumList = list(unusedNumbers)
unusedI = 0
left = []
right = []
for i in range(n):
if i < l:
left.append(unusedNumList[unusedI])
unusedI += 1
elif i > r:
right.append(unusedNumList[unusedI])
unusedI += 1
ans = left + subArray + right
allans.append(ans)
multiLineArrayOfArraysPrint(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 makeArr(defaultVal, dimensionArr):
dv = defaultVal
da = dimensionArr
if len(da) == 1:
return [dv for _ in range(da[0])]
else:
return [makeArr(dv, da[1:]) for _ in range(da[0])]
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 _abc in range(1):
main() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR 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 ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER 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 |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def solve(N, L, R, S):
n = N
max_val, min_val = 0, (1 + R - L + 1) * (R - L + 1) // 2
for _ in range(R - L + 1):
max_val += n
n -= 1
if not min_val <= S <= max_val:
return -1
diff = max_val - S
can = []
for i in range(R - L + 1):
can += [N - i]
can.reverse()
idx = 0
while diff:
curr = min(diff, can[idx] - (idx + 1))
can[idx] -= curr
diff -= curr
idx += 1
ans = []
idx = 0
all_can = set([(i + 1) for i in range(N)])
for c in can:
all_can.remove(c)
for i in range(1, N + 1, 1):
if i < L or i > R:
ans += [str(all_can.pop())]
else:
ans += [str(can[idx])]
idx += 1
return " ".join(ans)
T = int(input())
for _ in range(T):
N, L, R, S = map(int, input().split())
print(solve(N, L, R, S)) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR LIST BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def low(k):
return k * (k + 1) / 2
def high(n, k):
return k * (2 * n + 1 - k) / 2
for _ in range(int(input())):
n, l, r, s = map(int, input().split())
k = r - l + 1
minSumm = k * (k + 1) / 2
maxSumm = k * (2 * n - k + 1) / 2
segment = []
used = [-1] * n
for i in range(n, -1, -1):
if k > 0:
if high(i, k) >= s and s - i >= low(k - 1):
s -= i
k -= 1
segment.append(i)
used[i - 1] = 1
unused = []
for i in range(n):
if used[i] != 1:
unused.append(i + 1)
if s != 0:
print(-1)
continue
else:
j = 0
for i in range(n):
if i >= l - 1 and i <= r - 1:
print(segment[i - l + 1], end=" ")
else:
print(unused[j], end=" ")
j += 1
print() | FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
low = lambda k: k * (k + 1) // 2
high = lambda n, k: k * (2 * n - k + 1) // 2
for ti in range(t):
n, l, r, s = map(int, input().split())
k = r - l + 1
if s < low(k) or s > high(n, k):
print(-1)
continue
ans = []
for i in range(n, 0, -1):
if k > 0 and high(i, k) >= s and s - i >= low(k - 1):
ans.append(i)
s -= i
k -= 1
ans = set(ans)
setDiff = set(range(1, n + 1)) - ans
setDiff, ans = list(setDiff), list(ans)
setDiff[l - 1 : l - 1] = ans
for i in setDiff:
print(i, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | from sys import stdin
t = int(stdin.readline().strip())
for i in range(t):
lst = [int(x) for x in stdin.readline().strip().split()]
n = lst[0]
l = lst[1]
r = lst[2]
s = lst[3]
d = r - l + 1
e = n - d
if d * (d + 1) // 2 > s:
print(-1)
continue
if n * (n + 1) // 2 - e * (e + 1) // 2 < s:
print(-1)
continue
av = s // d
missing = s - av * d
liste = []
halb = d // 2
for k in range(halb):
liste.append(av - halb + k)
for k in range(halb):
liste.append(av + k + 1)
if d % 2 == 1:
liste.insert(halb, av)
result = [(x + 1) for x in range(n)]
if d % 2 == 0:
for k in range((d - 1) // 2, -1, -1):
if missing > 0:
liste[k] += 1
missing -= 1
for k in range(len(liste) - 1, -1, -1):
if missing > 0:
liste[k] += 1
missing -= 1
del result[liste[k] - 1]
for k in range(len(result)):
if k + 1 == l:
for elem in liste:
print(elem, "", end="")
print(result[k], "", end="")
if r == n:
for elem in liste:
print(elem, "", end="")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR VAR STRING STRING IF VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
for _ in range(t):
n, l, r, s = (int(i) for i in input().split())
nums = r - l + 1
mins = (nums + 1) * nums // 2
maxs = (n + n - nums + 1) * nums // 2
if s < mins or s > maxs:
print(-1)
continue
lst = list(range(1, nums + 1))
d = s - mins
i = 1
maxnum = n
while d:
add = d if maxnum - lst[nums - i] > d else maxnum - lst[nums - i]
lst[nums - i] += add
d -= add
i += 1
maxnum -= 1
used = set(lst)
counter = 1
for i in range(1, n + 1):
if i < l or i > r:
while counter in used:
counter += 1
print(counter, end=" ")
counter += 1
else:
print(lst[i - l], end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = [int(x) for x in input().split()]
diff = r - l + 1
lal = diff - 1
lala = lal * (lal + 1) // 2
if s < diff * (diff + 1) // 2 or s > diff * n - lala:
print(-1)
continue
main = [i for i in range(1, diff + 1)]
sum = diff * (diff + 1) // 2
main_diff = s - sum
i = diff - 1
ver = n
while i >= 0:
if main_diff + main[i] > ver:
main_diff -= ver - main[i]
main[i] = ver
ver -= 1
i -= 1
else:
main[i] += main_diff
break
j = 0
el = 1
for i in range(n):
if i >= l - 1 and i <= r - 1:
print(main[j], end=" ")
j += 1
else:
while el in main:
el += 1
print(el, end=" ")
el += 1
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
subarr = [i for i in range(1, r - l + 2)]
if (
s > len(subarr) * (2 * n - len(subarr) + 1) // 2
or s < len(subarr) * (len(subarr) + 1) // 2
):
print(-1)
continue
minsum = (r - l + 1) * (r - l + 2) // 2
diff = s - minsum
marked = [(0) for i in range(501)]
for i in range(len(subarr)):
subarr[i] = subarr[i] + diff // len(subarr)
marked[subarr[i]] = 1
for i in range(diff % len(subarr)):
marked[subarr[len(subarr) - 1 - i]] = 0
subarr[len(subarr) - 1 - i] += 1
marked[subarr[len(subarr) - 1 - i]] = 1
ans = []
i = 1
ele = 0
while ele < l - 1:
if marked[i] == 0:
ans.append(i)
ele += 1
i += 1
ans.extend(subarr)
ele = 0
while ele < n - r:
if marked[i] == 0:
ans.append(i)
ele += 1
i += 1
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def ari(n, start):
end = start + n - 1
return (start + end) * n // 2
for _ in range(int(input())):
n, l, r, s = map(int, input().split())
k = r - l + 1
if ari(k, 1) > s:
print(-1)
continue
if ari(k, n - k + 1) < s:
print(-1)
continue
segment = []
for i in range(n - k + 2):
if ari(k, i) >= s:
segment = list(range(i, i + k))
diff = ari(k, i) - s
for seg_i in range(diff):
segment[seg_i] -= 1
break
used = [0] * (n + 1)
for v in segment:
used[v] = 1
other = []
for i in range(1, n + 1):
if not used[i]:
other.append(i)
ans = other[: l - 1] + segment + other[l - 1 :]
print(*ans) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
summ1, summ2 = 0, 0
for i in range(1, r - l + 2):
summ1 += i
for i in range(n - r + l, n + 1):
summ2 += i
if summ1 <= s <= summ2:
a = []
for i in range(
(s - summ1) // (r - l + 1) + 1, (s - summ1) // (r - l + 1) + 1 + (r - l + 1)
):
a.append(i)
for i in range(r - l + 1 - (s - summ1) % (r - l + 1), r - l + 1):
a[i] += 1
a1 = set()
for i in range(1, n + 1):
a1.add(i)
a1 -= set(a)
a1 = list(a1)
print(*(a1[: l - 1] + a + a1[l - 1 :]))
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | import sys
def solve(n, l, r, s):
l -= 1
r -= 1
ll = r - l + 1
if s > (n + n - ll + 1) * ll // 2 or s < (1 + ll) * ll // 2:
print(-1)
else:
ans = [None] * n
arr = list(range(1, n + 1))
p_sum = sum(arr[:ll])
found = False
for i in range(ll, n):
if p_sum <= s < p_sum + ll:
found = True
tmp = arr[i - ll : i]
if s > p_sum:
tmp.remove(arr[i] - (s - p_sum))
tmp.append(arr[i])
break
p_sum += arr[i]
p_sum -= arr[i - ll]
if not found:
if p_sum == s:
tmp = arr[n - ll :]
ans[l : r + 1] = tmp.copy()
t = set(tmp)
j = 0
for i in range(1, n + 1):
if i not in t:
while j < n and ans[j] != None:
j += 1
ans[j] = i
print(*ans)
T = int(input())
for t in range(T):
n, l, r, s = map(int, input().split())
solve(n, l, r, s) | IMPORT FUNC_DEF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR NONE VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
x = r - l + 1
y = sum(range(n - x + 1, n + 1))
z = sum(range(1, x + 1))
if s > y or s < z:
print(-1)
else:
test = n - x + 1
s1 = []
for i in range(1, n + 1):
if y - test + i >= s:
s1.append(i)
y += i - test
test += 1
s2 = list(set(range(1, n + 1)) - set(s1))
ans = s2[: l - 1] + s1 + s2[l - 1 :]
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
while t:
t -= 1
n, l, r, s = map(int, input().split())
interval = r - l + 1
if s < interval * (interval + 1) // 2 or s > (2 * n - interval + 1) * (
interval // 2
) + interval % 2 * ((2 * n - interval + 1) // 2):
print(-1)
continue
s -= interval * (interval + 1) // 2
forward, remained = s // interval, s % interval
res = [0] * n
start = 1 + forward
end = start + interval - 1 + remained
if end < n + 1:
for i in range(l, r):
res[i - 1] = start + i - l
res[r - 1] = end
exist = set(res[l - 1 : r])
nonexist = list(set([i for i in range(1, n + 1)]) - exist)
res[: l - 1] = nonexist[: l - 1]
res[r:] = nonexist[l - 1 :]
else:
start += 1
end = start + interval - 1
total = 0
for i in range(start, start + interval):
total += i
start -= total - s - interval * (interval + 1) // 2
if start > 0:
res[l - 1] = start
temp = end
for i in range(r - 1, l - 1, -1):
res[i] = temp
temp -= 1
exist = set(res[l - 1 : r])
nonexist = list(set([i for i in range(1, n + 1)]) - exist)
res[: l - 1] = nonexist[: l - 1]
res[r:] = nonexist[l - 1 :]
else:
start += total - s - interval * (interval + 1) // 2
end = start + interval - 1
m = [i for i in range(start, end + 1)]
total = sum(m)
ix = 0
while total != s + interval * (interval + 1) // 2:
m[ix % interval] -= 1
ix += 1
total -= 1
res[l - 1 : r] = m
exist = set(res[l - 1 : r])
nonexist = list(set([i for i in range(1, n + 1)]) - exist)
res[: l - 1] = nonexist[: l - 1]
res[r:] = nonexist[l - 1 :]
print(*res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
z = r - l + 1
m = 0
t = 0
temp = n - z + 1
while t < z:
m += n - t
t += 1
mi = z * (z + 1) // 2
if s >= mi and s <= m:
a = []
v = []
for i in range(n):
v.append(0)
for i in range(1, n + 1):
if s - i <= m - temp:
a.append(i)
s -= i
m -= temp
temp += 1
v[i - 1] = 1
j = 0
for i in range(l - 1):
while v[j] == 1:
j += 1
print(j + 1, end=" ")
j += 1
print(*a, end=" ")
for i in range(r, n):
while v[j] == 1:
j += 1
print(j + 1, end=" ")
j += 1
print()
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | import sys
input = sys.stdin.readline
t = int(input())
for you in range(t):
l = input().split()
n = int(l[0])
r = int(l[2])
s = int(l[3])
l = int(l[1])
ok = 0
for i in range(n, n - (r - l + 1), -1):
ok += i
if s > ok:
print(-1)
continue
if s < r - l + 1:
print(-1)
continue
l -= 1
r -= 1
p = [(0) for i in range(n)]
for i in range(l, r + 1):
p[i] = i - l + 1
z = sum(p[l : r + 1])
if s < z:
print(-1)
continue
s -= z
currmax = n
for i in range(r, l - 1, -1):
inc = currmax - p[i]
if inc < s:
p[i] += inc
s -= inc
currmax -= 1
else:
p[i] += s
break
hashi = dict()
for i in range(1, n + 1):
hashi[i] = 1
for i in range(l, r + 1):
del hashi[p[i]]
op = []
for i in hashi:
op.append(i)
curr = 0
for i in range(n):
if p[i] == 0:
p[i] = op[curr]
curr += 1
for i in range(n):
print(p[i], end=" ")
print() | IMPORT 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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
x = r - l + 1
f = 0
ai = []
for i in range(x):
ai.append(i + 1)
if sum(ai) > s:
print(-1)
else:
k = (s - sum(ai)) // x
y = (s - sum(ai)) % x
for i in range(x):
ai[i] = ai[i] + k
for i in range(y):
ai[x - i - 1] = ai[x - i - 1] + 1
for i in range(x):
if ai[i] > n - r + l + i:
f = 1
break
if f == 1:
print(-1)
else:
li = [1] * (n + 1)
for ele in ai:
li[ele] = 0
ki = []
for i in range(1, n + 1):
if li[i] != 0:
ki.append(i)
li = ki[: l - 1] + ai + ki[l - 1 :]
print(*li) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def solve():
n, l, r, s = [int(i) for i in input().split()]
k = r - l + 1
l1 = [0] * n
l2 = [i for i in range(1, n + 1)]
l3 = []
low = k * (k + 1) // 2
high = k * (2 * n + 1 - k) // 2
if s < low or high < s:
print(-1)
return
c = l - 1
for i in range(n, 0, -1):
if k > 0 and s <= k * (2 * i + 1 - k) // 2 and s - i >= k * (k - 1) // 2:
l1[c] = i
c += 1
s -= i
k -= 1
l2[i - 1] = -1
for i in range(n):
if l2[i] != -1:
l3.append(l2[i])
c1 = 0
for i in range(l - 1):
l1[i] = l3[c1]
c1 += 1
for i in range(r, n):
l1[i] = l3[c1]
c1 += 1
print(*l1)
return
def main():
T = int(input())
for t in range(T):
solve()
return
main() | FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR 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 RETURN EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def question5():
Length, Left, Right, sum_from_l_r = map(int, input().split())
sub_length = Right - Left + 1
remain = Length - sub_length
if (
sub_length * (sub_length + 1) // 2
<= sum_from_l_r
<= Length * (Length + 1) // 2 - remain * (remain + 1) // 2
):
initial = [i for i in range(1, sub_length + 1)]
sum_initial = sum(initial)
last = Length
last_sub = len(initial) - 1
sum_left = sum_from_l_r - sum_initial
while sum_initial < sum_from_l_r:
if last - initial[last_sub] >= sum_left:
initial[last_sub] += sum_left
sum_initial += sum_left
break
else:
sum_left -= last - initial[last_sub]
sum_initial += last - initial[last_sub]
initial[last_sub] = last
last -= 1
last_sub -= 1
avail_in_l_r = [(False) for i in range(Length + 1)]
for i in range(len(initial)):
avail_in_l_r[initial[i]] = True
answer = []
j = 1
in_it = 0
for i in range(1, Length + 1):
if Left <= i <= Right:
answer.append(initial[in_it])
in_it += 1
else:
while True:
if not avail_in_l_r[j]:
answer.append(j)
j += 1
break
else:
j += 1
return answer
else:
return -1
remained_test_cases = int(input())
while remained_test_cases > 0:
ans = question5()
if ans == -1:
print(-1)
else:
print(*ans)
remained_test_cases -= 1 | FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
ct = r - l + 1
a = list(range(1, ct + 1))
high = n
sm = sum(a)
if sm > s:
print(-1)
continue
for j in range(ct - 1, -1, -1):
if sm == s:
break
s1 = sm - a[j]
cand = min(high, s - s1)
a[j] = cand
high = cand - 1
sm = s1 + cand
if sm != s:
print(-1)
else:
nums = [d for d in range(1, n + 1) if d not in a]
res = []
while len(res) < n:
if a and len(res) + 1 >= l:
res.append(a.pop(0))
else:
res.append(nums.pop(0))
print(" ".join(map(str, res))) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def solve(n, l, r, s):
a = r - l + 1
u = (n - a + 1 + n) * a // 2
w = (a + 1) * a // 2
if not w <= s <= u:
print(-1)
return
m = list(reversed(range(1, a + 1)))
s -= w
for i in range(a):
y = n - i
if s > y - m[i]:
s -= y - m[i]
m[i] = y
else:
m[i] += s
break
t = set(m)
b = []
c = 1
for i in range(l - 1):
while c in t:
c += 1
b += [c]
t.add(c)
f = []
for i in range(n - r):
while c in t:
c += 1
f += [c]
t.add(c)
print(*b, *m, *f)
t = int(input())
for _ in range(t):
n, l, r, s = map(int, input().split())
solve(n, l, r, s) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR VAR VAR NUMBER VAR LIST VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def gauss(a, b):
return (a + b) * (b - a + 1) // 2
for t in range(int(input())):
n, l, r, s = map(int, input().split())
s2 = s
cnt = r - l + 1
taken = set()
for i in range(1, cnt + 1):
for j in range(n - i + 1, 0, -1):
if gauss(1, cnt - i) + j <= s:
taken.add(j)
s -= j
break
else:
s -= 921021581234050283
rest = list(set(range(1, n + 1)) - taken)
if s == 0 and sum(taken) == s2:
print(*rest[: l - 1], *taken, *rest[l - 1 :])
else:
print("-1") | FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
z = r - l + 1
p = 0
q = 0
i = 0
b = list([(i + 1) for i in range(n)])
a = list(b[:z])
while i < z:
p = p + i + 1
q = q + n - i
i += 1
c = s - p
if s <= q and s >= p:
i = -1
while 1 > 0:
if c == 0:
break
u = b[i] - a[i]
if c > u:
a[i] = b[i]
c -= u
else:
a[i] = a[i] + c
c = 0
i -= 1
for x in a:
b.remove(x)
b = b[: l - 1] + a + b[l - 1 :]
print(*(i for i in b))
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers β a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
def high(i, k):
return k * (2 * i + 1 - k) / 2
def low(k):
return k * (k + 1) / 2
def fun(n, width, s):
rset = []
if width == 1:
rset.append(s)
return rset
if s == width * (width + 1) // 2:
return list(range(1, width + 1))
if high(n, width) >= s and s - n >= low(width - 1):
rset.append(n)
rset += fun(n - 1, width - 1, s - n)
else:
rset += fun(n - 1, width, s)
return rset
for _ in range(t):
n, l, r, s = map(int, input().split())
ran = list(range(1, n + 1))
width = r - l + 1
if s < sum(ran[:width]) or s > sum(ran[-width:]):
print(-1, end="")
else:
lr = fun(n, width, s)
non_lr = [ele for ele in ran if ele not in lr]
res = non_lr[: l - 1] + lr + non_lr[l - 1 :]
for ele in res:
print(ele, end=" ")
print("") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.