description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again.
The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view):
<image>
Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut.
To understand the problem better please read the notes to the test samples.
Input
The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise.
Output
Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled.
Examples
Input
-++-
Output
Yes
Input
+-
Output
No
Input
++
Output
Yes
Input
-
Output
No
Note
The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses.
In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled:
<image>
In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher:
<image>
In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself:
<image> | str = input()
if len(str) % 2 != 0:
print("No")
exit()
stack = ["0"]
for c in str:
if c == stack[-1]:
stack.pop()
else:
stack.append(c)
if len(stack) == 1:
print("Yes")
else:
print("No") | ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST STRING FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if len(s) < k:
return False
seen = {}
odd = 0
for letter in s:
seen[letter] = seen.get(letter, 0) + 1
for value in seen.values():
if value % 2 != 0:
odd += 1
if odd > k:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if len(s) < k:
return False
dict = {}
for st in s:
if st in dict:
dict[st] += 1
else:
dict[st] = 1
countOddCh = 0
for key, v in dict.items():
if v % 2 != 0:
countOddCh += 1
if countOddCh > k:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
count = {}
for char in s:
count[char] = count.get(char, 0) + 1
return k >= len([char for char in count if count[char] % 2 == 1]) and k <= len(
s
) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if k > len(s):
return False
if k == len(s):
return True
charCount = Counter(s)
countOdd = 0
for count in list(charCount.values()):
if count % 2 != 0:
countOdd += 1
return countOdd <= k | CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
n = len(s)
if n < k:
return False
if n == k:
return True
ch_to_cnt = collections.defaultdict(int)
for ch in s:
ch_to_cnt[ch] += 1
for cnt in ch_to_cnt.values():
if cnt % 2 == 1:
k -= 1
return k >= 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if k > len(s):
return False
elif k == len(s):
return True
seen = {}
for letter in s:
seen[letter] = seen.get(letter, 0) + 1
singles = 0
for key in seen:
value = seen[key]
if value % 2 == 1:
singles += 1
return singles <= k | CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if len(s) < k:
return False
cnts = collections.defaultdict(int)
for ch in s:
cnts[ch] += 1
odds, evens = [], []
for ch in cnts:
if cnts[ch] % 2 == 1:
odds.append(ch)
else:
evens.append(ch)
return len(odds) <= k
class Solution1:
def canConstruct(self, s: str, k: int) -> bool:
cnts = collections.defaultdict(int)
for ch in s:
cnts[ch] += 1
odds, evens = [], []
for ch in cnts:
if cnts[ch] % 2 == 1:
odds.append(ch)
else:
evens.append(ch)
if len(odds) > k:
return False
for i in range(len(odds)):
k -= 1
ch = odds.pop()
cnts[ch] -= 1
if cnts[ch] > 0:
evens.append(ch)
while k > len(evens):
if not evens:
return False
ch = evens.pop()
k -= 2
cnts[ch] -= 2
if cnts[ch] > 0:
evens.append(ch)
return True | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if len(s) < k:
return False
d = {}
for c in list(s):
if c in d:
d[c] += 1
else:
d[c] = 1
print(d)
if len(list(d.keys())) == k:
return True
else:
odds = 0
for v in list(d.values()):
if v % 2 == 1:
odds += 1
return False if odds > k else True | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR NUMBER NUMBER VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
d = {}
for i in range(len(s)):
if s[i] in d:
d[s[i]] += 1
else:
d[s[i]] = 1
odds = 0
for v in d.values():
if v % 2 == 1:
odds += 1
return odds <= k and k <= len(s) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if len(s) < k:
return False
dic = {}
for sym in s:
if sym in dic.keys():
dic[sym] = 1 - dic[sym]
else:
dic[sym] = 1
num = 0
for i in dic.values():
num += i
if num <= k:
return True
else:
return False | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if k > len(s):
return False
freq = collections.defaultdict(int)
for c in s:
freq[c] += 1
odd = 0
for c in freq:
odd += freq[c] & 1
return odd <= k | CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if k > len(s):
return False
cnt = Counter()
for c in s:
cnt[c] += 1
single_nums = 0
for key in cnt:
if cnt[key] % 2 == 1:
single_nums += 1
if k < single_nums:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
cnt_map = collections.defaultdict(int)
for c in s:
cnt_map[c] += 1
n_odd = 0
for c in cnt_map:
if cnt_map[c] % 2 == 1:
n_odd += 1
if n_odd <= k <= len(s):
return True
else:
return False | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
sCounter = collections.Counter(s)
numberOfLettersWithOddCounts = len(
[count for count in list(sCounter.values()) if count % 2 != 0]
)
return not (len(s) < k or numberOfLettersWithOddCounts > k) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def is_palindrome(self, s):
return bool(s == s[::-1])
def canConstruct(self, s: str, k: int) -> bool:
r_map = dict()
if k > len(s):
return False
for x in s:
if x not in r_map:
r_map[x] = 1
else:
r_map[x] += 1
odd_count = 0
for key in r_map:
if r_map[key] % 2 == 1:
odd_count += 1
return odd_count <= k | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
c = Counter(s)
return sum(c.values()) >= k and len([v for v in c.values() if v % 2]) <= k | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if len(s) < k:
return False
numodds = 0
keys = collections.defaultdict(int)
for c in s:
keys[c] += 1
for key in keys:
if keys[key] % 2:
numodds += 1
return numodds <= k | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR |
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5 | class Solution:
def canConstruct(self, s: str, k: int) -> bool:
odd = 0
counts = defaultdict(lambda: 0)
for c in s:
counts[c] += 1
if counts[c] % 2 == 0:
odd -= 1
else:
odd += 1
if odd > k or k > len(s):
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR |
In the world of Dota2, there are two parties: the Radiant and the Dire.
The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:
Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds.
Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and make the decision about the change in the game.
Given a string representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party respectively. Then if there are n senators, the size of the given string will be n.
The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
Suppose every senator is smart enough and will play the best strategy for his own party, you need to predict which party will finally announce the victory and make the change in the Dota2 game. The output should be Radiant or Dire.
Example 1:
Input: "RD"
Output: "Radiant"
Explanation: The first senator comes from Radiant and he can just ban the next senator's right in the round 1. And the second senator can't exercise any rights any more since his right has been banned. And in the round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
Example 2:
Input: "RDD"
Output: "Dire"
Explanation:
The first senator comes from Radiant and he can just ban the next senator's right in the round 1. And the second senator can't exercise any rights anymore since his right has been banned. And the third senator comes from Dire and he can ban the first senator's right in the round 1. And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
Note:
The length of the given string will in the range [1, 10,000]. | class Solution:
def predictPartyVictory(self, senate):
queue = collections.deque()
people, bans = [0, 0], [0, 0]
for person in senate:
x = person == "R"
people[x] += 1
queue.append(x)
while all(people):
x = queue.popleft()
if bans[x]:
bans[x] -= 1
people[x] -= 1
else:
bans[x ^ 1] += 1
queue.append(x)
return "Radiant" if people[1] else "Dire" | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER STRING STRING |
In the world of Dota2, there are two parties: the Radiant and the Dire.
The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:
Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds.
Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and make the decision about the change in the game.
Given a string representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party respectively. Then if there are n senators, the size of the given string will be n.
The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
Suppose every senator is smart enough and will play the best strategy for his own party, you need to predict which party will finally announce the victory and make the change in the Dota2 game. The output should be Radiant or Dire.
Example 1:
Input: "RD"
Output: "Radiant"
Explanation: The first senator comes from Radiant and he can just ban the next senator's right in the round 1. And the second senator can't exercise any rights any more since his right has been banned. And in the round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
Example 2:
Input: "RDD"
Output: "Dire"
Explanation:
The first senator comes from Radiant and he can just ban the next senator's right in the round 1. And the second senator can't exercise any rights anymore since his right has been banned. And the third senator comes from Dire and he can ban the first senator's right in the round 1. And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
Note:
The length of the given string will in the range [1, 10,000]. | class Solution:
def predictPartyVictory(self, senate):
num = 0
while "R" in senate and "D" in senate:
res = []
for i in senate:
if i == "R":
if num >= 0:
res.append(i)
num += 1
else:
if num <= 0:
res.append(i)
num -= 1
senate = res
return "Radiant" if "R" in senate else "Dire" | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE STRING VAR STRING VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN STRING VAR STRING STRING |
In the world of Dota2, there are two parties: the Radiant and the Dire.
The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:
Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds.
Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and make the decision about the change in the game.
Given a string representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party respectively. Then if there are n senators, the size of the given string will be n.
The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
Suppose every senator is smart enough and will play the best strategy for his own party, you need to predict which party will finally announce the victory and make the change in the Dota2 game. The output should be Radiant or Dire.
Example 1:
Input: "RD"
Output: "Radiant"
Explanation: The first senator comes from Radiant and he can just ban the next senator's right in the round 1. And the second senator can't exercise any rights any more since his right has been banned. And in the round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
Example 2:
Input: "RDD"
Output: "Dire"
Explanation:
The first senator comes from Radiant and he can just ban the next senator's right in the round 1. And the second senator can't exercise any rights anymore since his right has been banned. And the third senator comes from Dire and he can ban the first senator's right in the round 1. And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
Note:
The length of the given string will in the range [1, 10,000]. | class Solution:
def predictPartyVictory(self, senate):
r = collections.deque()
d = collections.deque()
for i, c in enumerate(senate):
if c == "R":
r.append(i)
else:
d.append(i)
return self.eliminateSenators(r, d)
def eliminateSenators(self, r, d):
if len(r) == 0:
return "Dire"
if len(d) == 0:
return "Radiant"
r2 = collections.deque()
d2 = collections.deque()
while len(r) > 0 or len(d) > 0:
if len(r) == 0:
if len(r2) == 0:
return "Dire"
else:
r2.popleft()
d2.append(d.popleft())
elif len(d) == 0:
if len(d2) == 0:
return "Radiant"
else:
d2.popleft()
r2.append(r.popleft())
else:
r_curr = r.popleft()
d_curr = d.popleft()
if r_curr < d_curr:
r2.append(r_curr)
else:
d2.append(d_curr)
return self.eliminateSenators(r2, d2) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN STRING IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
In the world of Dota2, there are two parties: the Radiant and the Dire.
The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:
Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds.
Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and make the decision about the change in the game.
Given a string representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party respectively. Then if there are n senators, the size of the given string will be n.
The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
Suppose every senator is smart enough and will play the best strategy for his own party, you need to predict which party will finally announce the victory and make the change in the Dota2 game. The output should be Radiant or Dire.
Example 1:
Input: "RD"
Output: "Radiant"
Explanation: The first senator comes from Radiant and he can just ban the next senator's right in the round 1. And the second senator can't exercise any rights any more since his right has been banned. And in the round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
Example 2:
Input: "RDD"
Output: "Dire"
Explanation:
The first senator comes from Radiant and he can just ban the next senator's right in the round 1. And the second senator can't exercise any rights anymore since his right has been banned. And the third senator comes from Dire and he can ban the first senator's right in the round 1. And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
Note:
The length of the given string will in the range [1, 10,000]. | class Solution:
def predictPartyVictory(self, senate):
s = [(1 if x == "R" else 0) for x in senate]
Rban = 0
Dban = 0
i = 0
ban = False
while True:
if s[i] == -1:
pass
elif s[i] == 1:
if Dban > 0:
Dban -= 1
s[i] = -1
ban = True
else:
Rban += 1
elif Rban > 0:
Rban -= 1
s[i] = -1
ban = True
else:
Dban += 1
i += 1
if i == len(s):
i = 0
if not ban:
break
else:
ban = False
if Rban > 0:
return "Radiant"
else:
return "Dire" | CLASS_DEF FUNC_DEF ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING |
Given a square grid of size N, each cell of which contains integer cost which represents a cost to traverse through that cell, we need to find a path from top left cell to bottom right cell by which the total cost incurred is minimum.
From the cell (i,j) we can go (i,j-1), (i, j+1), (i-1, j), (i+1, j).
Note: It is assumed that negative cost cycles do not exist in the input matrix.
Example 1:
Input: grid = {{9,4,9,9},{6,7,6,4},
{8,3,3,7},{7,4,9,10}}
Output: 43
Explanation: The grid is-
9 4 9 9
6 7 6 4
8 3 3 7
7 4 9 10
The minimum cost is-
9 + 4 + 7 + 3 + 3 + 7 + 10 = 43.
Example 2:
Input: grid = {{4,4},{3,7}}
Output: 14
Explanation: The grid is-
4 4
3 7
The minimum cost is- 4 + 3 + 7 = 14.
Your Task:
You don't need to read or print anything. Your task is to complete the function minimumCostPath() which takes grid as input parameter and returns the minimum cost to react at bottom right cell from top left cell.
Expected Time Compelxity: O(n^{2}*log(n))
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ n ≤ 500
1 ≤ cost of cells ≤ 1000 | class Solution:
def heapify(self, queue, N, i):
if i > -1:
smallest = i
l = 2 * i + 1
r = 2 * i + 2
if l < N and queue[l][0] < queue[smallest][0]:
smallest = l
if r < N and queue[r][0] < queue[smallest][0]:
smallest = r
if smallest != i:
queue[smallest], queue[i] = queue[i], queue[smallest]
self.heapify(queue, N, smallest)
def heapify_min(self, queue, N, i):
if i > -1:
smallest = i
l = 2 * i + 1
r = 2 * i + 2
if l < N and queue[l][0] < queue[smallest][0]:
smallest = l
if r < N and queue[r][0] < queue[smallest][0]:
smallest = r
if smallest != i:
queue[smallest], queue[i] = queue[i], queue[smallest]
self.heapify_min(queue, N, (i - 1) // 2)
def valid_movement(self, d, x, y):
return d[0] > -1 and d[1] > -1 and d[0] < x and d[1] < y
def minimumCostPath(self, grid):
queue = [(0) for i in range(len(grid[0]) * len(grid))]
queue[0] = grid[0][0], 0, 0
arr_size = 1
x, y = len(grid), len(grid[0])
d = {(0, 0): True}
while True:
a = queue[0]
queue[0] = queue[arr_size - 1]
arr_size -= 1
if arr_size:
self.heapify(queue, arr_size, 0)
i, j = a[1], a[2]
actions = [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]
for act in actions:
if not self.valid_movement(act, x, y):
continue
if (act[0], act[1]) in d:
continue
d[act[0], act[1]] = True
if act == (x - 1, y - 1):
return a[0] + grid[-1][-1]
queue[arr_size] = a[0] + grid[act[0]][act[1]], act[0], act[1]
arr_size += 1
self.heapify_min(queue, arr_size, (arr_size - 2) // 2) | CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER WHILE NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER |
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a_1, a_2, …, a_{2k-1}, is the array b with elements b_1, b_2, …, b_{k} such that b_i is equal to the median of a_1, a_2, …, a_{2i-1} for all i. Omkar has found an array b of size n (1 ≤ n ≤ 2 ⋅ 10^5, -10^9 ≤ b_i ≤ 10^9). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?
The median of a set of numbers a_1, a_2, …, a_{2i-1} is the number c_{i} where c_{1}, c_{2}, …, c_{2i-1} represents a_1, a_2, …, a_{2i-1} sorted in nondecreasing order.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array b.
The second line contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9) — the elements of b.
It is guaranteed the sum of n across all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output one line containing YES if there exists an array a such that b_i is the median of a_1, a_2, ..., a_{2i-1} for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).
Examples
Input
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3
Output
NO
YES
NO
YES
YES
Input
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
Output
NO
YES
NO
NO
NO
Note
In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.
In the fourth case of the first sample, the array [3, 2, 5] will generate an OmkArray of [3, 3], as the median of 3 is 3 and the median of 2, 3, 5 is 3.
In the fifth case of the first sample, the array [2, 1, 0, 3, 4, 4, 3] will generate an OmkArray of [2, 1, 2, 3] as
* the median of 2 is 2
* the median of 0, 1, 2 is 1
* the median of 0, 1, 2, 3, 4 is 2
* and the median of 0, 1, 2, 3, 3, 4, 4 is 3.
In the second case of the second sample, the array [1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5] will generate an OmkArray of [1, 1, 3, 1, 0, -2, -1], as
* the median of 1 is 1
* the median of 0, 1, 4 is 1
* the median of 0, 1, 3, 4, 5 is 3
* the median of -2, -2, 0, 1, 3, 4, 5 is 1
* the median of -4, -2, -2, -2, 0, 1, 3, 4, 5 is 0
* the median of -4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5 is -2
* and the median of -4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5 is -1
For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a. | for _ in range(int(input())):
n = int(input())
b = list(map(int, input().split())) + [10**10, -(10**10)]
prv = [-1] * (n + 2)
nxt = [-2] * (n + 2)
nxt[-1] = 0
prv[-2] = 0
for i in range(1, n):
if b[i] > b[i - 1]:
if b[i] > b[nxt[i - 1]]:
print("NO")
break
if b[i] != b[nxt[i - 1]]:
prv[nxt[i - 1]] = i
nxt[i] = nxt[i - 1]
prv[i] = i - 1
nxt[i - 1] = i
else:
nxt[i] = nxt[nxt[i - 1]]
prv[i] = prv[nxt[i - 1]]
elif b[i] < b[i - 1]:
if b[i] < b[prv[i - 1]]:
print("NO")
break
if b[i] != b[prv[i - 1]]:
nxt[prv[i - 1]] = i
prv[i] = prv[i - 1]
nxt[i] = i - 1
prv[i - 1] = i
else:
nxt[i] = nxt[prv[i - 1]]
prv[i] = prv[prv[i - 1]]
else:
nxt[i] = nxt[i - 1]
prv[i] = prv[i - 1]
else:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING |
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a_1, a_2, …, a_{2k-1}, is the array b with elements b_1, b_2, …, b_{k} such that b_i is equal to the median of a_1, a_2, …, a_{2i-1} for all i. Omkar has found an array b of size n (1 ≤ n ≤ 2 ⋅ 10^5, -10^9 ≤ b_i ≤ 10^9). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?
The median of a set of numbers a_1, a_2, …, a_{2i-1} is the number c_{i} where c_{1}, c_{2}, …, c_{2i-1} represents a_1, a_2, …, a_{2i-1} sorted in nondecreasing order.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array b.
The second line contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9) — the elements of b.
It is guaranteed the sum of n across all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output one line containing YES if there exists an array a such that b_i is the median of a_1, a_2, ..., a_{2i-1} for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).
Examples
Input
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3
Output
NO
YES
NO
YES
YES
Input
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
Output
NO
YES
NO
NO
NO
Note
In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.
In the fourth case of the first sample, the array [3, 2, 5] will generate an OmkArray of [3, 3], as the median of 3 is 3 and the median of 2, 3, 5 is 3.
In the fifth case of the first sample, the array [2, 1, 0, 3, 4, 4, 3] will generate an OmkArray of [2, 1, 2, 3] as
* the median of 2 is 2
* the median of 0, 1, 2 is 1
* the median of 0, 1, 2, 3, 4 is 2
* and the median of 0, 1, 2, 3, 3, 4, 4 is 3.
In the second case of the second sample, the array [1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5] will generate an OmkArray of [1, 1, 3, 1, 0, -2, -1], as
* the median of 1 is 1
* the median of 0, 1, 4 is 1
* the median of 0, 1, 3, 4, 5 is 3
* the median of -2, -2, 0, 1, 3, 4, 5 is 1
* the median of -4, -2, -2, -2, 0, 1, 3, 4, 5 is 0
* the median of -4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5 is -2
* and the median of -4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5 is -1
For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a. | import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LI1():
return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def LLI1(rows_number):
return [LI1() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = 10**16
md = 998244353
class BitSum:
def __init__(self, n):
self.n = n + 1
self.table = [0] * self.n
def add(self, i, x):
i += 1
while i < self.n:
self.table[i] += x
i += i & -i
def sum(self, i):
i += 1
res = 0
while i > 0:
res += self.table[i]
i -= i & -i
return res
def sumlr(self, l, r):
if l >= r:
return 0
if l == 0:
return self.sum(r - 1)
return self.sum(r - 1) - self.sum(l - 1)
def rank(self, x):
idx = 0
for lv in range((self.n - 1).bit_length() - 1, -1, -1):
mid = idx + (1 << lv)
if mid >= self.n:
continue
if self.table[mid] < x:
x -= self.table[mid]
idx += 1 << lv
return idx
def ok():
now = 0
for i, a in enumerate(aa):
if a < now and bit.sumlr(a + 1, now):
return False
if now < a and bit.sumlr(now + 1, a):
return False
bit.add(a, 1)
now = a
return True
for testcase in range(II()):
n = II()
aa = LI()
enc = {a: (i + 1) for i, a in enumerate(sorted(set(aa)))}
aa = [enc[a] for a in aa]
bit = BitSum(len(enc) + 5)
print("YES" if ok() else "NO") | IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING STRING |
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a_1, a_2, …, a_{2k-1}, is the array b with elements b_1, b_2, …, b_{k} such that b_i is equal to the median of a_1, a_2, …, a_{2i-1} for all i. Omkar has found an array b of size n (1 ≤ n ≤ 2 ⋅ 10^5, -10^9 ≤ b_i ≤ 10^9). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?
The median of a set of numbers a_1, a_2, …, a_{2i-1} is the number c_{i} where c_{1}, c_{2}, …, c_{2i-1} represents a_1, a_2, …, a_{2i-1} sorted in nondecreasing order.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array b.
The second line contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9) — the elements of b.
It is guaranteed the sum of n across all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output one line containing YES if there exists an array a such that b_i is the median of a_1, a_2, ..., a_{2i-1} for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).
Examples
Input
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3
Output
NO
YES
NO
YES
YES
Input
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
Output
NO
YES
NO
NO
NO
Note
In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.
In the fourth case of the first sample, the array [3, 2, 5] will generate an OmkArray of [3, 3], as the median of 3 is 3 and the median of 2, 3, 5 is 3.
In the fifth case of the first sample, the array [2, 1, 0, 3, 4, 4, 3] will generate an OmkArray of [2, 1, 2, 3] as
* the median of 2 is 2
* the median of 0, 1, 2 is 1
* the median of 0, 1, 2, 3, 4 is 2
* and the median of 0, 1, 2, 3, 3, 4, 4 is 3.
In the second case of the second sample, the array [1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5] will generate an OmkArray of [1, 1, 3, 1, 0, -2, -1], as
* the median of 1 is 1
* the median of 0, 1, 4 is 1
* the median of 0, 1, 3, 4, 5 is 3
* the median of -2, -2, 0, 1, 3, 4, 5 is 1
* the median of -4, -2, -2, -2, 0, 1, 3, 4, 5 is 0
* the median of -4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5 is -2
* and the median of -4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5 is -1
For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a. | for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
left = [-(10**18)]
right = [10**18]
s = l[0]
ans = "YES"
for i in l[1:]:
if i > s:
if i > right[-1]:
ans = "NO"
break
left.append(s)
s = i
if i == right[-1]:
right.pop()
elif i < s:
if i < left[-1]:
ans = "NO"
break
right.append(s)
s = i
if i == left[-1]:
left.pop()
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR NUMBER IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a_1, a_2, …, a_{2k-1}, is the array b with elements b_1, b_2, …, b_{k} such that b_i is equal to the median of a_1, a_2, …, a_{2i-1} for all i. Omkar has found an array b of size n (1 ≤ n ≤ 2 ⋅ 10^5, -10^9 ≤ b_i ≤ 10^9). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?
The median of a set of numbers a_1, a_2, …, a_{2i-1} is the number c_{i} where c_{1}, c_{2}, …, c_{2i-1} represents a_1, a_2, …, a_{2i-1} sorted in nondecreasing order.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array b.
The second line contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9) — the elements of b.
It is guaranteed the sum of n across all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output one line containing YES if there exists an array a such that b_i is the median of a_1, a_2, ..., a_{2i-1} for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).
Examples
Input
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3
Output
NO
YES
NO
YES
YES
Input
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
Output
NO
YES
NO
NO
NO
Note
In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.
In the fourth case of the first sample, the array [3, 2, 5] will generate an OmkArray of [3, 3], as the median of 3 is 3 and the median of 2, 3, 5 is 3.
In the fifth case of the first sample, the array [2, 1, 0, 3, 4, 4, 3] will generate an OmkArray of [2, 1, 2, 3] as
* the median of 2 is 2
* the median of 0, 1, 2 is 1
* the median of 0, 1, 2, 3, 4 is 2
* and the median of 0, 1, 2, 3, 3, 4, 4 is 3.
In the second case of the second sample, the array [1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5] will generate an OmkArray of [1, 1, 3, 1, 0, -2, -1], as
* the median of 1 is 1
* the median of 0, 1, 4 is 1
* the median of 0, 1, 3, 4, 5 is 3
* the median of -2, -2, 0, 1, 3, 4, 5 is 1
* the median of -4, -2, -2, -2, 0, 1, 3, 4, 5 is 0
* the median of -4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5 is -2
* and the median of -4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5 is -1
For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a. | from itertools import combinations
from sys import stdin
def getInt():
return int(stdin.readline().strip())
def getInts():
return tuple(int(z) for z in stdin.readline().split())
class BinaryIndexedTree:
def __init__(self, N):
self.N = N
self.seq = [0] * (N + 1)
def incr(self, i, j):
i += 1
while i <= self.N:
self.seq[i] += j
i += i & -i
def accumulate(self, k):
ok = k
k += 1
tot = 0
while k > 0:
tot += self.seq[k]
k -= k & -k
return tot
n = getInt()
for i in range(n):
k = getInt()
bs = getInts()
w = sorted(list(set(bs)))
h = {w[i]: i for i in range(len(w))}
bs = [h[p] for p in bs]
T = BinaryIndexedTree(len(w))
s = set([])
ok = True
prev = None
for b in bs:
if b not in s:
s.add(b)
T.incr(b, 1)
if prev is not None:
if abs(T.accumulate(b) - T.accumulate(prev)) > 1:
ok = False
break
prev = b
print("YES" if ok else "NO") | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NONE IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING |
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a_1, a_2, …, a_{2k-1}, is the array b with elements b_1, b_2, …, b_{k} such that b_i is equal to the median of a_1, a_2, …, a_{2i-1} for all i. Omkar has found an array b of size n (1 ≤ n ≤ 2 ⋅ 10^5, -10^9 ≤ b_i ≤ 10^9). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?
The median of a set of numbers a_1, a_2, …, a_{2i-1} is the number c_{i} where c_{1}, c_{2}, …, c_{2i-1} represents a_1, a_2, …, a_{2i-1} sorted in nondecreasing order.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array b.
The second line contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9) — the elements of b.
It is guaranteed the sum of n across all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output one line containing YES if there exists an array a such that b_i is the median of a_1, a_2, ..., a_{2i-1} for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).
Examples
Input
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3
Output
NO
YES
NO
YES
YES
Input
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
Output
NO
YES
NO
NO
NO
Note
In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.
In the fourth case of the first sample, the array [3, 2, 5] will generate an OmkArray of [3, 3], as the median of 3 is 3 and the median of 2, 3, 5 is 3.
In the fifth case of the first sample, the array [2, 1, 0, 3, 4, 4, 3] will generate an OmkArray of [2, 1, 2, 3] as
* the median of 2 is 2
* the median of 0, 1, 2 is 1
* the median of 0, 1, 2, 3, 4 is 2
* and the median of 0, 1, 2, 3, 3, 4, 4 is 3.
In the second case of the second sample, the array [1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5] will generate an OmkArray of [1, 1, 3, 1, 0, -2, -1], as
* the median of 1 is 1
* the median of 0, 1, 4 is 1
* the median of 0, 1, 3, 4, 5 is 3
* the median of -2, -2, 0, 1, 3, 4, 5 is 1
* the median of -4, -2, -2, -2, 0, 1, 3, 4, 5 is 0
* the median of -4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5 is -2
* and the median of -4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5 is -1
For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a. | INF = 1000000000.0 + 1
for ii in range(int(input())):
n = int(input())
b = list(map(int, input().split()))
m = b[0]
l = [-INF]
r = [INF]
ans = 1
for i in range(1, len(b)):
if m > b[i]:
if b[i] > l[-1]:
r += [m]
elif b[i] < l[-1]:
ans = 0
break
else:
r += [m]
l.pop()
elif m < b[i]:
if b[i] < r[-1]:
l += [m]
elif b[i] > r[-1]:
ans = 0
break
else:
l += [m]
r.pop()
m = b[i]
if ans:
print("YES")
else:
print("NO") | ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER VAR LIST VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR LIST VAR EXPR FUNC_CALL VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER VAR LIST VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR LIST VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a_1, a_2, …, a_{2k-1}, is the array b with elements b_1, b_2, …, b_{k} such that b_i is equal to the median of a_1, a_2, …, a_{2i-1} for all i. Omkar has found an array b of size n (1 ≤ n ≤ 2 ⋅ 10^5, -10^9 ≤ b_i ≤ 10^9). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?
The median of a set of numbers a_1, a_2, …, a_{2i-1} is the number c_{i} where c_{1}, c_{2}, …, c_{2i-1} represents a_1, a_2, …, a_{2i-1} sorted in nondecreasing order.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array b.
The second line contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9) — the elements of b.
It is guaranteed the sum of n across all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output one line containing YES if there exists an array a such that b_i is the median of a_1, a_2, ..., a_{2i-1} for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).
Examples
Input
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3
Output
NO
YES
NO
YES
YES
Input
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
Output
NO
YES
NO
NO
NO
Note
In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.
In the fourth case of the first sample, the array [3, 2, 5] will generate an OmkArray of [3, 3], as the median of 3 is 3 and the median of 2, 3, 5 is 3.
In the fifth case of the first sample, the array [2, 1, 0, 3, 4, 4, 3] will generate an OmkArray of [2, 1, 2, 3] as
* the median of 2 is 2
* the median of 0, 1, 2 is 1
* the median of 0, 1, 2, 3, 4 is 2
* and the median of 0, 1, 2, 3, 3, 4, 4 is 3.
In the second case of the second sample, the array [1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5] will generate an OmkArray of [1, 1, 3, 1, 0, -2, -1], as
* the median of 1 is 1
* the median of 0, 1, 4 is 1
* the median of 0, 1, 3, 4, 5 is 3
* the median of -2, -2, 0, 1, 3, 4, 5 is 1
* the median of -4, -2, -2, -2, 0, 1, 3, 4, 5 is 0
* the median of -4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5 is -2
* and the median of -4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5 is -1
For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a. | for _ in range(int(input())):
n = int(input())
b = list(map(int, input().split()))
next = dict()
prev = dict()
curr = b[0]
next[curr] = "!"
prev[curr] = "!"
fine = True
for i in b:
if i > curr:
if next[curr] == "!":
next[curr] = i
prev[i] = curr
next[i] = "!"
elif next[curr] < i:
fine = False
break
elif next[curr] > i:
prev[next[curr]] = i
next[i] = next[curr]
next[curr] = i
prev[i] = curr
elif i < curr:
if prev[curr] == "!":
prev[curr] = i
prev[i] = "!"
next[i] = curr
elif i < prev[curr]:
fine = False
break
elif i > prev[curr]:
next[prev[curr]] = i
prev[i] = prev[curr]
prev[curr] = i
next[i] = curr
curr = i
print("YES") if fine else print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR STRING IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING |
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a_1, a_2, …, a_{2k-1}, is the array b with elements b_1, b_2, …, b_{k} such that b_i is equal to the median of a_1, a_2, …, a_{2i-1} for all i. Omkar has found an array b of size n (1 ≤ n ≤ 2 ⋅ 10^5, -10^9 ≤ b_i ≤ 10^9). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?
The median of a set of numbers a_1, a_2, …, a_{2i-1} is the number c_{i} where c_{1}, c_{2}, …, c_{2i-1} represents a_1, a_2, …, a_{2i-1} sorted in nondecreasing order.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array b.
The second line contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9) — the elements of b.
It is guaranteed the sum of n across all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output one line containing YES if there exists an array a such that b_i is the median of a_1, a_2, ..., a_{2i-1} for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).
Examples
Input
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3
Output
NO
YES
NO
YES
YES
Input
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
Output
NO
YES
NO
NO
NO
Note
In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.
In the fourth case of the first sample, the array [3, 2, 5] will generate an OmkArray of [3, 3], as the median of 3 is 3 and the median of 2, 3, 5 is 3.
In the fifth case of the first sample, the array [2, 1, 0, 3, 4, 4, 3] will generate an OmkArray of [2, 1, 2, 3] as
* the median of 2 is 2
* the median of 0, 1, 2 is 1
* the median of 0, 1, 2, 3, 4 is 2
* and the median of 0, 1, 2, 3, 3, 4, 4 is 3.
In the second case of the second sample, the array [1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5] will generate an OmkArray of [1, 1, 3, 1, 0, -2, -1], as
* the median of 1 is 1
* the median of 0, 1, 4 is 1
* the median of 0, 1, 3, 4, 5 is 3
* the median of -2, -2, 0, 1, 3, 4, 5 is 1
* the median of -4, -2, -2, -2, 0, 1, 3, 4, 5 is 0
* the median of -4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5 is -2
* and the median of -4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5 is -1
For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a. | for stu in [*open(0)][2::2]:
mx, *a = map(int, stu.split())
lt = [-1000000000.0]
r = [1000000000.0]
o = 0
for x in a:
if x > mx:
if x > r[-1]:
o = 1
break
lt += (mx,)
mx = x
if x == r[-1]:
r.pop()
elif x < mx:
if x < lt[-1]:
o = 1
break
r += (mx,)
mx = x
if x == lt[-1]:
lt.pop()
print("yNeOs"[o::2]) | FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING VAR NUMBER |
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a_1, a_2, …, a_{2k-1}, is the array b with elements b_1, b_2, …, b_{k} such that b_i is equal to the median of a_1, a_2, …, a_{2i-1} for all i. Omkar has found an array b of size n (1 ≤ n ≤ 2 ⋅ 10^5, -10^9 ≤ b_i ≤ 10^9). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?
The median of a set of numbers a_1, a_2, …, a_{2i-1} is the number c_{i} where c_{1}, c_{2}, …, c_{2i-1} represents a_1, a_2, …, a_{2i-1} sorted in nondecreasing order.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array b.
The second line contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9) — the elements of b.
It is guaranteed the sum of n across all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output one line containing YES if there exists an array a such that b_i is the median of a_1, a_2, ..., a_{2i-1} for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).
Examples
Input
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3
Output
NO
YES
NO
YES
YES
Input
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
Output
NO
YES
NO
NO
NO
Note
In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.
In the fourth case of the first sample, the array [3, 2, 5] will generate an OmkArray of [3, 3], as the median of 3 is 3 and the median of 2, 3, 5 is 3.
In the fifth case of the first sample, the array [2, 1, 0, 3, 4, 4, 3] will generate an OmkArray of [2, 1, 2, 3] as
* the median of 2 is 2
* the median of 0, 1, 2 is 1
* the median of 0, 1, 2, 3, 4 is 2
* and the median of 0, 1, 2, 3, 3, 4, 4 is 3.
In the second case of the second sample, the array [1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5] will generate an OmkArray of [1, 1, 3, 1, 0, -2, -1], as
* the median of 1 is 1
* the median of 0, 1, 4 is 1
* the median of 0, 1, 3, 4, 5 is 3
* the median of -2, -2, 0, 1, 3, 4, 5 is 1
* the median of -4, -2, -2, -2, 0, 1, 3, 4, 5 is 0
* the median of -4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5 is -2
* and the median of -4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5 is -1
For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a. | import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
n = int(input())
A = list(map(int, input().split()))
compression_dict = {a: ind for ind, a in enumerate(sorted(set(A)))}
A = [(compression_dict[a] + 3) for a in A]
LEN = n + 6
BIT = [0] * (LEN + 1)
USE = [0] * (LEN + 1)
def update(v, w):
while v <= LEN:
BIT[v] += w
v += v & -v
def getvalue(v):
ANS = 0
while v != 0:
ANS += BIT[v]
v -= v & -v
return ANS
def bisect_on_BIT(x):
if x <= 0:
return 0
ANS = 0
h = 1 << LEN.bit_length() - 1
while h > 0:
if ANS + h <= LEN and BIT[ANS + h] < x:
x -= BIT[ANS + h]
ANS += h
h //= 2
return ANS + 1
USE[1] = 1
USE[A[0]] = 1
USE[n + 5] = 1
update(1, 1)
update(A[0], 1)
update(n + 5, 1)
before = A[0]
for a in A[1:]:
if a == before:
continue
elif a > before:
x = getvalue(before)
u = bisect_on_BIT(x + 1)
if a <= u:
if USE[a] == 0:
USE[a] = 1
update(a, 1)
else:
print("NO")
break
else:
x = getvalue(before)
u = bisect_on_BIT(x - 1)
if a >= u:
if USE[a] == 0:
USE[a] = 1
update(a, 1)
else:
print("NO")
break
before = a
else:
print("YES") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING |
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a_1, a_2, …, a_{2k-1}, is the array b with elements b_1, b_2, …, b_{k} such that b_i is equal to the median of a_1, a_2, …, a_{2i-1} for all i. Omkar has found an array b of size n (1 ≤ n ≤ 2 ⋅ 10^5, -10^9 ≤ b_i ≤ 10^9). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?
The median of a set of numbers a_1, a_2, …, a_{2i-1} is the number c_{i} where c_{1}, c_{2}, …, c_{2i-1} represents a_1, a_2, …, a_{2i-1} sorted in nondecreasing order.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array b.
The second line contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9) — the elements of b.
It is guaranteed the sum of n across all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output one line containing YES if there exists an array a such that b_i is the median of a_1, a_2, ..., a_{2i-1} for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).
Examples
Input
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3
Output
NO
YES
NO
YES
YES
Input
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
Output
NO
YES
NO
NO
NO
Note
In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.
In the fourth case of the first sample, the array [3, 2, 5] will generate an OmkArray of [3, 3], as the median of 3 is 3 and the median of 2, 3, 5 is 3.
In the fifth case of the first sample, the array [2, 1, 0, 3, 4, 4, 3] will generate an OmkArray of [2, 1, 2, 3] as
* the median of 2 is 2
* the median of 0, 1, 2 is 1
* the median of 0, 1, 2, 3, 4 is 2
* and the median of 0, 1, 2, 3, 3, 4, 4 is 3.
In the second case of the second sample, the array [1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5] will generate an OmkArray of [1, 1, 3, 1, 0, -2, -1], as
* the median of 1 is 1
* the median of 0, 1, 4 is 1
* the median of 0, 1, 3, 4, 5 is 3
* the median of -2, -2, 0, 1, 3, 4, 5 is 1
* the median of -4, -2, -2, -2, 0, 1, 3, 4, 5 is 0
* the median of -4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5 is -2
* and the median of -4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5 is -1
For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a. | def solve(n, a):
prev = a[0]
r = [10**9]
l = [-r[0]]
for i in range(1, n):
if a[i] < prev:
if a[i] < l[-1]:
return False
elif a[i] == l[-1]:
l.pop()
r.append(prev)
elif a[i] > prev:
if a[i] > r[-1]:
return False
elif a[i] == r[-1]:
r.pop()
l.append(prev)
prev = a[i]
return True
def main():
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
if solve(n, a):
print("YES")
else:
print("NO")
main() | FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a_1, a_2, …, a_{2k-1}, is the array b with elements b_1, b_2, …, b_{k} such that b_i is equal to the median of a_1, a_2, …, a_{2i-1} for all i. Omkar has found an array b of size n (1 ≤ n ≤ 2 ⋅ 10^5, -10^9 ≤ b_i ≤ 10^9). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?
The median of a set of numbers a_1, a_2, …, a_{2i-1} is the number c_{i} where c_{1}, c_{2}, …, c_{2i-1} represents a_1, a_2, …, a_{2i-1} sorted in nondecreasing order.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array b.
The second line contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9) — the elements of b.
It is guaranteed the sum of n across all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output one line containing YES if there exists an array a such that b_i is the median of a_1, a_2, ..., a_{2i-1} for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).
Examples
Input
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3
Output
NO
YES
NO
YES
YES
Input
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
Output
NO
YES
NO
NO
NO
Note
In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.
In the fourth case of the first sample, the array [3, 2, 5] will generate an OmkArray of [3, 3], as the median of 3 is 3 and the median of 2, 3, 5 is 3.
In the fifth case of the first sample, the array [2, 1, 0, 3, 4, 4, 3] will generate an OmkArray of [2, 1, 2, 3] as
* the median of 2 is 2
* the median of 0, 1, 2 is 1
* the median of 0, 1, 2, 3, 4 is 2
* and the median of 0, 1, 2, 3, 3, 4, 4 is 3.
In the second case of the second sample, the array [1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5] will generate an OmkArray of [1, 1, 3, 1, 0, -2, -1], as
* the median of 1 is 1
* the median of 0, 1, 4 is 1
* the median of 0, 1, 3, 4, 5 is 3
* the median of -2, -2, 0, 1, 3, 4, 5 is 1
* the median of -4, -2, -2, -2, 0, 1, 3, 4, 5 is 0
* the median of -4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5 is -2
* and the median of -4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5 is -1
For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a. | class node:
def __init__(self, v):
self.value = v
self.left = None
self.right = None
def process(B):
n = len(B)
pointer = node(B[0])
for i in range(1, n):
if B[i] > B[i - 1]:
if pointer.right is None:
x = node(B[i])
pointer.right, x.left = x, pointer
elif pointer.right.value > B[i]:
x = node(B[i])
y = pointer.right
pointer.right, y.left, x.left, x.right = x, x, pointer, y
elif pointer.right.value != B[i]:
return "NO"
pointer = pointer.right
elif B[i] < B[i - 1]:
if pointer.left is None:
x = node(B[i])
pointer.left, x.right = x, pointer
elif pointer.left.value < B[i]:
x = node(B[i])
y = pointer.left
pointer.left, y.right, x.left, x.right = x, x, y, pointer
elif pointer.left.value != B[i]:
return "NO"
pointer = pointer.left
if pointer.left is None:
L = -1 * float("inf")
else:
L = pointer.left.value
if pointer.right is None:
R = float("inf")
else:
R = pointer.right.value
return "YES"
t = int(input())
for i in range(t):
n = int(input())
A = [int(x) for x in input().split()]
print(process(A)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR RETURN STRING ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR RETURN STRING ASSIGN VAR VAR IF VAR NONE ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a_1, a_2, …, a_{2k-1}, is the array b with elements b_1, b_2, …, b_{k} such that b_i is equal to the median of a_1, a_2, …, a_{2i-1} for all i. Omkar has found an array b of size n (1 ≤ n ≤ 2 ⋅ 10^5, -10^9 ≤ b_i ≤ 10^9). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?
The median of a set of numbers a_1, a_2, …, a_{2i-1} is the number c_{i} where c_{1}, c_{2}, …, c_{2i-1} represents a_1, a_2, …, a_{2i-1} sorted in nondecreasing order.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array b.
The second line contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9) — the elements of b.
It is guaranteed the sum of n across all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output one line containing YES if there exists an array a such that b_i is the median of a_1, a_2, ..., a_{2i-1} for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).
Examples
Input
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3
Output
NO
YES
NO
YES
YES
Input
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0
Output
NO
YES
NO
NO
NO
Note
In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.
In the fourth case of the first sample, the array [3, 2, 5] will generate an OmkArray of [3, 3], as the median of 3 is 3 and the median of 2, 3, 5 is 3.
In the fifth case of the first sample, the array [2, 1, 0, 3, 4, 4, 3] will generate an OmkArray of [2, 1, 2, 3] as
* the median of 2 is 2
* the median of 0, 1, 2 is 1
* the median of 0, 1, 2, 3, 4 is 2
* and the median of 0, 1, 2, 3, 3, 4, 4 is 3.
In the second case of the second sample, the array [1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5] will generate an OmkArray of [1, 1, 3, 1, 0, -2, -1], as
* the median of 1 is 1
* the median of 0, 1, 4 is 1
* the median of 0, 1, 3, 4, 5 is 3
* the median of -2, -2, 0, 1, 3, 4, 5 is 1
* the median of -4, -2, -2, -2, 0, 1, 3, 4, 5 is 0
* the median of -4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5 is -2
* and the median of -4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5 is -1
For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a. | for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
le = [-1000000000.0]
ri = [1000000000.0]
m = l[0]
ok = False
for x in l[1:]:
if x == m:
continue
if x > m:
if x > ri[-1]:
print("no")
ok = True
break
le.append(m)
m = x
if x == ri[-1]:
ri.pop()
else:
if x < le[-1]:
print("no")
ok = True
break
ri.append(m)
m = x
if x == le[-1]:
le.pop()
if not ok:
print("yes") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING |
You are given an integer array nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all 0 <= i < nums.length-1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
1 <= nums.length <= 3*10^4
-10^5 <= nums[i] <= 10^5 | class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
totol, res, min2, max2 = 0, 0, float("inf"), -float("inf")
for a, b in zip(nums, nums[1:]):
totol += abs(a - b)
res = max(res, abs(nums[0] - b) - abs(a - b))
res = max(res, abs(nums[-1] - a) - abs(a - b))
min2, max2 = min(min2, max(a, b)), max(max2, min(a, b))
return totol + max(res, (max2 - min2) * 2) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR |
You are given an integer array nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all 0 <= i < nums.length-1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
1 <= nums.length <= 3*10^4
-10^5 <= nums[i] <= 10^5 | class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
n = len(nums)
base = sum([abs(nums[i] - nums[i + 1]) for i in range(n - 1)])
if n <= 2:
return base
inds = sorted(list(range(n)), key=lambda x: nums[x])
return base + max(options(inds, nums))
def switch(nums, i, j, base=0):
i_inc = abs(nums[j] - nums[i - 1]) - abs(nums[i] - nums[i - 1]) if i > 0 else 0
j_inc = (
abs(nums[j + 1] - nums[i]) - abs(nums[j + 1] - nums[j])
if j < len(nums) - 1
else 0
)
return base + i_inc + j_inc
def options(inds, nums):
a, b = findRange(inds)
d, c = findRange(inds[::-1])
yield 0
yield 2 * (nums[c] - nums[b])
i = max(a, b)
j = max(c, d)
n = len(nums)
yield switch(nums, i, n - 1)
yield switch(nums, j, n - 1)
yield switch(nums, 0, i - 1)
yield switch(nums, 0, j - 1)
def findRange(inds):
seen = set()
for i, idx in enumerate(inds):
if idx + 1 in seen or idx - 1 in seen:
return (idx + 1, idx) if idx + 1 in seen else (idx - 1, idx)
seen.add(idx) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER EXPR NUMBER EXPR BIN_OP NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are given an integer array nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all 0 <= i < nums.length-1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
1 <= nums.length <= 3*10^4
-10^5 <= nums[i] <= 10^5 | class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
numsLen = len(nums)
oldSum = sum([abs(nums[i] - nums[i + 1]) for i in range(numsLen - 1)])
if numsLen < 3:
return oldSum
delta = 0
for i in range(1, numsLen - 1):
delta = max(
delta,
abs(nums[i + 1] - nums[0]) - abs(nums[i + 1] - nums[i]),
abs(nums[-1] - nums[i - 1]) - abs(nums[i] - nums[i - 1]),
)
high = float("-inf")
low = float("inf")
for x, y in zip(nums, nums[1:]):
high = max(high, min(x, y))
low = min(low, max(x, y))
return oldSum + max(delta, (high - low) * 2) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR |
You are given an integer array nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all 0 <= i < nums.length-1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
1 <= nums.length <= 3*10^4
-10^5 <= nums[i] <= 10^5 | class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
vals = [0] * (len(nums) - 1)
for i, n in enumerate(nums[1:]):
vals[i] = abs(n - nums[i])
base = sum(vals)
bonus = 0
MPP = max(nums[L] + nums[L + 1] - vals[L] for L in range(len(nums) - 1))
MPN = max(nums[L] - nums[L + 1] - vals[L] for L in range(len(nums) - 1))
MNP = max(-nums[L] + nums[L + 1] - vals[L] for L in range(len(nums) - 1))
MNN = max(-nums[L] - nums[L + 1] - vals[L] for L in range(len(nums) - 1))
bonus = max(MPP + MNN, MPN + MNP)
for R in range(0, len(nums) - 1):
bonus = max(bonus, abs(nums[0] - nums[R + 1]) - vals[R])
for L in range(1, len(nums) - 1):
bonus = max(bonus, abs(nums[L - 1] - nums[len(nums) - 1]) - vals[L - 1])
return base + bonus | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR |
You are given an integer array nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all 0 <= i < nums.length-1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
1 <= nums.length <= 3*10^4
-10^5 <= nums[i] <= 10^5 | class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
n = len(nums)
s = 0
for i in range(len(nums) - 1):
s += abs(nums[i] - nums[i + 1])
if n <= 2:
return s
maxup = inc = 0
minmax = float("inf")
maxmin = -float("inf")
for left in range(1, n):
dis = abs(nums[left] - nums[left - 1])
minmax = min(minmax, max(nums[left], nums[left - 1]))
maxmin = max(maxmin, min(nums[left], nums[left - 1]))
inc = max(inc, abs(nums[0] - nums[left]) - dis)
inc = max(inc, abs(nums[-1] - nums[left - 1]) - dis)
return s + max(inc, 2 * (maxmin - minmax)) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR |
You are given an integer array nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all 0 <= i < nums.length-1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
1 <= nums.length <= 3*10^4
-10^5 <= nums[i] <= 10^5 | class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return 0
ans = 0
for i in range(1, n - 1):
ans = max(
ans,
abs(nums[0] - nums[i + 1]) - abs(nums[i] - nums[i + 1]),
abs(nums[-1] - nums[i - 1]) - abs(nums[i] - nums[i - 1]),
)
small, large = (-1, float("inf")), (-1, -float("inf"))
for i in range(n):
if i >= 1:
if nums[i] >= nums[i - 1]:
if small[1] > nums[i]:
small = i, nums[i]
if nums[i] <= nums[i - 1]:
if large[1] < nums[i]:
large = i, nums[i]
if i < n - 1:
if nums[i] >= nums[i + 1]:
if small[1] > nums[i]:
small = i, nums[i]
if nums[i] <= nums[i + 1]:
if large[1] < nums[i]:
large = i, nums[i]
return sum([abs(nums[i] - nums[i + 1]) for i in range(n - 1)]) + max(
2 * (large[1] - small[1]), 0, ans
) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR |
You are given an integer array nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all 0 <= i < nums.length-1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
1 <= nums.length <= 3*10^4
-10^5 <= nums[i] <= 10^5 | class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
n = len(nums)
s = 0
gain = 0
h = float("-inf")
l = float("inf")
for i in range(n - 1):
n1 = nums[i]
n2 = nums[i + 1]
s += abs(n1 - n2)
gain = max(
gain,
abs(nums[0] - n2) - abs(n1 - n2),
abs(nums[n - 1] - n1) - abs(n1 - n2),
)
h = max(h, min(n1, n2))
l = min(l, max(n1, n2))
return s + max(gain, 2 * (h - l)) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR |
You are given an integer array nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all 0 <= i < nums.length-1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
1 <= nums.length <= 3*10^4
-10^5 <= nums[i] <= 10^5 | class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
ans = 0
for i in range(len(nums) - 1):
ans += abs(nums[i + 1] - nums[i])
d = 0
for i in range(1, len(nums) - 1):
d = max(
d,
abs(nums[0] - nums[i + 1]) - abs(nums[i] - nums[i + 1]),
abs(nums[-1] - nums[i - 1]) - abs(nums[i] - nums[i - 1]),
)
high = -sys.maxsize
low = sys.maxsize
for x, y in zip(nums, nums[1:]):
high = max(high, min(x, y))
low = min(low, max(x, y))
return ans + max(d, (high - low) * 2) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR |
You are given an integer array nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all 0 <= i < nums.length-1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
1 <= nums.length <= 3*10^4
-10^5 <= nums[i] <= 10^5 | class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return 0
if n == 2:
return abs(nums[1] - nums[0])
base = 0
for i in range(n - 1):
base = base + abs(nums[i + 1] - nums[i])
res = base
for i in range(1, n - 1):
if res < base + abs(nums[i + 1] - nums[0]) - abs(nums[i + 1] - nums[i]):
res = base + abs(nums[i + 1] - nums[0]) - abs(nums[i + 1] - nums[i])
for i in range(1, n - 1):
if res < base + abs(nums[i - 1] - nums[n - 1]) - abs(nums[i] - nums[i - 1]):
res = base + abs(nums[i - 1] - nums[n - 1]) - abs(nums[i] - nums[i - 1])
currMax = nums[0], nums[1]
currMin = nums[0], nums[1]
for i in range(1, n - 1):
curr = nums[i], nums[i + 1]
if min(currMax) > max(curr):
if res < base + 2 * (min(currMax) - max(curr)):
res = base + 2 * (min(currMax) - max(curr))
if max(currMin) < min(curr):
if res < base + 2 * (min(curr) - max(currMin)):
res = base + 2 * (min(curr) - max(currMin))
if min(curr) > min(currMax):
currMax = curr
if max(curr) < max(currMin):
currMin = curr
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
You are given an integer array nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all 0 <= i < nums.length-1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
1 <= nums.length <= 3*10^4
-10^5 <= nums[i] <= 10^5 | class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
return sum(abs(a - b) for a, b in zip(nums, nums[1:])) + max(
max(abs(nums[0] - b) - abs(a - b) for a, b in zip(nums, nums[1:])),
max(abs(nums[-1] - a) - abs(a - b) for a, b in zip(nums, nums[1:])),
2
* (
max(min(a, b) for a, b in zip(nums, nums[1:]))
- min(max(a, b) for a, b in zip(nums, nums[1:]))
),
) | CLASS_DEF FUNC_DEF VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR |
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.
You are given an array [a_1, a_2, ..., a_n].
Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times.
An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array.
Output
You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0.
Examples
Input
7
1 1 2 2 3 3 3
Output
6
Input
10
1 1 1 5 4 1 3 1 2 2
Output
7
Input
1
1
Output
0
Note
In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times. | n = int(input())
arr = list(map(int, input().split()))
freq = [0] * 101
for i in arr:
freq[i] += 1
maxx = max(freq)
amtOFmaxx = freq.count(maxx)
if amtOFmaxx >= 2:
print(n)
else:
must_apper = freq.index(maxx)
ans = 0
for j in range(1, 101):
if j == must_apper:
continue
first_index = [10**6] * (n + 1)
first_index[0] = -1
curr = 0
for i in range(n):
if arr[i] == must_apper:
curr += 1
elif arr[i] == j:
curr -= 1
ans = max(ans, i - first_index[curr])
first_index[curr] = min(first_index[curr], i)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).
You may perform the following operations until both a and s are empty:
Take the first element of a, push it into s and remove it from a (if a is not empty); Take the top element from s, append it to the end of array b and remove it from s (if s is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.
For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:
Remove 3 from a and push it into s; Remove 1 from a and push it into s; Remove 1 from s and append it to the end of b; Remove 2 from a and push it into s; Remove 2 from s and append it to the end of b; Remove 3 from s and append it to the end of b.
After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.
You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k q_{i} = p_{i}, and q_{k} > p_{k}). You may not swap or change any of first k elements of the permutation.
Print the lexicographically maximal permutation p you can obtain.
If there exists no answer then output -1.
-----Input-----
The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains k integers p_1, p_2, ..., p_{k} (1 ≤ p_{i} ≤ n) — the first k elements of p. These integers are pairwise distinct.
-----Output-----
If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1.
-----Examples-----
Input
5 3
3 2 1
Output
3 2 1 5 4
Input
5 3
2 3 1
Output
-1
Input
5 1
3
Output
3 2 1 5 4
Input
5 2
3 4
Output
-1 | import sys
f = sys.stdin
n, k = map(int, f.readline().split())
s, t = [n + 1], 1
a = list(map(int, f.readline().split()))
for i in range(n):
if i >= k:
a += [s[-1] - 1]
s += [a[i]]
while len(s) != 0 and s[-1] == t:
s.pop()
t += 1
if len(s):
print("-1")
else:
print(" ".join(str(x) for x in a)) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR LIST BIN_OP VAR NUMBER NUMBER VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).
You may perform the following operations until both a and s are empty:
Take the first element of a, push it into s and remove it from a (if a is not empty); Take the top element from s, append it to the end of array b and remove it from s (if s is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.
For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:
Remove 3 from a and push it into s; Remove 1 from a and push it into s; Remove 1 from s and append it to the end of b; Remove 2 from a and push it into s; Remove 2 from s and append it to the end of b; Remove 3 from s and append it to the end of b.
After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.
You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k q_{i} = p_{i}, and q_{k} > p_{k}). You may not swap or change any of first k elements of the permutation.
Print the lexicographically maximal permutation p you can obtain.
If there exists no answer then output -1.
-----Input-----
The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains k integers p_1, p_2, ..., p_{k} (1 ≤ p_{i} ≤ n) — the first k elements of p. These integers are pairwise distinct.
-----Output-----
If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1.
-----Examples-----
Input
5 3
3 2 1
Output
3 2 1 5 4
Input
5 3
2 3 1
Output
-1
Input
5 1
3
Output
3 2 1 5 4
Input
5 2
3 4
Output
-1 | import sys
n, k = [int(x) for x in input().split()]
a = list(reversed([(int(x) - 1) for x in input().split()]))
s = []
b = []
goal = 0
used = [False] * n
for node in a:
used[node] = True
search_from = -1
big = n - 1
res = []
while goal != n:
while a:
res.append(a[-1])
s.append(a.pop())
search_from = s[-1] - 1
if len(s) > 1 and s[-1] > s[-2]:
print(-1)
return
while s and s[-1] == goal:
goal += 1
s.pop()
if s:
search_from = s[-1] - 1
if goal == n:
break
if len(s) == 0:
while big >= 0 and used[big]:
big -= 1
if big == -1:
print(-1)
return
used[big] = True
a.append(big)
else:
while search_from >= 0 and used[search_from]:
search_from -= 1
if search_from == -1:
print(-1)
return
used[search_from] = True
a.append(search_from)
print(*[(x + 1) for x in res]) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN WHILE VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).
You may perform the following operations until both a and s are empty:
Take the first element of a, push it into s and remove it from a (if a is not empty); Take the top element from s, append it to the end of array b and remove it from s (if s is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.
For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:
Remove 3 from a and push it into s; Remove 1 from a and push it into s; Remove 1 from s and append it to the end of b; Remove 2 from a and push it into s; Remove 2 from s and append it to the end of b; Remove 3 from s and append it to the end of b.
After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.
You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k q_{i} = p_{i}, and q_{k} > p_{k}). You may not swap or change any of first k elements of the permutation.
Print the lexicographically maximal permutation p you can obtain.
If there exists no answer then output -1.
-----Input-----
The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains k integers p_1, p_2, ..., p_{k} (1 ≤ p_{i} ≤ n) — the first k elements of p. These integers are pairwise distinct.
-----Output-----
If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1.
-----Examples-----
Input
5 3
3 2 1
Output
3 2 1 5 4
Input
5 3
2 3 1
Output
-1
Input
5 1
3
Output
3 2 1 5 4
Input
5 2
3 4
Output
-1 | import sys
def print_list(list):
for i in list:
print(i, end=" ")
print()
n, k = [int(i) for i in input().split(" ")]
my_list = [int(i) for i in input().split(" ")]
stack = list()
next_pop = 1
for num in my_list:
if stack and stack[-1] < num:
print("-1")
sys.exit()
stack.append(num)
while stack and stack[-1] == next_pop:
stack.pop()
next_pop += 1
while stack:
for i in range(stack[-1] - 1, next_pop - 1, -1):
my_list.append(i)
next_pop = stack.pop() + 1
if next_pop > n:
print_list(my_list)
else:
for j in range(n, next_pop - 1, -1):
my_list.append(j)
print_list(my_list) | IMPORT FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).
You may perform the following operations until both a and s are empty:
Take the first element of a, push it into s and remove it from a (if a is not empty); Take the top element from s, append it to the end of array b and remove it from s (if s is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.
For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:
Remove 3 from a and push it into s; Remove 1 from a and push it into s; Remove 1 from s and append it to the end of b; Remove 2 from a and push it into s; Remove 2 from s and append it to the end of b; Remove 3 from s and append it to the end of b.
After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.
You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k q_{i} = p_{i}, and q_{k} > p_{k}). You may not swap or change any of first k elements of the permutation.
Print the lexicographically maximal permutation p you can obtain.
If there exists no answer then output -1.
-----Input-----
The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains k integers p_1, p_2, ..., p_{k} (1 ≤ p_{i} ≤ n) — the first k elements of p. These integers are pairwise distinct.
-----Output-----
If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1.
-----Examples-----
Input
5 3
3 2 1
Output
3 2 1 5 4
Input
5 3
2 3 1
Output
-1
Input
5 1
3
Output
3 2 1 5 4
Input
5 2
3 4
Output
-1 | n, k = map(int, input().split())
p = list(map(int, input().split()))
d = {x: (1) for x in p}
def solve(p, d, n):
add = []
s = []
for x in range(1, n + 1):
if x not in d:
while len(p) > 0:
s.append(p.pop(0))
if len(s) >= 2 and s[-1] > s[-2]:
return False, None
if len(s) == 0 or s[-1] != x:
up = n if len(s) == 0 else s[-1] - 1
for y in range(up, x - 1, -1):
add.append(y)
s.append(y)
d[y] = 1
s.pop()
else:
if len(s) == 0 or s[-1] != x:
while len(p) > 0:
s.append(p.pop(0))
if len(s) >= 2 and s[-1] > s[-2]:
return False, None
if s[-1] == x:
break
s.pop()
return True, add
ans = [x for x in p]
flg, add = solve(p, d, n)
if flg == False:
print(-1)
else:
print(" ".join([str(x) for x in ans + add])) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER NONE IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER NONE IF VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP VAR VAR |
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).
You may perform the following operations until both a and s are empty:
Take the first element of a, push it into s and remove it from a (if a is not empty); Take the top element from s, append it to the end of array b and remove it from s (if s is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.
For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:
Remove 3 from a and push it into s; Remove 1 from a and push it into s; Remove 1 from s and append it to the end of b; Remove 2 from a and push it into s; Remove 2 from s and append it to the end of b; Remove 3 from s and append it to the end of b.
After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.
You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k q_{i} = p_{i}, and q_{k} > p_{k}). You may not swap or change any of first k elements of the permutation.
Print the lexicographically maximal permutation p you can obtain.
If there exists no answer then output -1.
-----Input-----
The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains k integers p_1, p_2, ..., p_{k} (1 ≤ p_{i} ≤ n) — the first k elements of p. These integers are pairwise distinct.
-----Output-----
If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1.
-----Examples-----
Input
5 3
3 2 1
Output
3 2 1 5 4
Input
5 3
2 3 1
Output
-1
Input
5 1
3
Output
3 2 1 5 4
Input
5 2
3 4
Output
-1 | import sys
n, k = map(int, input().split())
a = list(map(int, input().split()))
setofa = set(a)
s = []
f = False
ai = 0
ans = []
for i in range(1, n + 1):
if i in setofa:
while ai < k and (len(s) == 0 or s[-1] != i):
s.append(a[ai])
ai += 1
if len(s) == 0 or s[-1] != i:
f = True
break
s.pop(-1)
a += ans[::-1]
ans = []
else:
if ai != k:
s += a[ai:k]
ai = k
ans.append(i)
if f:
print(-1)
else:
print(" ".join(map(str, a + ans[::-1]))) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER |
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).
You may perform the following operations until both a and s are empty:
Take the first element of a, push it into s and remove it from a (if a is not empty); Take the top element from s, append it to the end of array b and remove it from s (if s is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.
For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:
Remove 3 from a and push it into s; Remove 1 from a and push it into s; Remove 1 from s and append it to the end of b; Remove 2 from a and push it into s; Remove 2 from s and append it to the end of b; Remove 3 from s and append it to the end of b.
After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.
You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k q_{i} = p_{i}, and q_{k} > p_{k}). You may not swap or change any of first k elements of the permutation.
Print the lexicographically maximal permutation p you can obtain.
If there exists no answer then output -1.
-----Input-----
The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains k integers p_1, p_2, ..., p_{k} (1 ≤ p_{i} ≤ n) — the first k elements of p. These integers are pairwise distinct.
-----Output-----
If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1.
-----Examples-----
Input
5 3
3 2 1
Output
3 2 1 5 4
Input
5 3
2 3 1
Output
-1
Input
5 1
3
Output
3 2 1 5 4
Input
5 2
3 4
Output
-1 | import sys
n, k = map(int, sys.stdin.buffer.readline().decode("utf-8").split())
a = list(map(int, sys.stdin.buffer.readline().decode("utf-8").split()))
result = [0]
used = [0] * (n + 1)
stack = []
for x in a:
used[x] = 1
if result[-1] + 1 == x:
result.append(x)
while stack and result[-1] + 1 == stack[-1]:
result.append(stack.pop())
else:
if stack and stack[-1] < x:
print(-1)
exit()
stack.append(x)
stack = [n + 1] + stack + [0]
for i in range(len(stack) - 2, -1, -1):
for j in range(stack[i] - 1, stack[i + 1], -1):
if not used[j]:
a.append(j)
sys.stdout.buffer.write(" ".join(map(str, a)).encode("utf-8")) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP VAR NUMBER VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING |
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).
You may perform the following operations until both a and s are empty:
Take the first element of a, push it into s and remove it from a (if a is not empty); Take the top element from s, append it to the end of array b and remove it from s (if s is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.
For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:
Remove 3 from a and push it into s; Remove 1 from a and push it into s; Remove 1 from s and append it to the end of b; Remove 2 from a and push it into s; Remove 2 from s and append it to the end of b; Remove 3 from s and append it to the end of b.
After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.
You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k q_{i} = p_{i}, and q_{k} > p_{k}). You may not swap or change any of first k elements of the permutation.
Print the lexicographically maximal permutation p you can obtain.
If there exists no answer then output -1.
-----Input-----
The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains k integers p_1, p_2, ..., p_{k} (1 ≤ p_{i} ≤ n) — the first k elements of p. These integers are pairwise distinct.
-----Output-----
If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1.
-----Examples-----
Input
5 3
3 2 1
Output
3 2 1 5 4
Input
5 3
2 3 1
Output
-1
Input
5 1
3
Output
3 2 1 5 4
Input
5 2
3 4
Output
-1 | n, k = map(int, input().split(" "))
p = list(map(int, input().split(" ")))
i = 0
s = []
cur = 1
solution = list(p)
while True:
if len(s) > 0 and s[-1] == cur:
cur += 1
s.pop()
elif i < len(p):
if len(s) > 0 and p[i] > s[-1]:
solution = [-1]
break
s.append(p[i])
i += 1
else:
break
if solution[0] != -1:
while cur <= n:
top = s.pop() if len(s) > 0 else n + 1
solution.extend(reversed(range(cur, top)))
cur = top + 1
print(" ".join(str(x) for x in solution)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).
You may perform the following operations until both a and s are empty:
Take the first element of a, push it into s and remove it from a (if a is not empty); Take the top element from s, append it to the end of array b and remove it from s (if s is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.
For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:
Remove 3 from a and push it into s; Remove 1 from a and push it into s; Remove 1 from s and append it to the end of b; Remove 2 from a and push it into s; Remove 2 from s and append it to the end of b; Remove 3 from s and append it to the end of b.
After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.
You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k q_{i} = p_{i}, and q_{k} > p_{k}). You may not swap or change any of first k elements of the permutation.
Print the lexicographically maximal permutation p you can obtain.
If there exists no answer then output -1.
-----Input-----
The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains k integers p_1, p_2, ..., p_{k} (1 ≤ p_{i} ≤ n) — the first k elements of p. These integers are pairwise distinct.
-----Output-----
If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1.
-----Examples-----
Input
5 3
3 2 1
Output
3 2 1 5 4
Input
5 3
2 3 1
Output
-1
Input
5 1
3
Output
3 2 1 5 4
Input
5 2
3 4
Output
-1 | import sys
input = sys.stdin.buffer.readline
n, k = map(int, input().split())
p = list(map(int, input().split()))
blocks = [[1, n]]
fail = 0
for i in range(k):
if blocks[-1][0] <= p[i] <= blocks[-1][1]:
if p[i] == blocks[-1][0]:
blocks[-1][0] += 1
elif p[i] == blocks[-1][1]:
blocks[-1][1] -= 1
else:
blocks.append([blocks[-1][0], p[i] - 1])
blocks[-2][0] = p[i] + 1
if blocks[-1][0] > blocks[-1][1]:
blocks.pop()
else:
fail = 1
if fail:
print(-1)
else:
for i in p[::-1]:
blocks.append([i, i])
while blocks:
block = blocks.pop()
print(*range(block[1], block[0] - 1, -1), end=" ") | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER STRING |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | def maxProduct(data, currentHeight):
index = 0
while index != len(data):
nextDepth = list()
for node in currentHeight:
nextDepth.append(node * data[index])
nextDepth.append(node * data[index + 1])
index += 2
currentHeight = nextDepth
return max(currentHeight)
while True:
if input() == "0":
break
data = [int(n) for n in input().split()]
root = [data.pop(0)]
print(maxProduct(data, root) % 1000000007) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR WHILE NUMBER IF FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | n = int(input())
while n != 0:
a = list(map(int, input().split()))
for i in range(len(a) // 2 - 1, -1, -1):
a[i] = max(a[i * 2 + 1] * a[i], a[i * 2 + 2] * a[i])
print(a[0] % 1000000007)
n = int(input()) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | def maxProduct(data, fringe, H):
for i in range(H - 1):
nextDepth = list()
for node in fringe:
nextDepth.append(node * next(data))
nextDepth.append(node * next(data))
fringe = nextDepth
return max(fringe)
while True:
H = int(input())
if H == 0:
break
data = iter(map(int, input().split()))
print(maxProduct(data, [next(data)], H) % 1000000007) | FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR VAR NUMBER |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | from sys import *
mod = 1000000000 + 7
def solve():
lst = list(map(int, input().strip().split(" ")))
if n == 1:
print(lst[0])
return
index = len(lst) - 1
leaf_nodes = index - 2 ** (n - 1) + 2
ans = [0] * (index + 1)
while index != 0:
last = lst.pop()
if leaf_nodes > 0:
ans[index] = last
leaf_nodes -= 1
p_index = (index - 1) // 2
ans[p_index] = max(ans[p_index], lst[p_index] * ans[index])
index -= 1
print(ans[0] % mod)
for _ in range(15):
n = int(input().strip())
if n == 0:
break
solve() | ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | def makeAdj(arr):
n = len(arr)
adj = {i: [] for i in range(n)}
for i in range(n):
if 2 * i + 2 < n:
adj[i].append(2 * i + 1)
adj[i].append(2 * i + 2)
adj[2 * i + 1].append(i)
adj[2 * i + 2].append(i)
elif 2 * i + 1 < n:
adj[i].append(2 * i + 1)
adj[2 * i + 1].append(i)
return adj
def dfs(node, par, arr, adj, P):
if len(adj[node]) == 1:
P[node] = arr[node]
return
for child in adj[node]:
if child == par:
continue
dfs(child, node, arr, adj, P)
P[node] = max(P[child] * arr[node], P[node])
return P
while True:
h = int(input())
if h == 0:
break
arr = list(map(int, input().split()))
adj = makeAdj(arr)
P = [0] * len(arr)
print(dfs(0, -1, arr, adj, P)[0] % 1000000007) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | while True:
n = int(input())
if n == 0:
break
L = list(map(int, input().split()))
i = n - 1
while i > 0:
if i >= 2:
for j in range(2**i - 2, 2 ** (i - 1) - 2, -1):
L[j] *= max(L[-1], L[-2])
L.pop()
L.pop()
else:
L[0] *= max(L[1], L[2])
i -= 1
print(L[0] % (10**9 + 7)) | WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | i = int(input())
modu = 1000000007
while i != 0:
tree = [int(i) for i in input().split()]
def P(ind):
l = 2 * ind + 1
r = 2 * ind + 2
if l >= len(tree):
return tree[ind]
else:
return tree[ind] * max(P(l), P(r))
print(P(0) % modu)
i = int(input()) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | MOD = 10**9 + 7
h = int(input())
while h != 0:
a = [int(i) for i in input().split()]
for i in range(len(a) - 1, -1, -2):
par = i // 2 - 1
if par > 0:
a[par] = max(a[par * 2 + 1] * a[par], a[par * 2 + 2] * a[par])
print(max(a[1] * a[0], a[2] * a[0]) % MOD)
h = int(input()) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | z = 0
while z == 0:
h = int(input())
if h == 0:
break
n = 2**h - 1
p = [0] * (n + 10)
v = list(map(int, input().split()))
for i in range(n - 1, -1, -1):
if 2 * i + 1 >= n:
p[i] = v[i]
else:
p[i] = v[i] * max(p[2 * i + 1], p[2 * i + 2])
ans = p[0] % (1000000000 + 7)
print(ans) | ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | a = 1000000007
while True:
N = int(input())
if N == 0:
break
v = list(map(int, input().split()))
len = (1 << N) - 1
for i in range(len // 2, -1, -1):
left = i * 2 + 1
right = i * 2 + 2
if left < len and right < len:
v[i] = v[i] * max(v[left], v[right])
elif left < len:
v[i] = v[i] * v[left]
print(v[0] % a) | ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | h = int(input())
while h != 0:
l = list(map(int, input().split()))
i = len(l)
f = 1
c = 10**9 + 7
prev = 10000000112121121
while i >= 1 and f == 1:
x = i // 2
if x < prev:
max = 0
prev = x
calv = l[i - 1] * l[x - 1]
if calv > max:
max = calv
if i % 2 == 0:
l[x - 1] = max
i = i - 1
print(l[0] % c)
h = int(input()) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | while True:
h = int(input())
if h == 0:
break
nums = list(map(int, input().split(" ")))
n = 2**h - 1
p = [0] * (n + 1)
for i in range(n - 1, -1, -1):
if 2 * i + 1 >= n:
p[i] = nums[i]
else:
p[i] = nums[i] * max(p[2 * i + 1], p[2 * i + 2])
print(p[0] % 1000000007) | WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | solver = 1
sol = []
def callup(s, solver):
if s == 1 or s == 0:
k = l[1] * solver
sol.append(k)
else:
solver = l[s] * solver
s = s // 2
callup(s, solver)
while True:
n = int(input())
if n == 0:
break
l = list(map(int, input().split()))
l.insert(0, 0)
for i in range(len(l) - 1, 0, -1):
l[i] = callup(i, solver)
solver = 1
print(max(sol) % 1000000007)
sol = []
solver = 1 | ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | mdl = 1000000007
while True:
h = int(input())
if h == 0:
break
n = 2**h - 1
z = input().split()
t = [0] + list(map(int, z[:n]))
for ix in range(n // 2, 0, -1):
ixc = 2 * ix
t[ix] *= max(t[ixc], t[ixc + 1])
print(t[1] % mdl) | ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR |
Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}.
------ Input ------
There are several test cases (fifteen at most), each formed as follows:
The first line contains a positive integer H (H ≤ 15).
The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}.
The input is ended with H = 0.
------ Output ------
For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007.
----- Sample Input 1 ------
2
1 2 3
3
3 1 5 2 6 4 7
0
----- Sample Output 1 ------
3
105
----- explanation 1 ------
The second test case is constructed as follows:
3
/ \
/ \
1 5
/ \ / \
2 6 4 7 | h = int(input())
while h != 0:
vs = [0] + list(map(int, input().split()))
for i in range(h - 1, 0, -1):
st = 2 ** (i - 1)
end = 2**i - 1
for j in range(st, end + 1):
vs[j] = vs[j] * max(vs[j * 2], vs[j * 2 + 1])
print(vs[1] % 1000000007)
h = int(input()) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | for _ in range(int(input())):
r, c, k = map(int, input().split())
left = c - k
if left <= 0:
left = 1
right = c + k
if right > 8:
right = 8
top = r - k
if top <= 0:
top = 1
bottom = r + k
if bottom > 8:
bottom = 8
length = bottom - top + 1
width = right - left + 1
print(length * width) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | n = int(input())
while n:
x, y, k = map(int, input().split())
count = 1
l = [[x - 1, y - 1]]
while k:
prev = 0
for i in range(prev, len(l)):
x = l[i][0]
y = l[i][1]
temp = [
[x - 1, y - 1],
[x - 1, y],
[x - 1, y + 1],
[x, y - 1],
[x, y + 1],
[x + 1, y - 1],
[x + 1, y],
[x + 1, y + 1],
]
prev = len(l)
for i in temp:
if i not in l:
if i[0] >= 0 and i[0] < 8 and i[1] >= 0 and i[1] < 8:
l.append(i)
if len(l) == 64:
break
k -= 1
print(len(l))
n -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | tn = int(input())
for _ in range(tn):
r, c, k = map(int, input().split())
l1 = min(8, c + k) - max(1, c - k) + 1
l2 = min(8, r + k) - max(1, r - k) + 1
print(l1 * l2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | adj = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]
num_test_cases = int(input())
for _ in range(num_test_cases):
R, C, K = [int(x) for x in input().split(" ")]
visited = set([(R, C)])
frontier = [(R, C, 1)]
while len(frontier) > 0:
curr_r, curr_c, curr_k = frontier.pop(0)
dk = curr_k + 1
for adj_r, adj_c in adj:
dr, dc = curr_r + adj_r, curr_c + adj_c
if dr < 1 or 8 < dr or dc < 1 or 8 < dc or (dr, dc) in visited:
continue
visited.add((dr, dc))
if dk <= K:
frontier.append((dr, dc, dk))
print(len(visited)) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | for tc in range(1, int(input()) + 1):
if tc:
ans = 0
x, y, k = list(map(int, input().split()))
for i in range(1, 8 + 1):
for j in range(1, 8 + 1):
if max(abs(i - x), abs(j - y)) <= k:
ans += 1
print(ans) | FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | T = int(input(""))
for t in range(T):
a = list(map(int, input("").strip().split()))
r, c, k = a[0], a[1], a[2]
rtl = max(r - k, 1)
ctl = max(c - k, 1)
rbr = min(r + k, 8)
cbr = min(c + k, 8)
print((rbr - rtl + 1) * (cbr - ctl + 1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | T = int(input())
while T > 0:
R, C, K = list(map(int, input().split()))
count = 0
i = R - K
while i <= R + K:
j = C - K
while j <= C + K:
if 0 < i < 9 and 0 < j < 9:
count += 1
j += 1
i += 1
print(count)
T -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR BIN_OP VAR VAR IF NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | t = int(input())
while t > 0:
r, c, k = map(int, input().split())
count = 0
for i in range(-k, k + 1):
for j in range(-k, k + 1):
x = r + i
y = c + j
if x >= 1 and x <= 8 and y >= 1 and y <= 8:
count += 1
print(count)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | def main():
T = int(input())
for counter in range(T):
r, c, k = map(int, input().split())
corner_points = [
[min(r + k, 8), min(c + k, 8)],
[min(r + k, 8), max(c - k, 1)],
[max(r - k, 1), min(c + k, 8)],
[max(r - k, 1), max(c - k, 1)],
]
ans = abs(corner_points[0][1] - corner_points[1][1] + 1) * abs(
corner_points[1][0] - corner_points[2][0] + 1
)
print(ans)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER LIST FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER LIST FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER LIST FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | nbofcases = int(input())
nbofblocks = 0
for i in range(nbofcases):
R, C, K = [int(x) for x in input().split()]
right = R + K
left = R - K
up = C - K
down = C + K
if up <= 0:
up = 1
if down > 8:
down = 8
if right > 8:
right = 8
if left <= 0:
left = 1
w = right - left + 1
l = down - up + 1
print(w * l) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | def isvalid(x, y):
if 1 <= x <= 8 and 1 <= y <= 8:
return True
else:
return False
T = int(input())
for _ in range(T):
r, c, k = map(int, input().split())
xmoves = [1, 1, 1, 0, 0, -1, -1, -1]
ymoves = [1, 0, -1, 1, -1, 0, -1, 1]
distance = [[(9) for i in range(9)] for i in range(9)]
visited = [[(0) for _i in range(9)] for i in range(9)]
idx = 0
L = [(r, c)]
visited[r][c] = 1
distance[r][c] = 0
while idx < len(L):
current_distance = distance[L[idx][0]][L[idx][1]]
for i in range(8):
x = L[idx][0] + xmoves[i]
y = L[idx][1] + ymoves[i]
if isvalid(x, y):
if not visited[x][y]:
visited[x][y] = 1
L.append((x, y))
distance[x][y] = current_distance + 1
idx += 1
ans = 0
for i in range(1, 9):
for j in range(1, 9):
if distance[i][j] <= k:
ans += 1
print(ans) | FUNC_DEF IF NUMBER VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | t = int(input())
for i in range(t):
rck = list(map(int, input().split()))
r = rck[0]
c = rck[1]
k = rck[2]
if c - k < 1:
left = 1
else:
left = c - k
if c + k > 8:
right = 8
else:
right = c + k
if r - k < 1:
top = 1
else:
top = r - k
if r + k > 8:
bot = 8
else:
bot = r + k
print((right - left + 1) * (bot - top + 1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | T = int(input())
for _ in range(T):
r, c, k = map(int, input().split())
count = 0
for i in range(1, 9):
for j in range(1, 9):
if max(abs(r - i), abs(c - j)) <= k:
count += 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | for _ in range(int(input())):
Y, X, K = map(int, input().split())
l = X - K if X - K > 0 else 1
r = X + K if X + K < 9 else 8
u = Y - K if Y - K > 0 else 1
d = Y + K if Y + K < 9 else 8
print((r - l + 1) * (d - u + 1)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | l = []
cur = []
for i in range(8):
a = []
for j in range(8):
a.append(0)
l.append(a)
cur.append(a)
def der(r, c, x, y, k):
if k <= 0 or r < 0 or c < 0 or c > 7 or r > 7:
return
else:
if r == x and c == y:
l[r][c + 1] = 1
der(r, c + 1, x, y, k - 1)
l[r + 1][c] = 1
der(r + 1, c, x, y, k - 1)
l[r + 1][c + 1] = 1
der(r + 1, c + 1, x, y, k - 1)
l[r - 1][c] = 1
der(r - 1, c, x, y, k - 1)
l[r - 1][c + 1] = 1
der(r - 1, c + 1, x, y, k - 1)
l[r - 1][c - 1] = 1
der(r - 1, c - 1, x, y, k - 1)
l[r][c - 1] = 1
der(r, c - 1, x, y, k - 1)
l[r + 1][c - 1] = 1
der(r + 1, c - 1, x, y, k - 1)
if r == x and c > y:
l[r][c + 1] = 1
der(r, c + 1, x, y, k - 1)
if r == x and c < y:
l[r][c - 1] = 1
der(r, c - 1, x, y, k - 1)
if c == y and r > x:
l[r + 1][c] = 1
der(r + 1, c, x, y, k - 1)
if c == y and r < x:
l[r - 1][c] = 1
der(r - 1, c, x, y, k - 1)
if c > y and r > x:
l[r][c + 1] = 1
der(r, c + 1, x, y, k - 1)
l[r + 1][c] = 1
der(r + 1, c, x, y, k - 1)
l[r + 1][c + 1] = 1
der(r + 1, c + 1, x, y, k - 1)
if c > y and r < x:
l[r][c + 1] = 1
der(r, c + 1, x, y, k - 1)
l[r - 1][c] = 1
der(r - 1, c, x, y, k - 1)
l[r - 1][c + 1] = 1
der(r - 1, c + 1, x, y, k - 1)
if c < y and r < x:
l[r - 1][c] = 1
der(r - 1, c, x, y, k - 1)
l[r - 1][c - 1] = 1
der(r - 1, c - 1, x, y, k - 1)
l[r][c - 1] = 1
der(r, c - 1, x, y, k - 1)
if c < y and r > x:
l[r + 1][c] = 1
der(r + 1, c, x, y, k - 1)
l[r + 1][c - 1] = 1
der(r + 1, c - 1, x, y, k - 1)
l[r][c - 1] = 1
der(r, c - 1, x, y, k - 1)
def newder(r, c, k):
if k < 0 or r < 0 or c < 0 or c > 7 or r > 7:
return
else:
l[r][c] = 1
newder(r, c + 1, k - 1)
newder(r + 1, c, k - 1)
newder(r + 1, c + 1, k - 1)
newder(r - 1, c, k - 1)
newder(r - 1, c + 1, k - 1)
newder(r - 1, c - 1, k - 1)
newder(r, c - 1, k - 1)
newder(r + 1, c - 1, k - 1)
for _ in range(int(input())):
r, c, k = map(int, input().split())
r = r - 1
c = c - 1
ans = (min(7, c + k) - max(0, c - k) + 1) * (min(7, r + k) - max(0, r - k) + 1)
print(ans) | ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | n = int(input())
for i in range(n):
lst = list(map(int, input().split()))
r = lst[0]
c = lst[1]
k = lst[2]
r1 = max(1, r - k)
r2 = min(8, r + k)
c1 = max(1, c - k)
c2 = min(8, c + k)
ans = r2 - r1 + 1
ans1 = c2 - c1 + 1
print(ans * ans1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | for _ in range(int(input())):
r, c, k = map(int, input().split())
ans = 0
for i in range(r - k, r + k + 1):
for j in range(c - k, c + k + 1):
if 0 < i < 9 and 0 < j < 9:
ans += 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | for _ in range(int(input())):
r, c, k = map(int, input().split())
print((min(c + k, 8) - max(c - k, 1) + 1) * (min(r + k, 8) - max(r - k, 1) + 1)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | t = int(input())
for i in range(t):
r, c, k = map(int, input().split())
visited = []
g = [[None for i in range(8)] for j in range(8)]
ans = 0
for i in range(r - k, r + k + 1):
for j in range(c - k, c + k + 1):
if 0 < i < 9 and 0 < j < 9:
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NONE VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | for _ in range(int(input())):
r, c, k = map(int, input().split())
r1 = max(r - k, 1)
r2 = min(r + k, 8)
c1 = max(c - k, 1)
c2 = min(c + k, 8)
print((r2 - r1 + 1) * (c2 - c1 + 1)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | def read_input():
t = int(input())
test_cases = []
for i in range(t):
test_case = [int(item) for item in input().split()]
test_cases.append(test_case)
return t, test_cases
def solve_cases(t, test_cases):
for r, c, k in test_cases:
print(count_squares(r, c, k))
def count_squares(r, c, k):
visited = set()
to_visit = {(r, c)}
for i in range(k):
to_visit_next = set()
for x, y in to_visit:
visited.add((x, y))
for h in range(-1, 2):
new_x = x + h
if new_x > 0 and new_x < 9:
for k in range(-1, 2):
new_y = y + k
new_visited = new_x, new_y
if new_y > 0 and new_y < 9 and new_visited not in visited:
to_visit_next.add(new_visited)
to_visit = to_visit_next
return len(visited.union(to_visit))
t, test_cases = read_input()
solve_cases(t, test_cases) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | def solution(r, c, k):
ans = 0
for i in range(1, 9):
for j in range(1, 9):
if max(abs(r - i), abs(c - j)) <= k:
ans += 1
return ans
t = int(input())
for _ in range(t):
r, c, k = [int(e) for e in input().split()]
print(solution(r, c, k)) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | def f(r, c, k):
olist = []
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
for i in range(0, 8):
ilist = []
for j in range(0, 8):
ilist.append(0)
olist.append(ilist)
pos = []
ans = 1
pos.append([r - 1, c - 1])
olist[r - 1][c - 1] = 1
while len(pos) and k > 0:
cnt = len(pos)
while cnt > 0:
i, j = pos[0]
pos = pos[1:]
for l in range(0, 8):
ni = i + dx[l]
nj = j + dy[l]
if ni >= 0 and nj >= 0 and ni < 8 and nj < 8 and olist[ni][nj] == 0:
ans += 1
olist[ni][nj] = 1
pos.append([ni, nj])
cnt -= 1
k -= 1
return ans
t = int(input())
for i in range(0, t):
r, c, k = list(map(int, input().split()))
print(f(r, c, k)) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN 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 |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | def moves(x, y, k):
if k < 0 or x < 1 or y < 1 or x > 8 or y > 8:
return 0
u, d = x - k, x + k
l, r = y - k, y + k
if u < 1:
u = 1
if d > 8:
d = 8
if l < 1:
l = 1
if r > 8:
r = 8
a = (d - u + 1) * (r - l + 1)
return a
t = int(input())
while t:
t -= 1
x, y, k = map(int, input().split())
print(moves(x, y, k)) | FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | def squaresInRange(r, c, k):
topLeft = [min(8, r + k), max(1, c - k)]
bottomRight = [max(1, r - k), min(8, c + k)]
return (abs(topLeft[0] - bottomRight[0]) + 1) * (
abs(topLeft[1] - bottomRight[1]) + 1
)
testCount = int(input())
for i in range(testCount):
r, c, k = [int(x) for x in input().split()]
print(squaresInRange(r, c, k)) | FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR LIST FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | for _ in range(int(input())):
r, c, k = map(int, input().split())
top = max(1, r - k)
bot = min(8, r + k)
left = max(1, c - k)
rig = min(8, c + k)
p = bot - top + 1
q = rig - left + 1
m = p * q
print(m) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | for _ in range(int(input())):
r, c, k = map(int, input().split())
q = [(r - 1, c - 1)]
tx = [-1, 1, 0, 0, 1, -1, 1, -1]
ty = [0, 0, 1, -1, 1, 1, -1, -1]
a = [[(False) for _ in range(8)] for _ in range(8)]
final = 0
while k >= 0 and q:
q.append(-1)
c = 0
while q[0] != -1:
t = q.pop(0)
a[t[0]][t[1]] = True
c += 1
for x in range(8):
txx = t[0] + tx[x]
tyy = t[1] + ty[x]
if 0 <= txx < 8 and 0 <= tyy < 8 and a[txx][tyy] == False:
q.append((txx, tyy))
a[txx][tyy] = True
q.pop(0)
k -= 1
final += c
print(final) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF NUMBER VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | def check(i, j):
if i < 0 or i > 7 or j < 0 or j > 7:
return False
return True
t = int(input())
for _ in range(t):
r, c, k = list(map(int, input().split()))
chess = [[(0) for i in range(8)] for j in range(8)]
chess[r - 1][c - 1] = 1
q = [(r - 1, c - 1)]
d = [(1, 0), (0, 1), (0, -1), (-1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]
while k:
n = len(q)
while n:
i, j = q.pop(0)
for y, z in d:
ni, nj = i + y, j + z
if check(ni, nj) and chess[ni][nj] == 0:
chess[ni][nj] = 1
q.append((ni, nj))
n -= 1
k -= 1
count = 0
for i in range(8):
for j in range(8):
if chess[i][j]:
count += 1
print(count) | FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | t = int(input())
for iui in range(t):
arr = list(map(int, input().split()))
start = [arr[0] - 1, arr[1] - 1]
grid = [[(0) for k in range(8)] for l in range(8)]
m = arr[2]
count = 0
previous = [start]
current = []
for o in range(m):
for item in previous:
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if not (0 <= item[0] + i <= 7 and 0 <= item[1] + j <= 7):
continue
if grid[item[0] + i][item[1] + j] == 0:
grid[item[0] + i][item[1] + j] = 1
current.append([item[0] + i, item[1] + j])
count += 1
previous = current[:]
current.clear()
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR LIST NUMBER NUMBER NUMBER FOR VAR LIST NUMBER NUMBER NUMBER IF NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure. | t = int(input())
for _ in range(t):
r, c, k = map(int, input().strip().split())
l = 0
b = 0
if r + k > 8:
a = r + k - 8
l += k - a
else:
l += k
if r - k < 1:
a = 1 - (r - k)
l += k - a
else:
l += k
if c + k > 8:
a = c + k - 8
b += k - a
else:
b += k
if c - k < 1:
a = 1 - (c - k)
b += k - a
else:
b += k
print((l + 1) * (b + 1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
A string is called happy if it does not have any of the strings 'aaa', 'bbb' or 'ccc' as a substring.
Given three integers a, b and c, return any string s, which satisfies following conditions:
s is happy and longest possible.
s contains at most a occurrences of the letter 'a', at most b occurrences of the letter 'b' and at most c occurrences of the letter 'c'.
s will only contain 'a', 'b' and 'c' letters.
If there is no such string s return the empty string "".
Example 1:
Input: a = 1, b = 1, c = 7
Output: "ccaccbcc"
Explanation: "ccbccacc" would also be a correct answer.
Example 2:
Input: a = 2, b = 2, c = 1
Output: "aabbc"
Example 3:
Input: a = 7, b = 1, c = 0
Output: "aabaa"
Explanation: It's the only correct answer in this case.
Constraints:
0 <= a, b, c <= 100
a + b + c > 0 | class Solution:
def longestDiverseString(self, a: int, b: int, c: int) -> str:
if a == 0 and b == 0 and c == 0:
return ""
res = ""
heap = [(-a, "a"), (-b, "b"), (-c, "c")]
heapq.heapify(heap)
prev_val = 0
prev_char = ""
while heap:
v, char = heapq.heappop(heap)
if prev_val < 0:
heapq.heappush(heap, (prev_val, prev_char))
if abs(v) >= 2:
if abs(v) > abs(prev_val):
res += char * 2
v += 2
else:
res += char
v += 1
elif abs(v) == 1:
res += char
v += 1
elif abs(v) == 0:
break
prev_val = v
prev_char = char
return res | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN STRING ASSIGN VAR STRING ASSIGN VAR LIST VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.