description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
t = int(input())
ans = [None for _ in range(t)]
for q in range(t):
s = input()
P, S, R = 0, 0, 0
for i in range(len(s)):
if s[i] == "P":
P += 1
elif s[i] == "S":
S += 1
else:
R += 1
if S == P == R:
ans[q] = "P" * R + "S" * P + "R" * S
elif max(P, S, R) == P:
ans[q] = "S" * len(s)
elif max(P, S, R) == R:
ans[q] = "P" * len(s)
else:
ans[q] = "R" * len(s)
for i in range(t):
print(ans[i])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
def solve(s):
ans = ""
r = "R" * len(s)
S = "S" * len(s)
p = "P" * len(s)
cnt1, cnt2, cnt3 = s.count("R"), s.count("S"), s.count("P")
if cnt1 >= cnt2 and cnt1 >= cnt3:
print(p)
elif cnt2 >= cnt1 and cnt2 >= cnt3:
print(r)
else:
print(S)
def main():
t = int(input())
for i in range(t):
solve(input())
main()
|
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
for _ in range(int(input())):
string = input()
r, p, s = 0, 0, 0
for i in string:
if i == "R":
r += 1
elif i == "P":
p += 1
else:
s += 1
ar = [r, p, s]
max_in = None
max_no = 0
for i, j in enumerate(ar):
if j >= max_no:
max_no = j
max_in = i
length = len(string)
if max_in == 0:
print("P" * length)
elif max_in == 1:
print("S" * length)
else:
print("R" * length)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
t = int(input())
while t > 0:
t = t - 1
s = input()
n = len(s)
x = s.count("R")
y = s.count("P")
z = s.count("S")
if x >= y and x >= z:
c = "P" * n
elif y >= x and y >= z:
c = "S" * n
elif z >= y and z >= x:
c = "R" * n
print(c)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR VAR
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
t = int(input())
cases = []
for _ in range(t):
cases.append(list(input()))
for c in cases:
m = {"R": 0, "P": 0, "S": 0}
for i in range(len(c)):
m[c[i]] += 1
maxi = max(m, key=m.get)
if maxi == "R":
print("P" * len(c))
elif maxi == "P":
print("S" * len(c))
else:
print("R" * len(c))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
t = int(input())
for i in range(t):
s1 = input()
s = list(s1)
l = len(s1)
hm = {}
hm["R"] = 0
hm["S"] = 0
hm["P"] = 0
for j in s:
hm[j] += 1
if hm["R"] == hm["S"] and hm["R"] == hm["P"]:
print(s1)
continue
q = max(hm, key=hm.get)
if q == "R":
print("P" * l)
continue
if q == "S":
print("R" * l)
continue
if q == "P":
print("S" * l)
continue
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR STRING VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
import sys
input = sys.stdin.readline
def int_array():
return list(map(int, input().strip().split()))
def str_array():
return input().strip().split()
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
for _ in range(int(input())):
arr = list(input())
n = len(arr)
r, p, s = arr.count("R"), arr.count("P"), arr.count("S")
maxi = max(r, p, s)
if r == maxi:
print("P" * (n - 1))
elif p == maxi:
print("S" * (n - 1))
else:
print("R" * (n - 1))
|
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
tests = int(input())
for _ in range(tests):
s = input()
n = len(s)
cnt = {"R": 0, "P": 0, "S": 0}
for c in s:
cnt[c] += 1
target = max(cnt.values())
ans = "R"
for u, v in cnt.items():
if v == target:
if u == "R":
ans = "P"
if u == "P":
ans = "S"
break
print(ans * n)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR IF VAR VAR IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
for kek in range(int(input())):
x = input()
R = 0
S = 0
P = 0
for i in x:
if i == "R":
R += 1
if i == "S":
S += 1
if i == "P":
P += 1
ans = ""
if max(S, R, P) == P:
ans = "S"
elif max(S, R, P) == S:
ans = "R"
else:
ans = "P"
print(ans * len(x))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
for i in range(int(input())):
di = dict()
n = input()
for i in n:
di[i] = di.get(i, 0) + 1
b = max(di.values())
if len(di) == 1:
for i, j in di.items():
if i == "R":
x = ["P"] * j
print(*x, sep="")
elif i == "S":
x = ["R"] * j
print(*x, sep="")
elif i == "P":
x = ["S"] * j
print(*x, sep="")
else:
for i, j in di.items():
if j == b:
if i == "R":
x = ["P"] * len(n)
print(*x, sep="")
break
elif i == "S":
x = ["R"] * len(n)
print(*x, sep="")
break
elif i == "P":
x = ["S"] * len(n)
print(*x, sep="")
break
else:
continue
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR STRING ASSIGN VAR BIN_OP LIST STRING VAR EXPR FUNC_CALL VAR VAR STRING IF VAR STRING ASSIGN VAR BIN_OP LIST STRING VAR EXPR FUNC_CALL VAR VAR STRING IF VAR STRING ASSIGN VAR BIN_OP LIST STRING VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR FUNC_CALL VAR IF VAR VAR IF VAR STRING ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR STRING ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR STRING ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
def universalsol(s):
rock = s.count("R")
paper = s.count("P")
scissor = s.count("S")
if rock == paper == scissor:
print("R" * scissor + "P" * rock + "S" * paper)
else:
maximum = max(rock, paper, scissor)
if rock == maximum:
print("P" * len(s))
elif paper == maximum:
print("S" * len(s))
else:
print("R" * len(s))
t = int(input())
for i in range(t):
s = input().strip()
universalsol(s)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
for i in range(int(input())):
s = input()
n = len(s)
x, y, z = 0, 0, 0
for i in range(n):
if s[i] == "R":
y += 1
elif s[i] == "P":
z += 1
else:
x += 1
if y == max(x, y, z):
for i in range(n):
print("P", end="")
y -= 1
elif z == max(x, y, z):
for i in range(n):
print("S", end="")
z -= 1
elif x == max(x, y, z):
for i in range(n):
print("R", end="")
x -= 1
print()
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING VAR NUMBER EXPR FUNC_CALL VAR
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
def counter(s):
if s == "R":
return "P"
elif s == "P":
return "S"
elif s == "S":
return "R"
for t in range(int(input())):
bot = input()
n = len(bot)
if len(set(bot)) == 1:
print(counter(bot[0]) * n)
else:
d = {}
l = ["R", "P", "S"]
for i in l:
d[i] = bot.count(i)
k = max(d, key=d.get)
s = counter(k)
print(s * n)
|
FUNC_DEF IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR DICT ASSIGN VAR LIST STRING STRING STRING FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
def get_beating(x):
if x == "R":
return "P"
elif x == "P":
return "S"
else:
return "R"
for _ in range(int(input())):
play = input()
output = ""
R = play.count("R")
S = play.count("S")
P = play.count("P")
if R >= S and R >= P:
print(len(play) * get_beating("R"))
elif S >= R and S >= P:
print(len(play) * get_beating("S"))
else:
print(len(play) * get_beating("P"))
|
FUNC_DEF IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING
|
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P.
While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper";
In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third round — on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game.
You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins.
In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$) — the string of the bot.
It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$.
If there are multiple optimal answers, print any of them.
-----Example-----
Input
3
RRRR
RSP
S
Output
PPPP
RSP
R
-----Note-----
In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$.
In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average.
A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
|
t = int(input())
while t:
dict1 = {"R": "P", "P": "S", "S": "R"}
dict2 = {}
s = input()
for i in s:
if i in dict2:
dict2[i] += 1
else:
dict2[i] = 1
max1 = 0
ans = ""
for i in dict2:
if max1 < dict2[i]:
ans = i
max1 = dict2[i]
ans1 = ""
for i in range(len(s)):
ans1 += dict1[ans]
print(ans1)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Vasya has found a piece of paper with an array written on it. The array consists of n integers a_1, a_2, ..., a_{n}. Vasya noticed that the following condition holds for the array a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} for any positive integer i (i < n).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a_1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5) — the size of the array. The second line contains space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the original array.
It is guaranteed that the condition a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} fulfills for any positive integer i (i < n).
-----Output-----
In a single line print the sequence of n characters "+" and "-", where the i-th character is the sign that is placed in front of number a_{i}. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a_1. If there are multiple solutions, you are allowed to print any of them.
-----Examples-----
Input
4
1 2 3 5
Output
+++-
Input
3
3 3 5
Output
++-
|
def invert(s):
t = ""
for i in s:
if i == "+":
t += "-"
else:
t += "+"
return t
n = int(input())
if n == 1:
print("+")
exit()
a = list(map(int, input().split()))
cur = a[-1]
s = "+"
for i in range(n - 2, 0, -1):
if cur > 0:
cur -= a[i]
s += "-"
else:
cur += a[i]
s += "+"
if cur >= a[0]:
s += "-"
elif abs(cur) <= a[0] and cur <= 0:
s += "+"
elif 0 < cur < a[0]:
s = invert(s)
s += "+"
else:
s = invert(s)
s += "-"
print(s[::-1])
|
FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR STRING VAR VAR VAR VAR STRING IF VAR VAR NUMBER VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR STRING IF NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
|
Vasya has found a piece of paper with an array written on it. The array consists of n integers a_1, a_2, ..., a_{n}. Vasya noticed that the following condition holds for the array a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} for any positive integer i (i < n).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a_1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5) — the size of the array. The second line contains space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the original array.
It is guaranteed that the condition a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} fulfills for any positive integer i (i < n).
-----Output-----
In a single line print the sequence of n characters "+" and "-", where the i-th character is the sign that is placed in front of number a_{i}. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a_1. If there are multiple solutions, you are allowed to print any of them.
-----Examples-----
Input
4
1 2 3 5
Output
+++-
Input
3
3 3 5
Output
++-
|
n = int(input())
arr = list(map(int, input().split()))
if n == 1:
print("+")
elif n == 2:
print("-+")
else:
ans = ["+"]
cur = arr[-1]
for i in range(n - 2, -1, -1):
if cur > 0:
cur -= arr[i]
ans.append("-")
else:
cur += arr[i]
ans.append("+")
ans.reverse()
if cur < 0:
for i in range(n):
if ans[i] == "-":
ans[i] = "+"
else:
ans[i] = "-"
print("".join(ans))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Vasya has found a piece of paper with an array written on it. The array consists of n integers a_1, a_2, ..., a_{n}. Vasya noticed that the following condition holds for the array a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} for any positive integer i (i < n).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a_1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5) — the size of the array. The second line contains space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the original array.
It is guaranteed that the condition a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} fulfills for any positive integer i (i < n).
-----Output-----
In a single line print the sequence of n characters "+" and "-", where the i-th character is the sign that is placed in front of number a_{i}. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a_1. If there are multiple solutions, you are allowed to print any of them.
-----Examples-----
Input
4
1 2 3 5
Output
+++-
Input
3
3 3 5
Output
++-
|
n = int(input())
b = list(map(int, input().split()))
s = 0
res = []
j = n - 1
while j >= 0:
if s <= 0:
s += b[j]
res.append(1)
else:
s -= b[j]
res.append(0)
j += -1
res.reverse()
if s >= 0:
for j in res:
if j:
print("+", end="")
else:
print("-", end="")
else:
for j in res:
if j:
print("-", end="")
else:
print("+", end="")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER FOR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING FOR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING
|
Vasya has found a piece of paper with an array written on it. The array consists of n integers a_1, a_2, ..., a_{n}. Vasya noticed that the following condition holds for the array a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} for any positive integer i (i < n).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a_1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5) — the size of the array. The second line contains space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the original array.
It is guaranteed that the condition a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} fulfills for any positive integer i (i < n).
-----Output-----
In a single line print the sequence of n characters "+" and "-", where the i-th character is the sign that is placed in front of number a_{i}. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a_1. If there are multiple solutions, you are allowed to print any of them.
-----Examples-----
Input
4
1 2 3 5
Output
+++-
Input
3
3 3 5
Output
++-
|
n = int(input())
a = list(map(int, input().split(" ")))
temp_sgn = 1
sgns = []
curr_sum = 0
for i in range(n):
if curr_sum >= a[n - i - 1]:
sgns.append(1)
sgns.append(-1)
curr_sum -= a[n - i - 1]
else:
sgns.append(-1)
sgns.append(1)
curr_sum -= a[n - i - 1]
curr_sum *= -1
sgns.reverse()
ans = []
for i in range(2 * n):
if i % 2 == 0:
ans.append(temp_sgn * sgns[i])
else:
temp_sgn *= sgns[i]
for x in ans:
if x == 1:
print("+", end="")
else:
print("-", end="")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING
|
Vasya has found a piece of paper with an array written on it. The array consists of n integers a_1, a_2, ..., a_{n}. Vasya noticed that the following condition holds for the array a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} for any positive integer i (i < n).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a_1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5) — the size of the array. The second line contains space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the original array.
It is guaranteed that the condition a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} fulfills for any positive integer i (i < n).
-----Output-----
In a single line print the sequence of n characters "+" and "-", where the i-th character is the sign that is placed in front of number a_{i}. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a_1. If there are multiple solutions, you are allowed to print any of them.
-----Examples-----
Input
4
1 2 3 5
Output
+++-
Input
3
3 3 5
Output
++-
|
n = int(input())
a = list(map(int, input().split()))
r = [0] * n
s = 0
for i in range(n - 1, -1, -1):
if abs(s + a[i]) <= a[i]:
s += a[i]
else:
s -= a[i]
r[i] = 1
if s < 0:
print("".join(["-", "+"][i] for i in r))
else:
print("".join(["+", "-"][i] for i in r))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING LIST STRING STRING VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING LIST STRING STRING VAR VAR VAR
|
Vasya has found a piece of paper with an array written on it. The array consists of n integers a_1, a_2, ..., a_{n}. Vasya noticed that the following condition holds for the array a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} for any positive integer i (i < n).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a_1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5) — the size of the array. The second line contains space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the original array.
It is guaranteed that the condition a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} fulfills for any positive integer i (i < n).
-----Output-----
In a single line print the sequence of n characters "+" and "-", where the i-th character is the sign that is placed in front of number a_{i}. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a_1. If there are multiple solutions, you are allowed to print any of them.
-----Examples-----
Input
4
1 2 3 5
Output
+++-
Input
3
3 3 5
Output
++-
|
n = int(input())
t = list(map(int, input().split()))
t.reverse()
s, p = 0, [0] * n
for i, j in enumerate(t):
if s > 0:
p[i] = 1
s -= j
else:
s += j
p.reverse()
if s < 0:
print("".join("-+"[i] for i in p))
else:
print("".join("+-"[i] for i in p))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING STRING VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING STRING VAR VAR VAR
|
Vasya has found a piece of paper with an array written on it. The array consists of n integers a_1, a_2, ..., a_{n}. Vasya noticed that the following condition holds for the array a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} for any positive integer i (i < n).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a_1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5) — the size of the array. The second line contains space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the original array.
It is guaranteed that the condition a_{i} ≤ a_{i} + 1 ≤ 2·a_{i} fulfills for any positive integer i (i < n).
-----Output-----
In a single line print the sequence of n characters "+" and "-", where the i-th character is the sign that is placed in front of number a_{i}. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a_1. If there are multiple solutions, you are allowed to print any of them.
-----Examples-----
Input
4
1 2 3 5
Output
+++-
Input
3
3 3 5
Output
++-
|
n = int(input())
a = list(map(int, input().split()))
s = a[-1]
ans = ["+"]
for v in reversed(a[:-1]):
if s > 0:
s -= v
ans.append("-")
else:
s += v
ans.append("+")
if 0 <= s <= a[-1]:
print("".join(reversed(ans)))
else:
s = -a[-1]
ans = ["-"]
for v in reversed(a[:-1]):
if s > 0:
s -= v
ans.append("-")
else:
s += v
ans.append("+")
if 0 <= s <= a[-1]:
print("".join(reversed(ans)))
else:
s = a[-1]
ans = ["+"]
for v in reversed(a[:-1]):
if s >= 0:
s -= v
ans.append("-")
else:
s += v
ans.append("+")
if 0 <= s <= a[-1]:
print("".join(reversed(ans)))
else:
s = -a[-1]
ans = ["-"]
for v in reversed(a[:-1]):
if s >= 0:
s -= v
ans.append("-")
else:
s += v
ans.append("+")
print("".join(reversed(ans)))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING IF NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING IF NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING IF NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def main():
for t in range(int(input())):
n, m, k = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
x, y = 0, 0
flag1, flag2 = False, False
for i in range(k):
if a[i] // n > 2:
flag1 = True
if a[i] // n >= 2:
x += a[i] // n
for i in range(k):
if a[i] // m > 2:
flag2 = True
if a[i] // m >= 2:
y += a[i] // m
if (
x >= m
and (m % 2 == 0 or m % 2 == 1 and flag1)
or y >= n
and (n % 2 == 0 or n % 2 == 1 and flag2)
):
print("Yes")
else:
print("No")
main()
|
FUNC_DEF 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
try:
for _ in range(int(input())):
n, m, k = map(int, input().split(" "))
l = list(map(int, input().split(" ")))
l.sort()
mul = m * n
mn = 10000000
f = 0
mx = 0
g = 0
for i in range(k):
if l[i] // n > 1 and f == 0:
mul = mul - (l[i] - l[i] % n)
mn = min(mn, l[i] - l[i] % n)
if mul == n:
f = 1
else:
mx = max(mx, l[i])
if f == 1:
if mn // n < mx // n:
print("Yes")
else:
g = 1
elif mul <= 0:
print("Yes")
else:
g = 1
if g == 1:
mul = m * n
mn = 10000000
f = 0
mx = 0
for i in range(k):
if l[i] // m > 1 and f == 0:
mul = mul - (l[i] - l[i] % m)
mn = min(mn, l[i] - l[i] % m)
if mul == m:
f = 1
else:
mx = max(mx, l[i])
if f == 1:
if mn // m < mx // m:
print("Yes")
else:
print("No")
elif mul <= 0:
print("Yes")
else:
print("No")
except:
pass
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
t = int(input())
def solve(n, m):
res = 0
for i in piles:
u = i // n
if u > 1:
res += u
if res >= m:
if res - u == m - 1:
return piles[0] // n > 2
return True
return False
for _ in range(t):
n, m, k = map(int, input().split())
piles = sorted(map(int, input().split()), reverse=True)
if solve(n, m) or solve(m, n):
print("Yes")
else:
print("No")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
for _ in range(int(input())):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
cols, rows = [], []
for count in a:
if count // n >= 2:
cols.append(count // n)
if count // m >= 2:
rows.append(count // m)
ans = "No"
if m <= sum(cols):
if m % 2 == 1 and max(cols) == 2:
pass
else:
ans = "Yes"
if n <= sum(rows):
if n % 2 == 1 and max(rows) == 2:
pass
else:
ans = "Yes"
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR STRING IF VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def resi():
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
mDK = [0] * k
for i in range(k):
mDK[i] = a[i] // n
if mDK[i] == 1:
mDK[i] = 0
mDV = [0] * k
for i in range(k):
mDV[i] = a[i] // m
if mDV[i] == 1:
mDV[i] = 0
if max(mDV) == 2 and max(mDK) == 2:
if sum(mDK) >= m and m % 2 == 0:
print("Yes")
return
if sum(mDV) >= n and n % 2 == 0:
print("Yes")
return
print("No")
return
if max(mDV) == 2:
if sum(mDV) >= n and n % 2 == 0:
print("Yes")
return
if sum(mDK) >= m:
print("Yes")
return
print("No")
return
if max(mDK) == 2:
if sum(mDK) >= m and m % 2 == 0:
print("Yes")
return
if sum(mDV) >= n:
print("Yes")
return
print("No")
return
if sum(mDK) < m and sum(mDV) < n:
print("No")
return
print("Yes")
for _ in range(int(input())):
resi()
|
FUNC_DEF ASSIGN VAR 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 BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def solve(n, m):
s = 0
cut = False
for i in a:
t = i // n
if t >= 2:
s += t
if t > 2 and not cut:
cut = True
return s >= m and (m % 2 == 0 or cut)
for _ in range(int(input())):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
print("Yes" if solve(n, m) or solve(m, n) else "No")
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER NUMBER VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
t = int(input())
for _ in range(t):
n, m, k = map(int, input().split())
l = [int(x) for x in input().split()]
l.sort(reverse=True)
i = 0
ans = 0
flag = False
while i < k:
x = l[i] // m
if x > 2:
flag = True
if x >= 2:
ans += x
i += 1
if ans >= n and (flag or n % 2 == 0):
print("Yes")
else:
i = 0
ans = 0
False
while i < k:
x = l[i] // n
if x > 2:
flag = True
if x >= 2:
ans += x
i += 1
if ans >= m and (flag or m % 2 == 0):
print("Yes")
else:
print("No")
|
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
for _ in range(int(input())):
n, m, k = list(map(int, input().split()))
color = list(map(int, input().split()))
row_cost = m
col_cost = n
tot = 0
flag = False
for i in color:
if i // row_cost > 2:
flag = True
if i // row_cost >= 2:
tot += i // row_cost
if tot >= n and (n % 2 == 0 or flag):
print("Yes")
continue
tot = 0
flag = False
for i in color:
if i // col_cost > 2:
flag = True
if i // col_cost >= 2:
tot += i // col_cost
if tot >= m and (m % 2 == 0 or flag):
print("Yes")
else:
print("No")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL 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 ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
t = int(input())
while t > 0:
t -= 1
n, m, k = map(int, input().split())
numH, numV = 0, 0
ntH, ntV = False, False
a = list(map(int, input().split()))
for i in a:
if int(i / n) > 1:
numH += int(i / n)
if int(i / n) > 2:
ntH = True
if int(i / m) > 1:
numV += int(i / m)
if int(i / m) > 2:
ntV = True
ans = False
if numH >= m:
if m % 2 == 0 or ntH:
ans = True
if numV >= n:
if n % 2 == 0 or ntV:
ans = True
if ans:
print("Yes")
else:
print("No")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def possible(xs, m, n):
ys = [(x // m) for x in xs]
ys = [y for y in ys if y > 1]
if len(ys) == 0 or sum(ys) < n:
return False
return max(ys) >= 3 or n % 2 == 0
def solve():
n, m, k = map(int, input().split())
xs = [int(x) for x in input().split()]
if possible(xs, m, n) or possible(xs, n, m):
print("Yes")
else:
print("No")
def main():
T = int(input().strip())
for t in range(1, T + 1):
solve()
main()
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
valid = 0
an = a[:]
ans = 0
for i in range(len(an)):
an[i] //= n
if an[i] >= 2 and ans + 2 <= m:
ans += 2
an[i] -= 2
else:
an[i] = 0
if sum(an) + ans >= m:
valid = 1
if valid == 0:
an = a[:]
ans = 0
for i in range(len(an)):
an[i] //= m
if an[i] >= 2 and ans + 2 <= n:
ans += 2
an[i] -= 2
else:
an[i] = 0
if sum(an) + ans >= n:
valid = 1
if n == 1 or m == 1:
valid = 0
if valid:
print("Yes")
else:
print("No")
|
IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
t = int(input())
while t > 0:
t -= 1
n, m, k = [int(x) for x in input().split(" ")]
quantities = [int(x) for x in input().split(" ")]
row_count = 0
flag = False
for num in quantities:
if num // m >= 2:
row_count += num // m
if num // m >= 3:
flag = True
if row_count >= n and (n % 2 == 0 or flag):
print("Yes")
continue
col_count = 0
flag = False
for num in quantities:
if num // n >= 2:
col_count += num // n
if num // n >= 3:
flag = True
if col_count >= m and (m % 2 == 0 or flag):
print("Yes")
else:
print("No")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR 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 NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def solve(m, n, arr):
res = 0
flag = 0
for i in arr:
if i // n <= 1:
continue
res += i // n
if i // n >= 3:
flag = 1
if res >= m and (m % 2 == 1 and flag or m % 2 == 0):
return 1
else:
return 0
t = int(input())
while t:
n, m, k = map(int, input().split())
arr = [int(i) for i in input().split()]
if solve(m, n, arr) or solve(n, m, arr):
print("Yes")
else:
print("No")
t -= 1
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
t = int(input())
for _ in range(t):
n, m, k = map(int, input().split())
arr = list(map(int, input().split()))
arr1 = []
arr2 = []
for i in arr:
if i // n > 1:
arr1.append(i // n)
if i // m > 1:
arr2.append(i // m)
arr1.sort(reverse=True)
arr2.sort(reverse=True)
s1 = 0
for i in arr1:
s1 += i
if s1 > m:
if i - (s1 - m) == 1 and arr1[0] == 2:
break
s1 = m
if s1 == m:
break
s2 = 0
for i in arr2:
s2 += i
if s2 > n:
if i - (s2 - n) == 1 and arr2[0] == 2:
break
s2 = n
if s2 == n:
break
if s1 == m or s2 == n:
print("Yes")
else:
print("No")
|
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def iin():
return list(map(int, input().split(" ")))
def solve(n, m, p):
pigments = p.copy()
for pigment in pigments:
cols = pigment // n
if cols >= m:
return "Yes"
elif cols > 1 and m > 3:
m = max(2, m - cols)
return "No"
[t] = iin()
for _ in range(t):
[n, m, k] = iin()
pigments = iin()
pigments.sort()
if solve(n, m, pigments) == "Yes":
print("Yes")
else:
print(solve(m, n, pigments))
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR RETURN STRING ASSIGN LIST VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
t = int(input())
for x in range(t):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
cols = 0
for num in a:
if num // n > 1:
cols += num // n
ans = "No"
if m % 2 == 0 and cols >= m:
ans = "Yes"
elif cols >= m:
for num in a:
if num // n - 1 > 1:
ans = "Yes"
if ans == "Yes":
print(ans)
continue
rows = 0
for num in a:
if num // m > 1:
rows += num // m
if n % 2 == 0 and rows >= n:
ans = "Yes"
elif rows >= n:
for num in a:
if num // m - 1 > 1:
ans = "Yes"
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR STRING IF VAR VAR FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR STRING IF VAR VAR FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
t = int(input())
for _ in range(t):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
ok = False
for _ in range(2):
n, m = m, n
s = 0
o = 0
for i in range(k):
x = a[i] // n
if x > 1:
s += x
if x > 2:
o += 1
if s < m:
continue
if m % 2 == 0:
ok = True
break
elif o > 0:
ok = True
break
print("Yes" if ok else "No")
|
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def ss(t, l):
if sum(l) < t:
return False
x = sum(l)
twos = 0
odd = 0
even = 0
for i in l:
if i == 2:
twos += 1
elif i % 2 == 0:
even += 1
else:
odd += 1
if t % 2 == 0:
return True
xxx = 0
for i in l:
if i > 2:
xxx += 1
if xxx > 0:
return True
return False
for _ in range(int(input())):
n, m, k = map(int, input().split())
l = list(map(int, input().split()))
l = sorted(l)[::-1]
a1 = []
a2 = []
for i in l:
x = i // n
if x >= 2:
a1.append(x)
x = i // m
if x >= 2:
a2.append(x)
if ss(m, a1) or ss(n, a2):
print("Yes")
else:
print("No")
|
FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def solve(n, m):
b = [(x // n) for x in a]
b = list(filter(lambda x: x >= 2, b))
return sum(b) >= m and (m % 2 == 0 or any(x >= 3 for x in b))
for _ in range(int(input())):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
print("Yes" if solve(n, m) or solve(m, n) else "No")
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
for i in range(int(input())):
n, m, k = [int(x) for x in input().split()]
colors = [int(x) for x in input().split()]
col, row = [], []
for i in range(k):
cell = colors[i]
if cell // n >= 2:
col.append(cell // n)
else:
col.append(0)
if cell // m >= 2:
row.append(cell // m)
else:
row.append(0)
ans1 = False
if sum(col) >= m:
if m % 2 == 1:
for i in col:
if i >= 3:
ans1 = True
break
else:
ans1 = True
ans2 = False
if sum(row) >= n:
if n % 2 == 1:
for i in row:
if i >= 3:
ans2 = True
break
else:
ans2 = True
fin = "Yes" if ans1 or ans2 else "No"
print(fin)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def solve(n, m, k, K):
Q = [x for x in K]
P = [(0) for x in K]
idx = 0
z = n
for i in range(k):
if Q[i] >= 2 * m and z >= 2:
Q[i] -= 2 * m
P[i] = 2
z -= 2
if z == 0:
return True
for i in range(k):
if P[i] > 0:
z -= Q[i] // m
if z <= 0:
return True
return False
def read():
s = input().split(" ")
n = int(s[0])
m = int(s[1])
k = int(s[2])
s = input().split(" ")
return n, m, k, sorted([int(x) for x in s], reverse=True)
def main():
n = int(input())
for _ in range(n):
n, m, k, K = read()
if solve(n, m, k, K) or solve(m, n, k, K):
print("Yes")
else:
print("No")
pass
pass
main()
|
FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING RETURN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def column(a, n, m, k):
sumi = 0
if n % 2:
if a[-1] >= 3 * m:
a[-1] -= 3 * m
n -= 3
sumi += a[-1] // m
a[-1] = 0
else:
return -1
r = 2 * m
for i in range(k):
if a[i] >= r:
sumi += a[i] // m
if sumi >= n:
return 1
else:
return -1
t = int(input())
while t > 0:
x = input().split()
n, m, k = int(x[0]), int(x[1]), int(x[2])
a = input().split()
for i in range(k):
a[i] = int(a[i])
fl = 0
a.sort()
p = n
q = m
b = [a[i] for i in range(k)]
c = column(b, p, q, k)
d = column(a, m, n, k)
if c == 1 or d == 1:
print("Yes")
else:
print("No")
t -= 1
|
FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
from sys import stdin, stdout
def get_ints():
return map(int, stdin.readline().split())
def PRINT(s):
stdout.write(s + "\n")
def f(a, b, arr):
evens = []
odds = []
s = 0
t = False
for v in arr:
x = v // a
if x >= 2:
s += x
if x % 2 == 0:
evens.append(x)
else:
odds.append(x)
if x >= 4:
t = True
L = len(odds)
if not t and b % 2 == 1 and L == 0:
return False
if b % 2 == 0 and L % 2 == 1 or b % 2 == 1 and L % 2 == 0:
s -= 1
if s >= b:
return True
return False
def logic(n, m, arr):
if f(n, m, arr) or f(m, n, arr):
return "Yes"
return "No"
for w in range(int(stdin.readline())):
n, m, k = get_ints()
arr = [int(x) for x in stdin.readline().split()]
print(logic(n, m, arr))
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
for i in range(int(input())):
n, m, k = map(int, input().split())
m1 = []
n1 = []
m01 = 0
n01 = 0
for j in map(int, input().split()):
m1 += [j // n]
n1 += [j // m]
if j // n > 1:
m01 += j // n
if j // m > 1:
n01 += j // m
maxm1 = max(m1)
maxn1 = max(n1)
summ1 = sum(m1)
sumn1 = sum(n1)
if (
m == 1
and summ1 > 0
or n == 1
and sumn1 > 0
or m01 >= m
and (maxm1 > 2 or m % 2 < 1)
or n01 >= n
and (maxn1 > 2 or n % 2 < 1)
):
print("Yes")
else:
print("No")
|
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 ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST BIN_OP VAR VAR VAR LIST BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def test(n, m, a):
x = [(i // m) for i in a]
enough = sum(i for i in x if i >= 2) >= n
if n % 2 == 1:
enough = enough and any(i >= 3 for i in x)
return enough
T = int(input())
for cc in range(T):
n, m, k = (int(_) for _ in input().split())
a = [int(_) for _ in input().split()]
ans = test(n, m, a) or test(m, n, a)
print("Yes" if ans else "No")
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR 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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def canColor(n, m, colors):
colors.sort(reverse=True)
n, m = _n, _m = sorted([n, m])
cando = 0
mfirst = colors[0] // n
nfirst = colors[0] // m
for c in colors:
if c // n > 1:
if _m == 1:
if mfirst > 2:
_m = 0
else:
_m -= c // n
if c // m > 1:
if _n == 1:
if nfirst > 2:
_n = 0
else:
_n -= c // m
if _m <= 0 or _n <= 0:
cando = 1
return "Yes" if cando else "No"
def main():
t = int(input())
for _ in range(t):
n, m, k = map(int, input().split())
colors = list(map(int, input().split()))
print(canColor(n, m, colors))
main()
|
FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN VAR STRING STRING 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
R = lambda: map(int, input().split())
G = range
(t,) = R()
def f(n, m):
b = [(v // n) for v in a if v >= 2 * n]
return not (m & 1 and all(v == 2 for v in b)) and sum(b) >= m
for _ in G(t):
n, m, k = R()
a = [*R()]
print(["No", "Yes"][f(n, m) or f(m, n)])
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR EXPR FUNC_CALL VAR LIST STRING STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, m, k = map(int, input().split())
(*a,) = map(int, input().split())
b = []
c = []
bodd = 0
codd = 0
for e in a:
bi = e // n
bi = bi * (bi >= 2)
ci = e // m
ci = ci * (ci >= 2)
bodd |= bi >= 3
codd |= ci >= 3
b += [bi]
c += [ci]
sb = sum(b)
sc = sum(c)
o = ""
if sb >= m and (m & 1 == 0 or bodd) or sc >= n and (n & 1 == 0 or codd):
o = "Yes"
else:
o = "No"
print(o)
|
IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR LIST VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def solve(n, m, arr):
p = m
curr = 0
flag = False
for i in arr:
k = i // p
if k > 1:
curr += k
if k > 2:
flag = True
if n % 2 == 0:
if curr >= n:
return True
else:
return False
elif curr >= n and flag:
return True
else:
return False
t = int(input())
while t:
n, m, k = map(int, input().split())
arr = list(map(int, input().split()))
a = solve(n, m, arr)
b = solve(m, n, arr)
if a or b:
print("Yes")
else:
print("No")
t -= 1
|
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR RETURN NUMBER RETURN NUMBER 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def rr(a, n, m):
if m % 2 == 1:
count = 0
if a[0] >= 3 * n:
pass
else:
return False
for i in a:
if i // n >= 2:
count += i // n
if count >= m:
return True
else:
return False
else:
count = 0
for i in a:
if i // n >= 2:
count += i // n
if count >= m:
return True
else:
return False
for _ in range(int(input())):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
if rr(a, n, m):
print("Yes")
elif rr(a, m, n):
print("Yes")
else:
print("No")
|
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP NUMBER VAR RETURN NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
def solve(a, n, m):
b = []
flag = False
for i in a:
curr = i // n
if curr >= 3:
flag = True
if curr >= 2:
b.append(curr)
if m % 2 == 1:
if not flag:
return False
return sum(b) >= m
for t in range(int(input())):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
if solve(a.copy(), n, m) or solve(a.copy(), m, n):
print("Yes")
else:
print("No")
|
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
A picture can be represented as an $n\times m$ grid ($n$ rows and $m$ columns) so that each of the $n \cdot m$ cells is colored with one color. You have $k$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $a_i$ cells with the $i$-th pigment.
A picture is considered beautiful if each cell has at least $3$ toroidal neighbors with the same color as itself.
Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $1 \leq x_1,x_2 \leq n$ and $1 \leq y_1,y_2 \leq m$, the cell in the $x_1$-th row and $y_1$-th column is a toroidal neighbor of the cell in the $x_2$-th row and $y_2$-th column if one of following two conditions holds:
$x_1-x_2 \equiv \pm1 \pmod{n}$ and $y_1=y_2$, or
$y_1-y_2 \equiv \pm1 \pmod{m}$ and $x_1=x_2$.
Notice that each cell has exactly $4$ toroidal neighbors. For example, if $n=3$ and $m=4$, the toroidal neighbors of the cell $(1, 2)$ (the cell on the first row and second column) are: $(3, 2)$, $(2, 2)$, $(1, 3)$, $(1, 1)$. They are shown in gray on the image below:
The gray cells show toroidal neighbors of $(1, 2)$.
Is it possible to color all cells with the pigments provided and create a beautiful picture?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($3 \leq n,m \leq 10^9$, $1 \leq k \leq 10^5$) — the number of rows and columns of the picture and the number of pigments.
The next line contains $k$ integers $a_1,a_2,\dots, a_k$ ($1 \leq a_i \leq 10^9$) — $a_i$ is the maximum number of cells that can be colored with the $i$-th pigment.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).
-----Examples-----
Input
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
Output
Yes
No
Yes
Yes
No
No
-----Note-----
In the first test case, one possible solution is as follows:
In the third test case, we can color all cells with pigment $1$.
|
import sys
def solve1(n, m, a):
s = 0
ok = False
for i in a:
v = i // m
if v >= 2:
s += v
if v > 2:
ok = True
if ok:
return s >= n
else:
return s >= n and n % 2 == 0
def solve():
inp = sys.stdin.readline
n, m, k = map(int, inp().split())
a = list(map(int, inp().split()))
if solve1(n, m, a) or solve1(m, n, a):
print("Yes")
else:
print("No")
def main():
for i in range(int(sys.stdin.readline())):
solve()
main()
|
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR 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 IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
T = int(input())
for t in range(T):
N = int(input()) + 2
s = "1" + input() + "1"
A = [-10000000000] + [int(x) for x in input().split()] + [10000000000]
ss = 0
mm = 0
ans = 0
for i in range(1, N):
ss += A[i] - A[i - 1]
mm = max(mm, A[i] - A[i - 1])
if s[i] == "1":
ans += ss - mm
ss = 0
mm = 0
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
for z in range(t):
n = int(input())
s = input()
d, v = [], []
d = list(map(int, input().split()))
for i in range(len(s)):
if s[i] == "1":
v.append(i)
if n == 1:
print(0)
continue
a, ans = len(v), 0
for i in range(v[0] - 1, -1, -1):
ans += d[i + 1] - d[i]
for i in range(v[a - 1] + 1, n):
ans += d[i] - d[i - 1]
if a == 1:
print(ans)
continue
for i in range(a - 1):
sum, m, l, r = 0, 0, v[i], v[i + 1]
for j in range(l, r):
sum += d[j + 1] - d[j]
m = max(m, d[j + 1] - d[j])
ans += sum - m
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
while t > 0:
n = int(input())
a = input()
y = input().split()
d = []
for i in range(n):
d.append(int(y[i]))
s = 0
i = 0
while a[i] == "0":
s = s + (d[i + 1] - d[i])
i = i + 1
loc1 = i
i = n - 1
while a[i] == "0":
s = s + (d[i] - d[i - 1])
i = i - 1
loc2 = i
while loc1 < loc2:
i = loc1
flag = 0
while a[i + 1] == "1":
i = i + 1
if i == loc2:
flag = 1
break
if flag == 1:
break
j = i
maximum = 0
g = 0
while a[j + 1] == "0":
g = g + (d[j + 1] - d[j])
if maximum < d[j + 1] - d[j]:
maximum = d[j + 1] - d[j]
j = j + 1
g = g + (d[j + 1] - d[j])
if maximum < d[j + 1] - d[j]:
maximum = d[j + 1] - d[j]
g = g - maximum
s = s + g
loc1 = j + 1
print(s)
t = t - 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
MAX = 1 + 10**9
T = int(input())
for t in range(T):
N = int(input())
E = input()
X = list(map(int, input().strip().split()))
ans = 0
if E[0] == "0":
E = E.lstrip("0")
i = N - len(E)
ans = ans + X[i] - X[0]
X = X[i:]
N -= i
if E[-1] == "0":
E = E.rstrip("0")
i = N - len(E)
ans = ans + X[-1] - X[N - i - 1]
X = X[: N - i]
N = N - i
E = list(E)
A = [0] * N
B = [0] * N
if N > 1:
cur = 0
for i in range(N):
if E[i] == "1":
cur = 0
else:
A[i] = A[i - 1] + X[i] - X[i - 1]
for i in range(N - 1, -1, -1):
if E[i] == "1":
cur = 0
else:
B[i] = B[i + 1] + X[i + 1] - X[i]
i = 0
while i < N - 1:
if E[i] == "1":
mn = MAX
i += 1
while E[i] != "1":
mn = min(mn, A[i - 1] + B[i])
i += 1
mn = min(mn, A[i - 1] + B[i])
ans += mn
print(ans)
|
ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
def test(n, y, x):
v = x[-1] - x[0]
w = False
record = 0
for i in range(n):
if i:
delta = x[i] - x[i - 1]
if delta > record:
record = delta
if y[i]:
if w:
v -= record
else:
w = True
record = 0
return v
T = int(input())
for t in range(T):
n = int(input())
y = list(map(int, list(input())))
x = list(map(int, input().split()))
print(test(n, y, x))
|
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
for i in range(t):
n = int(input())
elec = input()
coord = list(map(int, input().split()))
sum1 = 0
sum = 0
temp1 = elec.index("1")
for j in range(0, temp1):
x = coord[j + 1] - coord[j]
sum1 += x
max = 0
for j in range(temp1, n - 1):
if elec[j] == "1":
sum -= max
max = 0
x = coord[j + 1] - coord[j]
sum += x
if x > max:
max = x
if elec[n - 1] == "1":
sum -= max
print(sum1 + sum)
|
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
T = int(input())
while T > 0:
N = int(input())
mystr = input()
user_input = input().split()
ind = 1
t = 1
firstNum = mystr[0]
mysum = 0
if firstNum == "0":
while mystr[t] != "1":
t += 1
mysum = int(user_input[t]) - int(user_input[0])
while t < N:
max_len = 0
total_len = 0
if mystr[t] == "0" and mystr[t - 1] == "1":
while t < N and mystr[t] != "1":
diff = int(user_input[t]) - int(user_input[t - 1])
total_len += diff
if diff > max_len:
max_len = diff
t += 1
if t < N:
diff = int(user_input[t]) - int(user_input[t - 1])
total_len += diff
if diff > max_len:
max_len = diff
mysum += total_len - max_len
else:
mysum += total_len
else:
t += 1
print(mysum)
T = T - 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING WHILE VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING WHILE VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
while t > 0:
n = int(input())
s = input()
x_coords = [int(x) for x in input().split()]
min_len = 0
left_index = s.find("1")
for i in range(left_index):
min_len += x_coords[i + 1] - x_coords[i]
right_index = s.rfind("1")
for i in range(right_index + 1, n):
min_len += x_coords[i] - x_coords[i - 1]
right_index = s.find("1", left_index + 1)
while True:
if right_index == -1:
break
i, j = left_index, right_index
while i != j - 1:
diff_left = x_coords[i + 1] - x_coords[i]
diff_right = x_coords[j] - x_coords[j - 1]
if diff_left <= diff_right:
min_len += diff_left
i += 1
else:
min_len += diff_right
j -= 1
left_index = right_index
right_index = s.find("1", right_index + 1)
print(min_len)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER WHILE NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
T = int(input())
while T > 0:
T -= 1
N = int(input())
light = [int(x) for x in input()]
x = [int(x) for x in input().split()]
i = 0
while light[i] != 1:
i += 1
cost = x[i] - x[0]
prev = i
i += 1
while i < N:
Flag = True
while light[i] != 1:
if i == N - 1:
cost += x[N - 1] - x[prev]
Flag = False
break
else:
i += 1
if Flag:
tmp = 10**15
for k in range(prev + 1, i):
tmp = min(
tmp,
x[k] - x[prev] + x[i] - x[k + 1],
x[k - 1] - x[prev] + x[i] - x[k],
)
if tmp == 10**15:
tmp = 0
cost += tmp
prev = i
i += 1
print(cost)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
for _ in range(t):
n = int(input())
e = [(True if i == "1" else False) for i in input().strip()]
X = [int(i) for i in input().split()]
ans = X[-1] - X[0]
p, q = None, None
for i in range(n):
if e[i]:
p = q
q = i
m = 0
if p != None:
for j in range(p, q):
m = max(m, X[j + 1] - X[j])
ans -= m
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NONE NONE FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NONE FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
import sys
f = sys.stdin
big = 10**15
def solve(ee, x):
ret = 0
x0 = 0
for i in range(len(ee)):
if ee[i] == 0 or x0 > 0:
dx = x[i] - x[i - 1] if i > 0 else big
x0 = max(x0, dx)
ret += dx
if ee[i] == 1:
ret -= x0
x0 = 0
return ret
t = int(f.readline())
for i in range(t):
n = int(f.readline())
ee = list(map(int, list(f.readline())[:-1]))
x = list(map(int, f.readline().split()))
print(solve(ee, x))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
import sys
T = int(sys.stdin.readline())
for i in range(T):
N = int(sys.stdin.readline())
A = sys.stdin.readline()
B = list(map(int, sys.stdin.readline().split(" ")))
Node = []
for j in range(N):
Node.append([int(A[j]), B[j]])
wire = 0
start = 0
for j in range(len(Node)):
if Node[j][0] == 1:
start = j
break
wire = Node[start][1] - Node[0][1]
end = 0
for j in range(len(Node)):
if Node[len(Node) - j - 1][0] == 1:
end = len(Node) - j - 1
break
maxim = 0
for j in range(start + 1, end + 1):
wire += Node[j][1] - Node[j - 1][1]
if maxim < Node[j][1] - Node[j - 1][1] and Node[j - 1] != 1:
maxim = Node[j][1] - Node[j - 1][1]
if Node[j][0] == 1:
wire -= maxim
maxim = 0
wire += Node[len(Node) - 1][1] - Node[end][1]
print(wire)
|
IMPORT 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
for i in range(t):
n = int(input())
s = input()
x = list(map(int, input().split()))
p = []
cum = 0
l = 0
for j in range(n):
if s[j] == "1":
p += [j]
l += 1
j = 0
k = 0
if p[k] == j:
k += 1
else:
while j < p[k]:
z = x[j + 1] - x[j]
cum += z
j += 1
k += 1
while j < n:
if k == l:
while j < n - 1:
z = x[j + 1] - x[j]
cum += z
j += 1
break
m = 0
while j < p[k]:
z = x[j + 1] - x[j]
cum += z
if m < z:
m = z
j += 1
cum -= m
k += 1
print(cum)
|
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
def func1():
n = int(input())
vs = list(input())
ds = list(map(int, input().split()))
wireLength = 0
l = []
found1 = False
if vs[0] == "1":
found1 = True
for i in range(1, n):
if vs[i] == "0":
l.append(ds[i] - ds[i - 1])
if i == n - 1:
wireLength += sum(l)
if vs[i] == "1":
if vs[i - 1] == "0":
l.append(ds[i] - ds[i - 1])
if len(l) != 0:
if found1:
wireLength += sum(l) - max(l)
else:
wireLength += sum(l)
l = []
found1 = True
return wireLength
nn = int(input())
ans = []
for _ in range(nn):
ans.append(func1())
for i in ans:
print(i)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
for _ in range(int(input())):
n = int(input())
s = list(input())
coord = list(map(int, input().split()))
p = 0
i = 0
h = []
for i in range(0, n):
if s[i] == "1":
h.append(i)
if h[0] != 0:
p = p + coord[h[0]] - coord[0]
if h[len(h) - 1] != n - 1:
p = p + coord[n - 1] - coord[h[len(h) - 1]]
for j in range(0, len(h) - 1):
if h[j] + 1 == h[j + 1]:
continue
if h[j + 1] - h[j] - 1 == 1:
p = p + min(
coord[h[j] + 1] - coord[h[j]], coord[h[j + 1]] - coord[h[j] + 1]
)
else:
y = min(
coord[h[j + 1]] - coord[h[j] + 1], coord[h[j + 1] - 1] - coord[h[j]]
)
for k in range(h[j] + 1, h[j + 1] - 1):
y = min(y, coord[k] - coord[h[j]] + coord[h[j + 1]] - coord[k + 1])
p = p + y
print(p)
|
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
while t != 0:
t -= 1
n = int(input())
s = input()
l = list(map(int, input().split()))
l1 = []
i = 1
while i < n:
l1 += [l[i] - l[i - 1]]
i += 1
k1 = s.index("1")
c = s.count("1")
ans = l[n - 1] - l[0]
while c > 1:
k2 = s.index("1", k1 + 1)
ans -= max(l1[k1:k2])
c -= 1
k1 = k2
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR LIST BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
T = int(input())
for t in range(T):
n = int(input())
a = [int(v) for v in input()]
x = [int(v) for v in input().split()]
f = s = 0
while f < n - 1:
m = 0
f1 = n - 1
for i in range(f + 1, n):
m = max(m, x[i] - x[i - 1])
if a[i]:
f1 = i
break
s = s + x[f1] - x[f]
if a[f] and a[f1]:
s = s - m
f = f1
print(s)
|
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 VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
for t in range(int(input())):
n = int(input())
cin = [int(i) for i in list(input())]
pos = [int(i) for i in input().split()]
dist = [(pos[i + 1] - pos[i]) for i in range(n - 1)]
charged = [cin.index(1)]
while True:
try:
charged.append(cin.index(1, charged[-1] + 1))
except:
break
total = pos[charged[0]] - pos[0] + pos[-1] - pos[charged[-1]]
for l, r in zip(charged[:-1], charged[1:]):
total += sum(dist[l:r]) - max(dist[l:r])
print(total)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR NUMBER WHILE NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
for _ in range(int(input())):
n = int(input())
s = input()
arr = [int(i) for i in input().split()]
ans = 0
i = 0
if s[0] == "0":
while i < len(s) - 1 and s[i] != "1":
ans += arr[i + 1] - arr[i]
i += 1
if i == len(s) - 1:
print(ans)
continue
j = n - 1
if s[n - 1] == "0":
while j > 0 and s[j] != "1":
ans += arr[j] - arr[j - 1]
j -= 1
if j == 0:
print(ans)
continue
k = i
while k < j:
if s[k + 1] == "1":
k += 1
continue
maxi = -1
while s[k + 1] != "1":
ans += arr[k + 1] - arr[k]
maxi = max(maxi, arr[k + 1] - arr[k])
k += 1
ans += arr[k + 1] - arr[k]
maxi = max(maxi, arr[k + 1] - arr[k])
if maxi != -1:
ans -= maxi
k += 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR STRING VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING WHILE VAR NUMBER VAR VAR STRING VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
ans = []
for i in range(t):
n = int(input())
marker = input()
a = list(map(int, input().split()))
sum = 0
count = 0
if marker[0] == "0":
count += 1
while marker[count] == "0":
count += 1
sum += a[count] - a[0]
count += 1
max = 0
h1 = 0
h2 = 0
flag = 0
for j in range(count, n):
if marker[j] == "0":
if marker[j - 1] == "1":
h1 = j - 1
x = a[j] - a[j - 1]
if x > max:
max = x
elif marker[j - 1] == "0":
x = a[j] - a[j - 1]
if x > max:
max = x
elif marker[j] == "1":
if marker[j - 1] == "0":
h2 = j
x = a[j] - a[j - 1]
if x > max:
max = x
sum += a[h2] - a[h1] - max
max = 0
if marker[n - 1] == "0":
sum += a[n - 1] - a[h1]
ans.append(sum)
for x in ans:
print(x)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
from sys import stdin
def calcWire(L, D):
i = 0
wire = 0
while i < len(L):
if L[i] == 0:
leftPole = i
while i < len(L) and L[i] == 0:
i += 1
rightPole = i
if leftPole == 0:
wire += D[rightPole] - D[0]
elif rightPole > len(L) - 1:
wire += D[rightPole - 1] - D[leftPole - 1]
else:
j = leftPole
optimal = 10**9 + 1
while j < rightPole:
if D[j] - D[leftPole - 1] + D[rightPole] - D[j + 1] < optimal:
optimal = D[j] - D[leftPole - 1] + D[rightPole] - D[j + 1]
j += 1
wire += min(
optimal,
D[rightPole] - D[leftPole],
D[rightPole - 1] - D[leftPole - 1],
)
else:
i += 1
return wire
t = int(stdin.readline())
while t > 0:
n = int(stdin.readline())
L = [int(x) for x in stdin.readline().strip()]
D = list(map(int, stdin.readline().split()))
print(calcWire(L, D))
t -= 1
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
ans = []
for i in range(t):
n = int(input())
s = input()
l = list(map(int, input().split()))
c = 0
t1 = 0
t2 = 0
while c < n:
if s[c] == "1":
j = c + 1
m = -float("inf")
while j < n and s[j] == "0":
t2 += l[j] - l[j - 1]
if l[j] - l[j - 1] > m:
m = l[j] - l[j - 1]
j += 1
if j < n and s[j] == "1":
t2 += l[j] - l[j - 1]
if l[j] - l[j - 1] > m:
m = l[j] - l[j - 1]
t2 -= m
t1 += t2
t2 = 0
c = j
else:
c += 1
x = 0
for z in range(n - 1):
if s[z] == "1":
break
x += l[z + 1] - l[z]
t1 += t2 + x
ans.append(t1)
for u in ans:
print(u)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR VAR VAR STRING VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR STRING VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
for _ in range(0, t):
n = int(input())
e = input()
v = list(map(int, input().split()))
sum = 0
first = 0
last = 0
i = 0
flag = False
for i in range(0, n):
if e[i] == "1":
if flag == False:
first = i
flag = True
last = i
i = 0
x = 0
y = 0
while i < first:
sum += v[i + 1] - v[i]
i += 1
while i <= last and last != 0:
sumf = 0
suml = 0
sum1 = 0
sum2 = 0
if e[i] == "1":
i += 1
x = i
y = i
continue
elif e[i] == "0":
first_flag = i - 1
next_to__first_flag = i
while e[i] != "1":
if v[next_to__first_flag] - v[first_flag] <= v[i + 1] - v[i]:
sum += v[next_to__first_flag] - v[first_flag]
first_flag = i
next_to__first_flag = i + 1
else:
sum += v[i + 1] - v[i]
i += 1
j = last + 1
while j < n:
sum += v[j] - v[j - 1]
j += 1
print(sum)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR STRING IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
T = int(input())
answers = []
for i in range(T):
N = int(input())
electricity = input()
coordinates = list(map(int, input().split()))
oneIndexes = []
for j in range(0, len(electricity)):
if electricity[j] == "1":
oneIndexes.append(j)
oneCount = len(oneIndexes)
if oneCount == 1:
total = 0
for j in range(1, N):
total = total + coordinates[j] - coordinates[j - 1]
answers.append(total)
else:
total = 0
total = total + coordinates[oneIndexes[0]] - coordinates[0]
total = total + coordinates[N - 1] - coordinates[oneIndexes[-1]]
for j in range(0, len(oneIndexes) - 1):
start = oneIndexes[j]
end = oneIndexes[j + 1]
minans = pow(10, 9)
for k in range(start, end):
x = coordinates[k]
y = coordinates[k + 1]
ans = x - coordinates[start] + coordinates[end] - y
if ans < minans:
minans = ans
total = total + minans
answers.append(total)
for answer in answers:
print(answer)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
T = int(input())
while T:
N = int(input())
valid = input()
arr = input().split(" ")
e = []
for i in range(0, len(arr)):
arr[i] = int(arr[i])
if valid[i] == "1":
e.append(i)
ans = arr[e[0]] - arr[0]
ans = ans + arr[N - 1] - arr[e[len(e) - 1]]
for i in range(0, len(e) - 1):
left = arr[e[i]]
right = arr[e[i + 1]]
res = 1000000000
for j in range(e[i], e[i + 1]):
dist1 = arr[j] - left
dist2 = right - arr[j + 1]
res = min(res, dist1 + dist2)
ans = ans + res
print(ans)
T = T - 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
for _ in range(int(input())):
n = int(input())
s = input()
arr = list(map(int, input().split()))
ans = start = 0
end = n - 1
while s[end] == "0":
end -= 1
while s[start] == "0":
start += 1
i = start
while i < end:
j = i + 1
while j <= end and s[j] == "0":
j += 1
maxdiff = 0
for k in range(i + 1, j + 1):
maxdiff = max(maxdiff, arr[k] - arr[k - 1])
ans += arr[j] - arr[i] - maxdiff
i = j
ans += arr[start] - arr[0]
ans += arr[-1] - arr[end]
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING VAR NUMBER WHILE VAR VAR STRING VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
INT_MAX = 10**18
def get_ans(start, end, maps):
ans = INT_MAX
for i in range(start, end):
temp = maps[end] - maps[i + 1] + maps[i] - maps[start]
if temp < ans:
ans = temp
return ans
t = int(input())
while t:
n = int(input())
status = input()
maps = [int(loc) for loc in input().split()]
ans = 0
indices = [i for i, x in enumerate(status) if x == "1"]
ans += maps[indices[0]] - maps[0] + (maps[-1] - maps[indices[-1]])
for i in range(len(indices) - 1):
ans += get_ans(indices[i], indices[i + 1], maps)
print(ans)
t -= 1
|
ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
for z in range(t):
n = int(input().strip())
s = input().strip()
x = [int(i) for i in input().strip().split()]
lit = [int(s[i]) for i in range(n)]
lm, rm = -1, -1
for i in range(n):
if s[i] == "1":
lm = i
break
for i in range(n - 1, -1, -1):
if s[i] == "1":
rm = i
break
ans = 0
d = {}
ans += x[lm] + x[-1] - x[rm] - x[0]
tmp = x[rm] - x[lm]
ma = 0
for i in range(lm + 1, rm + 1):
if s[i] == "1":
if x[i] - x[i - 1] > ma:
ma = x[i] - x[i - 1]
tmp -= ma
ma = 0
elif x[i] - x[i - 1] > ma:
ma = x[i] - x[i - 1]
ans += tmp
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
ans = []
for i in range(t):
d = []
light = []
xcor = []
n = int(input())
ele = input()
xcor.extend(map(int, input().split()))
l = 0
for j in range(n):
if j != n - 1:
d.append(xcor[j + 1] - xcor[j])
if ele[j] == "1":
light.append(j)
l = l + 1
sm = 0
if l > 1:
sm = sm + xcor[light[0]] - xcor[0] + xcor[n - 1] - xcor[light[l - 1]]
for j in range(l - 1):
if light[j + 1] - light[j] > 1:
sm = (
sm
+ xcor[light[j + 1]]
- xcor[light[j]]
- max(d[light[j] : light[j + 1]])
)
else:
sm = sm + xcor[light[0]] - xcor[0] + xcor[n - 1] - xcor[light[0]]
ans.append(sm)
for i in range(t):
print(ans[i])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
import sys
t = int(sys.stdin.readline())
for _ in range(t):
N = int(sys.stdin.readline())
B = [True] + [bool(int(x)) for x in sys.stdin.readline().strip()] + [True]
X = [-(10**18)] + [int(x) for x in sys.stdin.readline().split()] + [10**18]
s = 0
mx = -1
o = 0
for i in range(1, N + 2):
if mx < X[i] - X[i - 1]:
mx = X[i] - X[i - 1]
if B[i]:
if not B[i - 1]:
s += X[i] - X[o] - mx
mx = -1
o = i
print(s)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
T = int(input())
for t in range(T):
n = int(input())
s = input()
x = [int(xi) for xi in input().split(" ")]
res = 0
first = False
i = 0
while i < n:
if not first:
if s[i] == "1":
first = True
else:
res += x[i + 1] - x[i]
i += 1
elif not s[i] == "1":
j = i + 1
while j < n and not s[j] == "1":
j += 1
if j < n:
mx = 0
for k in range(i, j + 1):
mx = max(mx, x[k] - x[k - 1])
res += x[k] - x[k - 1]
res -= mx
else:
for k in range(i, n):
res += x[k] - x[k - 1]
i = j
else:
i += 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
def minDist(start, end, a):
temp = 0
for i in range(start, end):
temp = max(temp, a[i + 1] - a[i])
return a[end] - a[start] - temp
t = int(input().strip())
for __ in range(t):
n = int(input().strip())
s = input().strip()
a = [int(i) for i in input().strip().split()]
l = []
for i in range(len(s)):
if s[i] == "1":
l.append(i)
ans = 0
if 0 not in l:
ans = ans + a[l[0]] - a[0]
if n - 1 not in l:
ans = ans - a[l[-1]] + a[-1]
for i in range(len(l) - 1):
ans = ans + minDist(l[i], l[i + 1], a)
print(ans)
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
for test in range(int(input())):
n = int(input())
s = str(input())
ip = list(map(int, input().split()))
a = []
for i in range(n):
if s[i] == "1":
a.append(i)
if len(a) == 1:
ans = ip[-1] - ip[0]
else:
ans = ip[a[0]] - ip[0] + ip[-1] - ip[a[-1]]
for i in range(len(a) - 1):
m = 0
ans += ip[a[i + 1]] - ip[a[i]]
for j in range(a[i], a[i + 1]):
if ip[j + 1] - ip[j] > m:
m = ip[j + 1] - ip[j]
ans -= m
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
for i in range(t):
n = int(input())
s = input()
x = list(map(int, input().split()))
ans = 0
start = 0
end = n - 1
while s[start] == "0":
start += 1
while s[end] == "0":
end -= 1
ans += x[start] - x[0]
ans += x[n - 1] - x[end]
while start < end:
temp = start
start += 1
largest = 0
while s[start] == "0":
largest = max(largest, x[start] - x[start - 1])
start += 1
largest = max(largest, x[start] - x[start - 1])
ans += x[start] - x[temp] - largest
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
for _ in range(t):
n = int(input())
s = input()
x = [int(i) for i in input().split()]
y = 0
z = 0
l = 0
k = 0
d = {}
for i in range(len(s)):
if s[i] == "1":
d[y] = i
y = y + 1
ln = y
if d[0] != 0:
i = d[0]
l = x[i] - x[0]
for i in range(ln - 1):
y = d[i]
z = d[i + 1]
max = -90
for j in range(d[i] + 1, d[i + 1] + 1):
if max < x[j] - x[j - 1]:
max = x[j] - x[j - 1]
l = l + (x[z] - x[y]) - max
if d[ln - 1] != n - 1:
i = d[ln - 1]
l = l + (x[n - 1] - x[i])
print(l)
|
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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
t = int(input())
ret = []
while t > 0:
t -= 1
n = int(input())
s = input()
arr = list(map(int, input().split(" ")))
l = []
f = True
for i in range(len(arr)):
if s[i] == "1":
if f:
fr = i
f = False
l.append(i)
r = []
for i in range(1, len(l)):
r.append([l[i - 1], l[i]])
ans = 0
for a in r:
x = a[0]
i = x + 1
y = a[1]
curr = arr[i] - arr[x]
while i <= y:
if arr[i] - arr[i - 1] > curr:
curr = arr[i] - arr[i - 1]
i += 1
ans += abs(arr[y] - arr[x])
ans -= curr
ans += arr[fr] - arr[0]
ans += arr[len(arr) - 1] - arr[l[len(l) - 1]]
ret.append(ans)
for i in ret:
print(i)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
T = int(input())
for tt in range(T):
x, N = 0, int(input())
S = input() + "$"
A = [int(y) for y in input().split(" ")]
cost = 0
while x < N:
if S[x] == "0":
start, end = x, x
while S[end] == "0":
end += 1
if start == 0:
cost += A[end] - A[0]
elif end == N:
cost += A[N - 1] - A[start - 1]
else:
t_cost = 1000000000
for i in range(start, end + 1):
t_cost = min(t_cost, A[end] - A[i] + A[i - 1] - A[start - 1])
cost += t_cost
x = end
else:
x += 1
print(cost)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR WHILE VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
test_cases = int(input())
for i in range(test_cases):
n = int(input())
vil = input()
dis = input()
dist = dis.split()
length, ind_zero = 0, 0
if vil[0] == "0":
zero = True
else:
zero = False
for j in range(1, n):
if vil[j] == "1":
if zero == True:
if ind_zero == 0:
length += int(dist[j]) - int(dist[0])
else:
first_one = ind_zero - 1
mini = int(dist[j]) - int(dist[ind_zero])
for k in range(ind_zero, j):
len1 = int(dist[k]) - int(dist[first_one])
len2 = int(dist[j]) - int(dist[k + 1])
if len1 + len2 < mini:
mini = len1 + len2
length += mini
zero = False
elif zero == False:
zero = True
ind_zero = j
if zero == True:
length += int(dist[n - 1]) - int(dist[ind_zero - 1])
print(length)
|
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
for _ in range(int(input())):
n = int(input())
s = input()
a = [int(temp) for temp in input().strip().split(" ")]
f = 0
l = 0
sm = 0
for b in range(len(s)):
if s[b] == "1":
f = l = b
sm += a[f] - a[0]
break
for i in range(f + 1, len(s)):
if s[i] == "1":
l = i
max = 0
for k in range(f, l):
curr = a[k + 1] - a[k]
if curr > max:
max = curr
sm += a[l] - a[f] - max
f = l
sm += a[n - 1] - a[l]
print(sm)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
tests = int(input())
for _ in range(tests):
n = int(input())
s = input()
d = list(map(int, input().split()))
mdiff = -1
r, l, ans = int(), 0, 0
if s[0] == "0":
while s[l] == "0":
l += 1
ans += d[l] - d[0]
r = n - 1
if s[n - 1] == "0":
r = n - 1
while s[r] != "1":
r -= 1
ans += d[n - 1] - d[r]
i = l + 1
while i <= r:
if d[i] - d[i - 1] > mdiff:
mdiff = d[i] - d[i - 1]
if s[i] == "1":
if s[i - 1] == "0":
lt = d[i] - d[l]
ans += lt - mdiff
l = i
mdiff = -1
i += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER STRING WHILE VAR VAR STRING VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
for t in range(int(input())):
N = int(input())
s = input()
elec = list(map(int, list(s)))
villages = list(map(int, input().split()))
power = [i for i, v in enumerate(elec) if v == 1]
ans = 0
if len(power) == 1:
idx = elec.index(1)
if idx == 0 or idx == N - 1:
ans = villages[-1] - villages[0]
else:
ans = villages[idx] - villages[0] + villages[-1] - villages[idx]
else:
if elec[0] == 0:
ans += villages[elec.index(1)] - villages[0]
if elec[-1] == 0:
ans += villages[-1] - villages[s.rfind("1")]
for i in range(len(power) - 1):
min_sum = 1 << 30
for s in range(power[i], power[i + 1]):
min_sum = min(
min_sum,
villages[s]
- villages[power[i]]
+ villages[power[i + 1]]
- villages[s + 1],
)
ans += min_sum
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER NUMBER VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
for t in range(int(input())):
n = int(input())
elec = list(input().strip())
elec += ["0"]
city = i = 0
x = [int(x) for x in input().split()]
city = elec.count("1") - 1
length = x[n - 1] - x[0]
while elec[i] == "0":
i += 1
while city != 0:
mx = 0
i += 1
while elec[i] == "0":
if x[i] - x[i - 1] > mx:
mx = x[i] - x[i - 1]
i += 1
if x[i] - x[i - 1] > mx:
mx = x[i] - x[i - 1]
length -= mx
city -= 1
print(length)
|
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 FUNC_CALL VAR VAR LIST STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR STRING VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR STRING IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Problem Statement
It is 2042 and we are surrounded by cyborgs everywhere. Geneo is one such cyborg with a human body and an electronic brain. To monitor skills and health of Geneo he is posed with questions everyday. On one fine day Geneo was asked the following question.
You are given a string S of size N containing letters a to z. You are allowed to remove a character from the string and exchange it for a number signifying its position. i.e a -> 1, b -> 2, c -> 3, ... z -> 26. After some number of these operations you arrive at a final array A.
Power P of any such array is defined as:
count of letters - count of numbers + (sum of numbers/K)
where K is a given integer and '/' denotes float division.
Geneo has to find maximum possible power P_{max} of such an array.
You want to test if Geneo is right. For this you have to generate correct answers for the question.
------ Input section ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a two space separated integers N and K.
The second line of each test case contains the string S of size N.
------ Output Section ------
For each test case, output a single line containing two space seperated integers a and b such that a / b = P_{max} - the maximum possible power achievable after transformation and gcd(a,b) = 1.
------ Input constraints ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ K ≤ 1000
----- Sample Input 1 ------
2
10 6
abcdefghij
10 1
qwertyuiop
----- Sample Output 1 ------
10 1
159 1
----- explanation 1 ------
For the first test case, if we replace no character, we get an array comprising of letters only. This gives us the maximum power 10.
For the second test case, we get maximum power when we replace all the characters by respective numbers.
|
def gcd(a, b):
while b:
a, b = b, a % b
return a
def sol():
charcount, k = map(int, input().split())
intcount, sum1 = 0, 0
s = input()
for a in s:
if (ord(a) - 96) / k > 2:
intcount += 1
charcount -= 1
sum1 += ord(a) - 96
if sum1 % k == 0:
print(sum1 // k + charcount - intcount, 1)
else:
div = gcd(sum1 + k * (charcount - intcount), k)
print((sum1 + k * (charcount - intcount)) // div, k // div)
for _ in range(int(input())):
sol()
|
FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Problem Statement
It is 2042 and we are surrounded by cyborgs everywhere. Geneo is one such cyborg with a human body and an electronic brain. To monitor skills and health of Geneo he is posed with questions everyday. On one fine day Geneo was asked the following question.
You are given a string S of size N containing letters a to z. You are allowed to remove a character from the string and exchange it for a number signifying its position. i.e a -> 1, b -> 2, c -> 3, ... z -> 26. After some number of these operations you arrive at a final array A.
Power P of any such array is defined as:
count of letters - count of numbers + (sum of numbers/K)
where K is a given integer and '/' denotes float division.
Geneo has to find maximum possible power P_{max} of such an array.
You want to test if Geneo is right. For this you have to generate correct answers for the question.
------ Input section ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a two space separated integers N and K.
The second line of each test case contains the string S of size N.
------ Output Section ------
For each test case, output a single line containing two space seperated integers a and b such that a / b = P_{max} - the maximum possible power achievable after transformation and gcd(a,b) = 1.
------ Input constraints ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ K ≤ 1000
----- Sample Input 1 ------
2
10 6
abcdefghij
10 1
qwertyuiop
----- Sample Output 1 ------
10 1
159 1
----- explanation 1 ------
For the first test case, if we replace no character, we get an array comprising of letters only. This gives us the maximum power 10.
For the second test case, we get maximum power when we replace all the characters by respective numbers.
|
from sys import setrecursionlimit as limits
limits(10**5)
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
t = int(input())
while t > 0:
n, k = [int(x) for x in input().split()]
string = [x for x in input().strip()]
string.sort(reverse=True)
m, s, nums, letters, sumNumbers, whole = n, 0, 0, n, 0, n
for i in range(n):
ch = string[i]
s += ord(ch) - 96
curr = n - i - 1 - (i + 1) + s / k
if m < curr:
m = curr
whole = n - i - 1 - (i + 1)
sumNumbers = s
letters, nums = letters - 1, nums + 1
if sumNumbers % k == 0:
print(whole + sumNumbers // k, 1)
else:
sumNumbers += k * whole
_gcd = gcd(sumNumbers, k)
print(sumNumbers // _gcd, k // _gcd)
t -= 1
|
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER
|
Problem Statement
It is 2042 and we are surrounded by cyborgs everywhere. Geneo is one such cyborg with a human body and an electronic brain. To monitor skills and health of Geneo he is posed with questions everyday. On one fine day Geneo was asked the following question.
You are given a string S of size N containing letters a to z. You are allowed to remove a character from the string and exchange it for a number signifying its position. i.e a -> 1, b -> 2, c -> 3, ... z -> 26. After some number of these operations you arrive at a final array A.
Power P of any such array is defined as:
count of letters - count of numbers + (sum of numbers/K)
where K is a given integer and '/' denotes float division.
Geneo has to find maximum possible power P_{max} of such an array.
You want to test if Geneo is right. For this you have to generate correct answers for the question.
------ Input section ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a two space separated integers N and K.
The second line of each test case contains the string S of size N.
------ Output Section ------
For each test case, output a single line containing two space seperated integers a and b such that a / b = P_{max} - the maximum possible power achievable after transformation and gcd(a,b) = 1.
------ Input constraints ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ K ≤ 1000
----- Sample Input 1 ------
2
10 6
abcdefghij
10 1
qwertyuiop
----- Sample Output 1 ------
10 1
159 1
----- explanation 1 ------
For the first test case, if we replace no character, we get an array comprising of letters only. This gives us the maximum power 10.
For the second test case, we get maximum power when we replace all the characters by respective numbers.
|
t = int(input())
for it in range(t):
n, k = map(int, input().split())
clst = list(input())
clst.sort()
num = 0
den = k
pure_int = n
index = n - 1
while index >= 0 and (ord(clst[index]) - 96) / den > 2:
num += ord(clst[index]) - 96
pure_int -= 2
index -= 1
num = den * pure_int + num
for i in range(den, 1, -1):
if den % i == 0 and num % i == 0:
den //= i
num //= i
print(num, den)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Problem Statement
It is 2042 and we are surrounded by cyborgs everywhere. Geneo is one such cyborg with a human body and an electronic brain. To monitor skills and health of Geneo he is posed with questions everyday. On one fine day Geneo was asked the following question.
You are given a string S of size N containing letters a to z. You are allowed to remove a character from the string and exchange it for a number signifying its position. i.e a -> 1, b -> 2, c -> 3, ... z -> 26. After some number of these operations you arrive at a final array A.
Power P of any such array is defined as:
count of letters - count of numbers + (sum of numbers/K)
where K is a given integer and '/' denotes float division.
Geneo has to find maximum possible power P_{max} of such an array.
You want to test if Geneo is right. For this you have to generate correct answers for the question.
------ Input section ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a two space separated integers N and K.
The second line of each test case contains the string S of size N.
------ Output Section ------
For each test case, output a single line containing two space seperated integers a and b such that a / b = P_{max} - the maximum possible power achievable after transformation and gcd(a,b) = 1.
------ Input constraints ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ K ≤ 1000
----- Sample Input 1 ------
2
10 6
abcdefghij
10 1
qwertyuiop
----- Sample Output 1 ------
10 1
159 1
----- explanation 1 ------
For the first test case, if we replace no character, we get an array comprising of letters only. This gives us the maximum power 10.
For the second test case, we get maximum power when we replace all the characters by respective numbers.
|
def gcd(a, b):
while b:
a, b = b, a % b
return a
t = int(input())
for j in range(t):
n, k = map(int, input().split(" ", 2)[:2])
s = input()
sum1 = 0
icnt = 0
ccnt = n
for a in s:
if (ord(a) - 96) / k > 2:
icnt += 1
ccnt -= 1
sum1 += ord(a) - 96
if sum1 % k == 0:
ans = sum1 // k + ccnt - icnt
print(ans, 1)
else:
gc = gcd(sum1 + k * (ccnt - icnt), k)
ans = (sum1 + k * (ccnt - icnt)) // gc
print(ans, k // gc)
|
FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.