description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
def getClosest(h, c, st, en, t):
if abs(val(h, c, st, t) / (2 * st - 1)) <= abs(val(h, c, en, t) / (2 * en - 1)):
return st
else:
return en
def val(h, c, x, t):
return x * h + (x - 1) * c - (2 * x - 1) * t
def findClosest(h, c, st, en, t):
if val(h, c, st, t) <= 0:
return st
if val(h, c, en, t) >= 0:
return en
i = st
j = en
mid = 0
while i < j:
mid = (i + j) // 2
if val(h, c, mid, t) == 0:
return mid
if val(h, c, mid, t) < 0:
if mid > st and val(h, c, mid - 1, t) > 0:
return getClosest(h, c, mid - 1, mid, t)
j = mid
else:
if mid < en and val(h, c, mid + 1, t) < 0:
return getClosest(h, c, mid, mid + 1, t)
i = mid + 1
return mid
for _ in range(int(input())):
h, c, t = map(int, input().split())
if 2 * t <= h + c:
print(2)
elif t == h:
print(1)
else:
x = 1
while x * h + (x - 1) * c > (2 * x - 1) * t:
x = x << 1
st = x >> 1
st = st - 1
en = x + 2
cx = findClosest(h, c, st, en, t)
print(2 * cx - 1)
|
FUNC_DEF IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN 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 IF BIN_OP NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
t = int(input())
def main():
hot, cold, temp = map(int, input().split())
if temp <= (hot + cold) // 2:
return 2
k = (hot - temp) // (2 * temp - hot - cold)
l = abs(k * (hot + cold) + hot - temp * (2 * k + 1)) * (2 * k + 3)
u = abs((k + 1) * (hot + cold) + hot - temp * (2 * k + 3)) * (2 * k + 1)
if l <= u:
return 2 * k + 1
return 2 * k + 3
for _ in range(t):
print(main())
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
T = int(input())
C = []
for i in range(0, T):
h, c, t = [int(x) for x in input().split(" ")]
if h + c == 2 * t:
C.append(2)
elif 2 * t < h + c:
if abs((h + c) / 2 - t) < abs(h - t):
C.append(2)
else:
C.append(1)
else:
m = (h - t) // (2 * t - h - c)
if abs(
((m + 2) * h + (m + 1) * c) * (2 * m + 1) - t * (2 * m + 1) * (2 * m + 3)
) >= abs(((m + 1) * h + m * c) * (2 * m + 3) - t * (2 * m + 3) * (2 * m + 1)):
C.append(2 * m + 1)
else:
C.append(2 * m + 3)
for i in C:
print(i)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
def temp(h, c, n):
return h * (n + 1) + c * n, 2 * n + 1
for _ in range(int(input())):
h, c, t = map(int, input().split())
if t * 2 <= h + c:
print(2)
continue
n = (h - t) // (2 * t - h - c)
n1 = n + 1
a, b = temp(h, c, n)
a1, b1 = temp(h, c, n1)
d, dn = abs(a - b * t), b
d1, dn1 = abs(a1 - b1 * t), b1
if d * dn1 > d1 * dn:
ans = n1
else:
ans = n
print(2 * ans + 1)
|
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
def cups(x, h, c, t):
error = (x + 1) * h + x * c - t * (2 * x + 1)
return abs(error)
def f(h, c, t):
if h + c >= 2 * t:
return 2
x = (h - t) // (2 * t - h - c)
e1 = cups(x, h, c, t)
e2 = cups(x + 1, h, c, t)
if e1 * (2 * x + 3) <= e2 * (2 * x + 1):
return 2 * x + 1
else:
return 2 * x + 3
T = int(input())
for t in range(T):
h, c, t = map(int, input().rstrip().split())
print(f(h, c, t))
|
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP VAR VAR BIN_OP NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
printn = lambda x: print(x, end="")
inn = lambda: int(input())
inl = lambda: list(map(int, input().split()))
inm = lambda: map(int, input().split())
ins = lambda: input().strip()
DBG = True
BIG = 10**18
R = 10**9 + 7
def ddprint(x):
if DBG:
print(x)
def foo(h, c, n):
return (n * h + (n - 1) * c) / (2 * n - 1)
def bar(h, c, t, n):
return n * h + (n - 1) * c == t * (2 * n - 1)
ttt = inn()
for tt in range(ttt):
h, c, t = inm()
if 5 * h + c <= 6 * t:
print(1)
elif 2 * t <= h + c:
print(2)
else:
mn = 1
mx = 10**16
found = False
while mx - mn > 1:
mid = (mn + mx) // 2
if bar(h, c, t, mid):
found = True
break
elif foo(h, c, mid) < t:
mx = mid
else:
mn = mid
if found:
print(2 * mid - 1)
continue
x = 2 * (2 * mn - 1) * (2 * mx - 1) * t
y = (2 * mx - 1) * (mn * h + (mn - 1) * c) + (2 * mn - 1) * (
mx * h + (mx - 1) * c
)
if x < y:
print(2 * mx - 1)
else:
print(2 * mn - 1)
|
ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
for _ in range(int(input())):
h, c, t = map(int, input().split())
h -= c
t -= c
if h == t:
print(1)
elif h / 2 >= t:
print(2)
else:
def p(a):
return h * (1 + a)
def q(a):
return 1 + 2 * a
a = 0
b = 10**10
while a + 1 != b:
mid = (a + b) // 2
val = p(mid)
T = t * q(mid)
if val == T:
print(1 + 2 * mid)
break
if val < T:
b = mid
else:
a = mid
if val != T:
if 2 * t * q(a) * q(b) >= p(a) * q(b) + p(b) * q(a):
print(1 + 2 * a)
else:
print(1 + 2 * b)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN BIN_OP VAR BIN_OP NUMBER VAR FUNC_DEF RETURN BIN_OP NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR IF BIN_OP BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
T = int(input())
for t in range(T):
h, c, t = map(int, input().split())
h *= 2
c *= 2
t *= 2
m = (h + c) // 2
if t <= m:
print(2)
else:
hc, tc = h - m, t - m
tms = hc // tc
apr = [2434841, 1]
for i in range(tms - 5, tms + 5):
if i % 2 and abs(tc - hc / i) < apr[0]:
apr = [abs(tc - hc / i), i]
print(apr[1])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
for i in range(int(input())):
h, c, t = map(int, input().split())
result = float("inf")
avg = (h + c) / 2
D = {}
if t == h:
result = 1
elif t in (c, avg):
result = 2
else:
D[abs(t - c)] = 2
D[abs(t - h)] = 1
if t - avg > 0:
X = (h - avg) / (t - avg)
if X < 3:
X = (h - avg) / 3
x = 3
elif int(X) & 1 == 0:
x = int(X) + 1
X = (h - avg) / (int(X) + 1)
else:
x = int(X)
X = (h - avg) / x
X1 = abs(t - avg - (h - avg) / x)
X2 = t - avg - (h - avg) / (x + 2)
if X1 > X2:
X = (h - avg) / (x + 2)
x += 2
X = t - avg - X
if X != abs(t - h):
D[X] = x
elif t - avg < 0:
X = abs(t - avg)
D[X] = 2
result = D[min(D.keys())]
print(result)
|
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 STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR DICT IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
q = int(input())
for i in range(q):
h, c, t = list(map(int, input().split()))
if 2 * t <= h + c:
print(2)
else:
k = (h - t) / (2 * t - h - c)
m = int(k)
n = m + 1
x = abs((m + 1) * h + m * c - (2 * m + 1) * t) / (2 * m + 1)
y = abs((n + 1) * h + n * c - (2 * n + 1) * t) / (2 * n + 1)
if x <= y:
print(2 * m + 1)
else:
print(2 * n + 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
import sys
input = sys.stdin.readline
flush = sys.stdout.flush
for _ in range(int(input())):
h, c, t = list(map(int, input().split()))
m = h + c >> 1
if t <= m:
print(2)
continue
a = (h - t) // (2 * t - h - c)
b = a + 1
print(
2 * a + 1
if 2 * t * (2 * a + 1) * (2 * b + 1)
>= (2 * b + 1) * ((a + 1) * h + a * c) + (2 * a + 1) * ((b + 1) * h + b * c)
else 2 * b + 1
)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR 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 BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
for _ in range(int(input())):
h, c, t = map(int, input().split())
if 2 * t <= h + c:
print(2)
continue
x = (h - t) // (2 * t - h - c)
k = 2 * x + 1
val1 = abs(k // 2 * c + (k + 1) // 2 * h - t * k)
val2 = abs((k + 2) // 2 * c + (k + 3) // 2 * h - t * (k + 2))
print(k if val1 * (k + 2) <= val2 * k else k + 2)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
T = int(input())
s = []
for i in range(T):
s.append(input())
for i in range(T):
h, c, t = map(int, s[i].split())
if t == h:
n = 1
elif t <= (h + c) / 2:
n = 2
else:
k = (c - t) / (h + c - 2 * t)
n = int(2 * k - 1)
if n % 2 == 0:
n = n + 1
k = (n + 1) / 2
tb = (k * h + (k - 1) * c) / n
n1 = n - 2
k = (n1 + 1) / 2
tb1 = (k * h + (k - 1) * c) / n1
n2 = n + 2
k = (n2 + 1) / 2
tb2 = (k * h + (k - 1) * c) / n2
if abs(t - tb1) < abs(t - tb) and abs(t - tb1) < abs(t - tb2):
n = n1
if abs(t - tb2) < abs(t - tb) and abs(t - tb2) < abs(t - tb1):
n = n2
print(n)
|
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 FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
h, c, t = list(map(int, input().split()))
if h + c >= 2 * t:
print(2)
else:
diff2 = 2 * t - (h + c)
hDiff2 = 2 * h - (h + c)
kDown = (hDiff2 // diff2 - 1) // 2
kUp = kDown + 1
diffDown = abs(diff2 - hDiff2 / (2 * kDown + 1))
diffUp = abs(diff2 - hDiff2 / (2 * kUp + 1))
if diffDown <= diffUp:
print(2 * kDown + 1)
else:
print(2 * kDown + 3)
|
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
t = int(input())
for test in range(t):
inp = input().split()
h = int(inp[0])
c = int(inp[1])
t = int(inp[2])
if t + t <= c + h:
print(2)
continue
if t >= h:
print(1)
continue
k = (h - t) // (t + t - c - h)
if (h - t) % (t + t - c - h) == 0:
print(k + k + 1)
continue
k += 1
if (k + k + 1) * (k * h + k * c - c) + (k + k - 1) * (k * h + k * c + h) > (
t + t
) * (k + k - 1) * (k + k + 1):
print(k + k + 1)
else:
print(k + k - 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
T = int(input().strip())
eps = 1e-20
def diff(n):
return abs(((n + 1) * h + n * c) / (2 * n + 1) - t)
def better(k, n):
return (2 * k + 1) * abs((n + 1) * (h - t) + n * (c - t)) >= (2 * n + 1) * abs(
(k + 1) * (h - t) + k * (c - t)
)
def strictlybetter(k, n):
return (2 * k + 1) * abs((n + 1) * (h - t) + n * (c - t)) > (2 * n + 1) * abs(
(k + 1) * (h - t) + k * (c - t)
)
for t in range(T):
h, c, t = list(map(int, input().split()))
if t >= h or h == c:
print(1)
continue
elif 2 * t <= h + c:
print(2)
continue
bestn = (h - t) // (2 * t - h - c)
bestn = int(bestn)
curdif = diff(bestn)
while bestn > 0 and better(bestn - 1, bestn):
bestn -= 1
while strictlybetter(bestn + 1, bestn):
bestn += 1
curdif = diff(bestn)
if abs(h - t) <= curdif:
print(1)
continue
if abs((h + c) / 2 - t) <= curdif:
print(2)
continue
print(2 * bestn + 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
for i in range(int(input())):
h, c, t = map(int, input().split())
if h + c >= 2 * t:
print(2)
elif h == t:
print(1)
else:
x = int((t - c) / (2 * t - h - c))
tb1 = abs((2 * x - 1) * t - ((h + c) * x - c)) * (2 * x + 1)
tb2 = abs(t * (2 * x + 1) - ((h + c) * x + h)) * (2 * x - 1)
if tb2 < tb1:
z = x + 1 + x
else:
z = x + x - 1
print(z)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
import sys
input = sys.stdin.readline
def print(val):
sys.stdout.write(str(val) + "\n")
def val(x, h, c):
return (h - c) / (2 * (x * 2 - 1))
def prog():
for _ in range(int(input())):
L = 1
R = 10**12
h, c, t = map(int, input().split())
if t <= (h + c) / 2:
print(2)
else:
t -= (h + c) / 2
while R > L:
m = (R + L) // 2
temp = val(m, h, c)
if temp > t:
L = m + 1
else:
R = m - 1
left = val(L, h, c)
if left > t:
right = val(L + 1, h, c)
if abs(t - left) <= abs(t - right):
print(2 * L - 1)
else:
print(2 * L + 1)
else:
right = val(L - 1, h, c)
if abs(t - left) >= abs(t - right):
print(2 * L - 3)
else:
print(2 * L - 1)
prog()
|
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
import sys
[t] = [int(i) for i in sys.stdin.readline().split()]
for _ in range(t):
[h, c, t] = [int(i) for i in sys.stdin.readline().split()]
h -= c
t -= c
if 2 * t <= h:
print(2)
else:
k1 = t // (2 * t - h)
k2 = k1 + 1
t1 = h * k1 * (2 * k2 - 1)
diff1 = abs(t * (2 * k1 - 1) * (2 * k2 - 1) - t1)
t2 = h * k2 * (2 * k1 - 1)
diff2 = abs(t * (2 * k1 - 1) * (2 * k2 - 1) - t2)
if diff1 <= diff2:
print(2 * k1 - 1)
else:
print(2 * k2 - 1)
|
IMPORT ASSIGN LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
def check(h, c, k, l, t):
if l == -1:
return abs(((k + 1) * h + k * c) / (2 * k + 1) - t)
T = abs((k + 1) * h + k * c - t * (2 * k + 1)) * (2 * l + 1) - abs(
(l + 1) * h + l * c - t * (2 * l + 1)
) * (2 * k + 1)
return T
def solve(h, c, t):
m1 = abs((h + c) // 2 - t)
if t <= (h + c) // 2:
return 2
left = 0
right = int((t - h) / (h + c - 2 * t)) + 2
while left < right:
mid = (left + right) // 2
x = check(h, c, mid - 1, mid, t)
y = check(h, c, mid, mid + 1, t)
if x > 0 and y <= 0:
left = mid
right = mid
elif 0 < y and x > 0:
left = mid + 1
else:
right = mid - 1
m2 = check(h, c, left, -1, t)
if m1 == min(m1, m2):
return 2
else:
return 2 * left + 1
test = 1
test = int(input())
for t in range(0, test):
h, c, t = [int(x) for x in input().split()]
print(solve(h, c, t))
|
FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
import sys
input = lambda: sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
h, c, t = map(int, input().split())
a = h - t
b = t - c
if a <= 0:
print(1)
elif a >= b:
print(2)
else:
mi = a, 1
mii = 1
if abs((a + b) * mi[1]) < mi[0] * 2:
mi = abs(a + b), 2
mii = 2
s = max(b // (b - a), 1)
for x in range(s, s + 2):
t = abs(a * x - b * (x - 1))
if t * mi[1] < mi[0] * (2 * x - 1):
mi = t, 2 * x - 1
mii = x * 2 - 1
print(mii)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL 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 BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
T = int(input())
for _ in range(T):
h, c, t = list(map(int, input().split()))
if h + c >= t * 2:
print(2)
else:
l, r = 0, 10**9
while r - l > 1:
m = (l + r) // 2
if h * (m + 1) + c * m >= t * (2 * m + 1):
l = m
else:
r = m
if (2 * l + 1) * ((2 * r + 1) * t - (h * (r + 1) + c * r)) >= (2 * r + 1) * (
h * (l + 1) + c * l - (2 * l + 1) * t
):
print(l * 2 + 1)
else:
print(r * 2 + 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
T = int(input())
for iteration in range(T):
[h, c, t] = list(map(int, input().split()))
if t <= (h + c) / 2:
print(2)
else:
k = 1 / 2 * (h - c) / (2 * t - h - c)
k = k + 1 / (4 * (int(k) + 1))
if k - int(k) <= 10**-8:
k = int(k) - 1
else:
k = int(k)
print(2 * k + 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
_ = int(input())
h = 0
c = 0
t = 0
def fn(cnt):
return t * (2 * cnt - 1) - cnt * h - (cnt - 1) * c
for __ in range(_):
h, c, t = [int(a) for a in input().split()]
if h + c >= t * 2:
print(2)
continue
l = 1
r = 1
while fn(r) < 0:
r *= 2
l = r // 2
while l < r:
mid = (l + r) // 2
if fn(mid) < 0:
l = mid + 1
else:
r = mid
if abs(fn(l)) * (2 * l - 3) < abs(fn(l - 1)) * (2 * l - 1):
print(2 * l - 1)
else:
print(2 * l - 3)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
for i in range(int(input())):
a, b, c = map(int, input().split())
d = a - c
e = c - b
f = e - d
if d >= e:
print(2)
elif d == 0:
print(1)
elif 2 <= e / d:
if c * 3 - (2 * a + b) >= (a - c) * 3:
print(1)
else:
print(3)
else:
g = round((d + e) / (2 * (e - d)))
x1 = abs((g * b + (g + 1) * a) * (2 * g - 1) - (2 * g - 1) * (2 * g + 1) * c)
x2 = abs(((g - 1) * b + g * a) * (2 * g + 1) - (2 * g - 1) * (2 * g + 1) * c)
if x2 <= x1:
print(2 * g - 1)
else:
print(2 * g + 1)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER BIN_OP VAR VAR IF BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
def N():
return int(input())
def NM():
return map(int, input().split())
def L():
return list(NM())
def LN(n):
return [N() for i in range(n)]
def LL(n):
return [L() for i in range(n)]
T = N()
def f():
h, c, t = NM()
h -= c
t -= c
if h == t:
print(1)
return
elif t * 2 <= h:
print(2)
return
lo = 0
hi = 10**6 * 2
while hi >= lo:
mi = (hi + lo) // 2
if sa(mi, t, h) > sa(mi + 1, t, h):
lo = mi + 1
else:
hi = mi - 1
if sa(lo, t, h) <= sa(lo + 1, t, h):
print(lo * 2 + 1)
else:
print((lo + 1) * 2 + 1)
return
def sa(k, t, h):
return abs((h * (k + 1) - t * (2 * k + 1)) / ((2 * k + 1) * h))
for i in range(T):
f()
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
def main(h, c, t):
lowtmp = (h + c) / 2
if t == h:
return 1
if t <= lowtmp:
return 2
def tempSum(n):
return n * h + (n - 1) * c
l = 1
r = 10**7
while l < r:
mid = (l + r) // 2
if tempSum(mid) <= t * (2 * mid - 1):
r = mid
else:
l = mid + 1
ltemp = tempSum(l)
lltemp = tempSum(l - 1)
ln = 2 * l - 1
lln = 2 * l - 3
if abs(ltemp * lln - t * ln * lln) >= abs(lltemp * ln - t * ln * lln):
return 2 * (l - 1) - 1
else:
return 2 * l - 1
N = int(input())
for _ in range(N):
h, c, t = list(map(int, input().split(" ")))
print(main(h, c, t))
|
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
import sys
def input():
return sys.stdin.readline().rstrip()
testcases = int(input())
answers = []
def loss(two_n_plus_one, hot, cold, desired):
n = two_n_plus_one // 2
return abs((desired * (2 * n + 1) - ((n + 1) * hot + n * cold)) / (2 * n + 1))
for _ in range(testcases):
hot, cold, desired_temp = [int(i) for i in input().split()]
mid_way = (hot + cold) / 2
if hot == cold:
answers.append(1)
elif desired_temp >= hot:
answers.append(1)
elif desired_temp <= mid_way:
answers.append(2)
else:
frac = (hot - desired_temp) / (desired_temp - mid_way)
frac /= 2
option1 = 2 * int(frac) + 1
option2 = option1 + 2
l1, l2 = loss(option1, hot, cold, desired_temp), loss(
option2, hot, cold, desired_temp
)
if (
min(l1, l2) >= hot - desired_temp
and hot - desired_temp <= desired_temp - mid_way
):
answers.append(1)
elif min(l1, l2) >= desired_temp - mid_way:
answers.append(2)
elif l1 <= l2:
answers.append(option1)
else:
answers.append(option2)
print(*answers, sep="\n")
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
def count(h, c, t):
if h + c >= 2 * t:
ans = 2
else:
k = int((h - t) / (2 * t - h - c))
if 4 * k * (k + 1) * (h + c) + (6 * k + 5) * h + c * (2 * k + 1) <= 2 * t * (
2 * k + 1
) * (2 * k + 3):
ans = 2 * k + 1
else:
ans = 2 * k + 3
return ans
t = int(input())
for _ in range(t):
h, c, t = map(int, input().split())
print(count(h, c, t))
|
FUNC_DEF IF BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR IF BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
T = int(input())
for t_itr in range(T):
H, C, T = list(map(int, input().rstrip().split()))
if T >= H:
print(1)
elif 2 * T <= H + C:
print(2)
else:
n = (C - T) // (H + C - 2 * T)
n1 = n + 1
d1 = H * n + C * (n - 1) - T * (2 * n - 1)
d2 = H * n1 + C * (n1 - 1) - T * (2 * n1 - 1)
if abs(d1 / (2 * n - 1)) <= abs(d2 / (2 * n1 - 1)):
print(2 * n - 1)
else:
print(2 * n1 - 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
def i_by_t(h, c, t):
return ((h - c) / (t - (h + c) / 2) + 2) / 2
for _ in range(int(input())):
h, c, t = map(int, input().split())
mean = (h + c) / 2
def temp(i):
half = i // 2
return (h * (i - i // 2) + c * half) / i
T = temp(1)
if t >= T:
print(1)
continue
best_eps = abs(T - t)
T = temp(2)
if t <= T:
print(2)
continue
best_i = int(i_by_t(h, c, t))
if best_i % 2 == 0:
best_i -= 1
i = best_i
best_i = 1
while True:
T = temp(i)
eps = abs(T - t)
if eps < best_eps:
best_i = i
if eps <= best_eps:
best_eps = eps
else:
print(best_i)
break
i += 2
|
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
def check(h, c, x, t):
return abs((x * (h + c - 2 * t) + t - c) / (2 * x - 1))
def answer(h, c, t):
avg = (h + c) // 2
if t <= avg:
return 2
x = (t - c) // (2 * t - h - c)
if check(h, c, x, t) <= check(h, c, x + 1, t):
return 2 * x - 1
else:
return 2 * x + 1
t = int(input())
for i in range(t):
h, c, t = map(int, input().split())
print(answer(h, c, t))
|
FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
t = int(input())
for _ in range(t):
h, c, t = map(int, input().split())
if t >= h:
print(1)
continue
if (h + c) % 2 == 0 and t == (h + c) // 2:
print(2)
continue
if t < (h + c) / 2:
print(2)
continue
n = (t - c) // (2 * t - h - c)
if (t - c) % (2 * t - h - c) == 0:
print(2 * n - 1)
continue
n0 = n
n1 = n + 1
t0 = abs(t * (2 * n0 - 1) * (2 * n1 - 1) - (n0 * h + (n0 - 1) * c) * (2 * n1 - 1))
t1 = abs(t * (2 * n0 - 1) * (2 * n1 - 1) - (n1 * h + (n1 - 1) * c) * (2 * n0 - 1))
if t0 <= t1:
print(2 * n0 - 1)
else:
print(2 * n1 - 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
inp = lambda cast=int: [cast(x) for x in input().split()]
printf = lambda s="", *args, **kwargs: print(str(s).format(*args), flush=True, **kwargs)
(t,) = inp()
for _ in range(t):
h, c, t = inp()
if 2 * t <= h + c:
print(2)
else:
x = (t - c) // (2 * t - h - c)
y = x + 1
res1 = (x * h + (x - 1) * c) * (2 * y - 1) - t * (2 * x - 1) * (2 * y - 1)
res2 = t * (2 * x - 1) * (2 * y - 1) - (y * h + (y - 1) * c) * (2 * x - 1)
if res1 <= res2:
print(2 * x - 1)
else:
print(2 * y - 1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF BIN_OP NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
for case in range(int(input())):
a = input().split(" ")
h = int(a[0])
c = int(a[1])
t = int(a[2])
avg = (h + c) / 2
if t < avg:
ans = 2
elif t == avg:
ans = 2
elif t == h:
ans = 1
elif h > t > avg:
diff = t - avg
v = (h - avg) // diff
if v % 2 == 0:
v1 = (h - avg) / (v + 1)
v2 = (h - avg) / (v - 1)
d1 = abs(diff - v1)
d2 = abs(diff - v2)
if d1 >= d2:
ans = v - 1
else:
ans = v + 1
else:
v1 = (h - avg) / (v - 2)
v2 = (h - avg) / v
v3 = (h - avg) / (v + 2)
d1 = abs(diff - v1)
d2 = abs(diff - v2)
d3 = abs(diff - v3)
if d1 <= d2 and d1 <= d3:
ans = v - 2
elif d2 <= d1 and d2 <= d3:
ans = v
else:
ans = v + 2
else:
ans = 1
print(int(ans))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$).
You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\dots$ and so on $\dots$
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 3 \cdot 10^4$) — the number of testcases.
Each of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \le c < h \le 10^6$; $c \le t \le h$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
-----Output-----
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.
-----Example-----
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
-----Note-----
In the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.
In the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.
In the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$.
|
for T in range(int(input())):
h, c, x = list(map(int, input().split()))
if h == x:
print(1)
else:
av = (h + c) / 2
if av >= x:
print(2)
else:
n = (h - x) // (2 * x - h - c)
p = n + 1
temp = ((h + c) * n + h) * (2 * p + 1)
temp1 = ((h + c) * p + h) * (2 * n + 1)
if 2 * x * (2 * p + 1) * (2 * n + 1) >= temp + temp1:
print(2 * n + 1)
else:
print(2 * p + 1)
|
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 IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = int(input())
name = []
for i in range(n):
x = input()
name.append(x + (101 - len(x)) * "{")
G = [[] for i in range(26)]
G2 = [[] for i in range(26)]
def f(s):
return ord(s) - ord("a")
flag = True
for i in range(1, n):
for j in range(100):
if name[i][j] != name[i - 1][j]:
if "{" == name[i][j]:
flag = False
break
elif "{" == name[i - 1][j]:
break
G[f(name[i - 1][j])].append(f(name[i][j]))
G2[f(name[i][j])].append(f(name[i - 1][j]))
break
L = []
S = []
for i in range(26):
if G2[i] == []:
S.append(i)
while S != []:
node = S.pop(0)
L.append(node)
for i in G[node]:
G2[i].remove(node)
if G2[i] == []:
S.append(i)
if G2 == [[] for i in range(26)] and len(L) == 26 and flag:
for i in L:
print(chr(ord("a") + i), end="")
print()
else:
print("Impossible")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF STRING VAR VAR VAR ASSIGN VAR NUMBER IF STRING VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR LIST EXPR FUNC_CALL VAR VAR WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR LIST EXPR FUNC_CALL VAR VAR IF VAR LIST VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
def go_through(given_char):
global order
global freq
global orders
if given_char in orders:
while orders[given_char]:
next_letter = orders[given_char].pop()
freq[next_letter] = freq[next_letter] - 1
go_through(next_letter)
if given_char not in order:
order.append(given_char)
num_names = int(input())
names = []
for _ in range(num_names):
curr_name = input()
names.append(curr_name)
prev_name = names[0]
possible = True
orders = {}
freq = {}
change_letters = set()
freq[prev_name[0]] = 0
for index in range(1, len(names)):
name = names[index]
char_index = 0
while (
char_index < len(name)
and char_index < len(prev_name)
and name[char_index] == prev_name[char_index]
):
char_index += 1
if char_index < len(name) and char_index < len(prev_name):
previous_char = prev_name[char_index]
curr_char = name[char_index]
if previous_char in orders:
orders[previous_char].add(curr_char)
else:
orders[previous_char] = {curr_char}
if curr_char in freq:
freq[curr_char] = freq[curr_char] + 1
else:
freq[curr_char] = 1
change_letters.add(previous_char)
change_letters.add(curr_char)
prev_name = name
elif len(prev_name) > len(name):
possible = False
break
if not possible:
print("Impossible")
else:
order = []
dependent_chars = list(orders.keys())
for char in dependent_chars:
curr_freq = 0
if char in freq:
curr_freq = freq[char]
if curr_freq <= 0:
go_through(char)
if len(order) is not len(change_letters):
print("Impossible")
else:
order = list(reversed(order))
for char in range(ord("a"), ord("z") + 1):
character = chr(char)
if character not in order:
order.append(character)
for char in order:
print(char, end="")
|
FUNC_DEF IF VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = int(input())
ss = [input() for i in range(n)]
graph = set()
def rec(fr=0, to=len(ss) - 1, dep=0):
if fr == to:
return
pos = fr
while pos <= to:
i = pos
while (
1 + i <= to
and len(ss[i]) > dep
and len(ss[1 + i]) > dep
and ss[i][dep] == ss[1 + i][dep]
):
i += 1
for t in range(1 + pos, 1 + i):
if len(ss[t - 1]) > 1 + dep and len(ss[t]) > 1 + dep:
t1, t2 = ss[t - 1][1 + dep], ss[t][1 + dep]
if t1 != t2:
t3 = t1, t2
graph.add(t3)
rec(pos, i, 1 + dep)
pos = 1 + i
for t in range(1 + fr, 1 + to):
if len(ss[t - 1]) > dep and len(ss[t]) > dep:
t1, t2 = ss[t - 1][dep], ss[t][dep]
if t1 != t2:
t3 = t1, t2
graph.add(t3)
for i in range(n):
for j in range(1 + i, n):
if ss[i].startswith(ss[j]) and ss[i] != ss[j]:
print("Impossible")
exit()
rec()
deg = dict()
for c1, c2 in graph:
if c2 not in deg:
deg[c2] = 0
deg[c2] += 1
k = 0
ans = ""
for i in range(26):
for c in "abcdefghijklmnopqrstuvwxyz":
if c not in deg or deg[c] == 0:
deg[c] = -1
ans += c
k += 1
for c1, c2 in graph:
if c1 == c:
deg[c2] -= 1
if k != 26:
print("Impossible")
else:
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR RETURN ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR WHILE BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR STRING IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
__author__ = "Utena"
n = int(input())
e = []
def cmp(a, b):
for i in range(min(len(a), len(b))):
if a[i] != b[i]:
e.append((a[i], b[i]))
return 0
if len(a) > len(b):
print("Impossible")
exit(0)
def indegree0(v, e):
if v == []:
return None
tmp = v[:]
for i in e:
if i[1] in tmp:
tmp.remove(i[1])
tmp = tmp[:1]
if tmp == []:
return -1
for t in tmp:
for i in range(len(e)):
if t in e[i]:
e[i] = 0, 0
if e:
eset = set(e)
if (0, 0) in eset:
eset.remove((0, 0))
e[:] = list(eset)
if v:
for t in tmp:
v.remove(t)
return tmp
def topoSort(v, e):
result = []
while True:
nodes = indegree0(v, e)
if nodes == None:
break
if nodes == -1:
print("Impossible")
return None
result.extend(nodes)
print("".join(map(str, result)))
return result
names = []
for i in range(n):
names.append(input())
for i in range(n - 1):
cmp(names[i], names[i + 1])
afs = [chr(i) for i in range(97, 123)]
topoSort(afs, e)
|
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR LIST RETURN NONE ASSIGN VAR VAR FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR LIST RETURN NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = int(input())
s = [input() for i in range(n)]
m = [([0] * 26) for i in range(26)]
f = lambda x: ord(x) - ord("a")
for i in range(n - 1):
for ch1, ch2 in zip(s[i], s[i + 1]):
if ch1 != ch2:
m[f(ch1)][f(ch2)] = 1
break
else:
if len(s[i]) > len(s[i + 1]):
print("Impossible")
exit()
indeg = [sum(m[j][i] for j in range(26)) for i in range(26)]
added = [0] * 26
ans = []
while 1:
for i in range(26):
if indeg[i] == 0 and not added[i]:
ans.append(chr(i + ord("a")))
added[i] = 1
for j in range(26):
if m[i][j]:
m[i][j] = 0
indeg[j] -= 1
break
else:
break
if any(indeg):
print("Impossible")
exit()
print("".join(ans))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST WHILE NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
imp = False
nodes = {}
for i in "abcdefghijklmnopqrstuvwxyz":
nodes[i] = set()
n = int(input())
prev = None
for i in range(n):
name = input()
if prev:
for x, y in zip(name, prev):
if x != y:
nodes[x].add(y)
break
else:
if prev > name:
imp = True
break
prev = name
q = []
r = []
for i in "abcdefghijklmnopqrstuvwxyz":
if not nodes[i]:
q.append(i)
else:
r.append(i)
l = []
while q:
e = q.pop()
l.append(e)
for i in r[:]:
if e in nodes[i]:
nodes[i].remove(e)
if not nodes[i]:
q.append(i)
r.remove(i)
if r or imp:
print("Impossible")
else:
print("".join(l))
|
ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
vis = [0] * 26
ans = []
graph = [[] for _ in range(26)]
A = ord("a")
def topo(i):
s = [i]
while s:
i = s.pop()
if vis[i] == 0:
s.append(i)
vis[i] = 1
for j in graph[i]:
if vis[j] == 1:
return False
elif vis[j] == 0:
s.append(j)
elif vis[i] == 1:
vis[i] = 2
ans.append(chr(i + A))
return True
def topo1(i):
vis[i] = 1
for j in graph[i]:
if vis[j] == 1:
return False
elif vis[j] == 0:
if not topo(j):
return False
vis[i] = 2
ans.append(chr(i + A))
return True
def solve():
t = int(input())
names = []
for _ in range(t):
names.append(input())
exist = True
for k in range(t - 1):
i = 0
while True:
if len(names[k]) == i:
break
elif len(names[k + 1]) == i:
exist = False
break
elif names[k][i] != names[k + 1][i]:
a, b = ord(names[k][i]) - A, ord(names[k + 1][i]) - A
graph[a].append(b)
break
else:
i += 1
if exist:
for i in range(26):
if vis[i] == 0:
exist = topo(i)
if not exist:
break
if exist:
print("".join(reversed(ans)))
if not exist:
print("Impossible")
solve()
|
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
def do_job():
def order_info(s1, s2):
smaller = None
larger = None
for i in range(min(len(s1), len(s2))):
if s1[i] != s2[i]:
smaller = s1[i]
larger = s2[i]
break
return smaller, larger
n = int(input())
list_of_string = []
for _ in range(n):
list_of_string.append(input())
g = {x: set() for x in "abcdefghijklmnopqrstuwvxyz"}
for ind in range(0, n - 1):
small, big = order_info(list_of_string[ind], list_of_string[ind + 1])
if big is not None:
g[big].add(small)
if big is None and len(list_of_string[ind]) > len(list_of_string[ind + 1]):
return "Impossible"
q = {x for x in g if g[x] == set()}
order_list = []
while len(q) > 0:
elem = q.pop()
order_list.append(elem)
for x in g:
if elem in g[x]:
g[x].remove(elem)
q = {x for x in g if g[x] == set() and x not in order_list}
if len(order_list) == 26:
return "".join(order_list)
else:
return "Impossible"
print(do_job())
|
FUNC_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR VAR IF VAR NONE FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN STRING ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL STRING VAR RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
s, n = 1, int(input())
a, b = [], []
t = ""
d = [0] * 26
u = input()
exit = False
for i in range(n - 1):
v = input()
j = 0
while j < min(len(u), len(v)) and u[j] == v[j]:
j += 1
if j == min(len(u), len(v)):
if len(u) >= len(v):
exit = True
break
else:
q = ord(u[j]) - 97, ord(v[j]) - 97
a.append(q)
u = v
d[q[1]] = s
if not exit:
while a:
b = [q for q in a if d[q[0]] == s]
s += 1
for q in b:
d[q[1]] = s
if len(a) == len(b):
exit = True
break
else:
a, b = b, []
if exit:
print("Impossible")
else:
for x in sorted(enumerate(d), key=lambda x: x[1]):
t += chr(x[0] + 97)
print(t)
|
ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR IF VAR WHILE VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR LIST IF VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = list(map(int, input().split()))[0]
name_list = []
for _ in range(0, n):
name = input().split()[0]
name_list.append(name)
dag = dict()
def add_edge(char1, char2):
if char1 in dag.keys():
dag[char1][1].add(char2)
else:
dag[char1] = [set(), set([char2])]
if char2 in dag.keys():
dag[char2][0].add(char1)
else:
dag[char2] = [set([char1]), set()]
def compare_2_names(name1, name2):
for i in range(min(len(name1), len(name2))):
if name1[i] != name2[i]:
add_edge(name1[i], name2[i])
return True
if len(name2) <= len(name1):
return False
return True
def run():
res = []
current_candidate_nodes = []
for k in dag.keys():
if len(dag[k][0]) == 0:
current_candidate_nodes.append(k)
if len(current_candidate_nodes) == 0:
return False
num_letters = len(dag)
while len(res) < num_letters:
new_candidate_nodes = []
for item in current_candidate_nodes:
dag.pop(item)
for k in dag.keys():
if len(current_candidate_nodes) == 0:
return False
for c in current_candidate_nodes:
if c in dag[k][0]:
if len(dag[k][1] & set(res)) > 0:
return False
dag[k][0].remove(c)
if len(dag[k][0]) == 0:
new_candidate_nodes.append(k)
res += current_candidate_nodes
current_candidate_nodes = new_candidate_nodes
return res
last_name = name_list[0]
impossible = False
for i in range(1, n):
name = name_list[i]
if not impossible and compare_2_names(last_name, name) == False:
impossible = True
last_name = name
if impossible:
print("Impossible")
else:
all_chars = set("abcdefghijklmnopqrstuvwxyz")
if len(dag.keys()) == 0:
print("abcdefghijklmnopqrstuvwxyz")
else:
no_contraint_chars = all_chars - set(dag.keys())
path = run()
if path == False:
print("Impossible")
else:
print("".join(path) + "".join(no_contraint_chars))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR LIST VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR LIST FUNC_CALL VAR LIST VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = int(input())
x = input()
a = len(x)
lex = dict()
ALPHA = list("abcdefghijklmnopqrstuvwxyz")
for i in ALPHA:
for j in ALPHA:
lex[i, j] = 0
for i in range(n - 1):
y = input()
b = len(y)
for j in range(min([a, b])):
if x[j] != y[j]:
if lex[x[j], y[j]] == -1:
print("Impossible")
raise SystemExit
else:
lex[x[j], y[j]] = 1
lex[y[j], x[j]] = -1
x = y
a = b
break
if j == min([a, b]) - 1:
if a > b:
print("Impossible")
raise SystemExit
x = y
a = b
def biggest(alphabet):
for i in alphabet:
t = 0
for j in alphabet:
if lex[i, j] < 0:
t = 1
break
if t == 0:
return i
return -1
newalpha = [""] * 26
i = 0
while ALPHA != []:
BIG = biggest(ALPHA)
if biggest(ALPHA) == -1:
print("Impossible")
raise SystemExit
newalpha[i] = BIG
i = i + 1
ALPHA.remove(BIG)
print("".join(newalpha))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR LIST VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST STRING NUMBER ASSIGN VAR NUMBER WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
from sys import stdin, stdout
inp = stdin.readline
op = stdout.write
n = int(inp())
st = []
for i in range(n):
temp = list(inp()[:-1])
st.append(temp)
alph = list("abcdefghijklmnopqrstuvwxyz")
g = {alph[i]: [] for i in range(26)}
pos = 0
flag = 1
for i in range(n - 1):
x = st[i]
y = st[i + 1]
pos = 0
while pos < len(x) and pos < len(y) and x[pos] == y[pos]:
if pos + 1 < len(x) and pos + 1 >= len(y):
break
else:
pos = pos + 1
else:
if pos < len(x) and pos < len(y):
if x[pos] in g[y[pos]]:
break
else:
g[x[pos]].append(y[pos])
continue
break
else:
flag = 0
if flag == 0:
st = []
vis = {i: (0) for i in list(g.keys())}
q = []
indeg = {i: (0) for i in list(g.keys())}
for i in alph:
for j in g[i]:
if not j == 0:
indeg[j] += 1
for j in list(indeg.keys()):
if indeg[j] == 0:
q.append(j)
vis[j] = 1
ans = []
while len(q) > 0:
temp = q.pop(0)
ans.append(temp)
for j in g[temp]:
if not j == 0 and vis[j] == 0:
indeg[j] -= 1
for j in list(indeg.keys()):
if indeg[j] == 0 and vis[j] == 0:
q.append(j)
vis[j] = 1
if len(ans) == 26:
op("".join(ans))
else:
op("Impossible\n")
else:
op("Impossible\n")
|
ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN 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 WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
from itertools import zip_longest
def main():
names = [input() for _ in range(int(input()))]
edges = [set() for _ in range(27)]
chars = [True] * 27
for na, nb in zip(names, names[1:]):
for a, b in zip_longest(na, nb, fillvalue="`"):
if a != b:
edges[ord(a) - 96].add(ord(b) - 96)
break
def dfs(u):
chars[u] = None
for v in edges[u]:
if chars[v]:
dfs(v)
elif chars[v] is None:
raise TabError
chars[u] = False
try:
for c, f in enumerate(chars):
if f:
dfs(c)
except TabError:
print("Impossible")
return
chars, i = list(range(27)), 27
while i:
i -= 1
a = chars[i]
for b in edges[a]:
j = chars.index(b)
if j < i:
chars[i], chars[j] = b, a
i += 1
break
print("Impossible" if chars[0] else "".join(chr(c + 96) for c in chars[1:]))
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR NONE FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NONE VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = int(input())
g = [set() for _ in range(26)]
t = [0] * 26
a = input()
for _ in range(1, n):
b = input()
j = 0
while j < min(len(a), len(b)) and a[j] == b[j]:
j += 1
if j < min(len(a), len(b)) and a[j] != b[j]:
g[ord(a[j]) - ord("a")].add(ord(b[j]) - ord("a"))
elif len(a) > len(b):
print("Impossible")
exit()
a = b
o = []
def dfs(x):
t[x] = 1
for j in g[x]:
if t[j] == 1:
print("Impossible")
exit()
if t[j] == 0:
dfs(j)
t[x] = 2
o.append(chr(x + ord("a")))
for i in range(26):
if t[i] == 0:
dfs(i)
print("".join(reversed(o)))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = int(input())
names = []
for i in range(n):
names.append(input())
abc = "abcdefghijklmnopqrstuvwxyz"
grafo = {}
visited = {}
orden = []
flag = True
def dfs(s):
visited[s] = True
if s in grafo.keys():
for i in grafo[s]:
if i not in visited.keys():
dfs(i)
elif visited[i]:
orden.append(-1)
visited[s] = False
orden.append(s)
for i in range(1, n):
actual = names[i]
anterior = names[i - 1]
aux = 0
while len(actual) > aux and len(anterior) > aux and actual[aux] == anterior[aux]:
aux += 1
if len(actual) <= aux:
print("Impossible")
flag = False
break
if len(anterior) <= aux:
continue
if anterior[aux] not in grafo.keys():
grafo[anterior[aux]] = [actual[aux]]
else:
grafo[anterior[aux]].append(actual[aux])
if flag:
for i in grafo.keys():
if i not in visited.keys():
dfs(i)
orden = orden[::-1]
if -1 in orden:
print("Impossible")
else:
solucion = "".join(orden)
for letra in abc:
if letra not in orden:
solucion += letra
print(solucion)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = int(input())
names = [""] * n
l = [0] * n
for i in range(n):
names[i] = input().strip()
l[i] = len(names[i])
relations = [0] * n
proc = True
res = True
rels = [[0] * 26] * 26
for i in range(1, 26):
rels[i] = list(rels[0])
j = 0
while proc and res:
proc = False
for i in range(n - 1):
if relations[i] == 0:
proc = True
if l[i] > l[i + 1] and j == l[i + 1]:
res = False
elif j == l[i]:
relations[i] = 1
elif names[i][j] != names[i + 1][j]:
x = ord(names[i][j]) - ord("a")
y = ord(names[i + 1][j]) - ord("a")
if rels[x][y] + rels[y][x] != 0 and rels[x][y] == 2:
res = False
else:
rels[x][y] = 1
rels[y][x] = 2
relations[i] = 1
j += 1
if res:
ans = ""
used = [0] * 26
for i in range(26):
found = -1
for j in range(26):
if used[j]:
continue
ok = True
for x in rels[j]:
if x == 1:
ok = False
break
if ok:
found = j
break
if found == -1:
res = False
else:
c = chr(ord("a") + found)
ans = c + ans
used[found] = 1
for j in range(26):
if used[j] == 0 and rels[j][found] == 2:
res = False
else:
rels[j][found] = 0
if res:
print(ans)
else:
print("Impossible")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
import sys
def left(c):
return ord(c) - ord("a")
def right(x):
return chr(ord("a") + x)
n = int(input())
names = []
for i in range(n):
names.append(input())
graph = [[] for i in range(26)]
arrowCounter = [(0) for i in range(26)]
for i in range(n):
for j in range(i):
counter = 0
zipped = list(zip(names[i], names[j]))
for entry in zipped:
if entry[0] != entry[1]:
break
else:
counter += 1
if counter == len(names[i]) or counter == len(names[j]):
if len(names[j]) > len(names[i]):
print("Impossible")
sys.exit(0)
else:
graph[left(zipped[counter][1])].append(left(zipped[counter][0]))
arrowCounter[left(zipped[counter][0])] += 1
noArrows = []
for i in range(26):
if arrowCounter[i] == 0:
noArrows.append(i)
order = []
while len(noArrows) != 0:
current = noArrows.pop()
order.append(current)
for endpoint in graph[current]:
arrowCounter[endpoint] -= 1
if arrowCounter[endpoint] == 0:
noArrows.append(endpoint)
if len(order) != 26:
print("Impossible")
else:
answer = ""
for i in range(26):
answer += right(order[i])
print(answer)
|
IMPORT FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
import sys
def topo_dfs(adj_dict, visited, stack, topo_list):
node = stack[-1]
for e in adj_dict[node]:
if visited[e] == False:
stack.append(e)
return
topo_list.append(node)
visited[node] = True
stack.pop()
return
def cycle_dfs(adj_dict, visited, stack):
node = stack[-1]
for e in adj_dict[node]:
if visited[e] == False and e in stack:
return True
elif visited[e] == False:
stack.append(e)
return False
visited[node] = True
stack.pop()
return False
def answer(n, s):
adj_dict = {chr(i): [] for i in range(97, 123)}
for i in range(n - 1):
ctr = 0
while s[i][ctr] == s[i + 1][ctr] and ctr < min(len(s[i]), len(s[i + 1])) - 1:
ctr += 1
if s[i][ctr] == s[i + 1][ctr]:
if len(s[i]) > len(s[i + 1]):
return "Impossible"
else:
continue
adj_dict[s[i][ctr]].append(s[i + 1][ctr])
visited = {chr(i): (False) for i in range(97, 123)}
for i in range(97, 123):
if visited[chr(i)] == False:
stack = [chr(i)]
while len(stack) > 0:
cycle = cycle_dfs(adj_dict, visited, stack)
if cycle == True:
return "Impossible"
visited = {chr(i): (False) for i in range(97, 123)}
topo_list = []
for i in range(97, 123):
if visited[chr(i)] == False:
stack = [chr(i)]
while len(stack) > 0:
topo_dfs(adj_dict, visited, stack, topo_list)
topo_list.reverse()
return "".join(topo_list)
def main():
n = int(sys.stdin.readline())
s = ["" for _ in range(n)]
for i in range(n):
s[i] = sys.stdin.readline().rstrip()
print(answer(n, s))
return
main()
|
IMPORT FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
from itertools import zip_longest
def main():
names = [input() for _ in range(int(input()))]
edges = [set() for _ in range(27)]
for na, nb in zip(names, names[1:]):
for a, b in zip_longest(na, nb, fillvalue="`"):
if a != b:
edges[ord(a) - 96].add(ord(b) - 96)
break
chars, i, visited = list(range(27)), 27, [False] * (27 * 27)
while i:
i -= 1
a = chars[i]
j = a * 27 + i
if visited[j]:
chars[0] = 1
break
visited[j] = True
for b in edges[a]:
j = chars.index(b)
if j < i:
chars[i], chars[j] = b, a
i += 1
break
print("Impossible" if chars[0] else "".join(chr(c + 96) for c in chars[1:]))
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
import sys
input = sys.stdin.readline
def toposort(graph):
searched, stack = set(), []
for node in graph:
if node not in searched:
toposort_recurse(graph, node, searched, stack)
return stack[::-1]
def toposort_recurse(graph, node, searched, stack):
searched.add(node)
for peer in graph[node]:
if peer not in searched:
toposort_recurse(graph, peer, searched, stack)
stack.append(node)
def toposort_check(graph, path):
for node in graph:
for peer in graph[node]:
if path.index(peer) < path.index(node):
return False
return True
graph, n, prev, curr = (
{char: [] for char in "abcdefghijklmnopqrstuvwxyz"},
int(input()),
input().strip(),
"",
)
for i in range(n - 1):
old_curr = curr = input().strip()
while len(prev) > 0 and len(curr) > 0 and curr[0] == prev[0]:
curr, prev = curr[1:], prev[1:]
if curr and prev:
graph[prev[0]].append(curr[0])
elif prev and not curr:
print("Impossible")
sys.exit(0)
prev = old_curr
path = toposort(graph)
if toposort_check(graph, path):
print("".join(path))
else:
print("Impossible")
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR LIST VAR STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
def isPrefix(sa, sb):
if len(sa) <= len(sb):
return False
return sa[0 : len(sb)] == sb
def getOrder(sa, sb):
for i in range(0, min(len(sa), len(sb))):
if sa[i] != sb[i]:
return sa[i], sb[i]
test = False
if test:
fp = open("in.txt", "r")
n = int(fp.readline().strip())
names = [fp.readline().strip() for i in range(0, n)]
fp.close()
else:
n = int(input().strip())
names = [input().strip() for i in range(0, n)]
g = [([False] * 26) for i in range(0, 26)]
res = True
for i in range(1, n):
if names[i - 1] == names[i] or isPrefix(names[i], names[i - 1]):
continue
elif isPrefix(names[i - 1], names[i]):
res = False
break
else:
ca, cb = getOrder(names[i - 1], names[i])
if g[ord(cb) - ord("a")][ord(ca) - ord("a")]:
res = False
break
else:
g[ord(ca) - ord("a")][ord(cb) - ord("a")] = True
def printG():
print(" abcdefghijklmnopqrstuvwxyz")
for i in range(0, 26):
print(chr(ord("a") + i), "".join([("1" if x else "0") for x in g[i]]), sep="")
if not res:
print("Impossible")
else:
def getZeroIndegreeNode():
for i in range(0, 26):
if not used[i] and indegree[i] == 0:
return i
return -1
theOrder = []
indegree = [0] * 26
used = [False] * 26
for i in range(0, 26):
ithIndegree = 0
for j in range(0, 26):
if g[j][i]:
ithIndegree += 1
indegree[i] = ithIndegree
for i in range(0, 26):
zeroIndegreeNode = getZeroIndegreeNode()
if zeroIndegreeNode == -1:
res = False
break
else:
used[zeroIndegreeNode] = True
theOrder.append(chr(ord("a") + zeroIndegreeNode))
for j in range(0, 26):
if g[zeroIndegreeNode][j]:
indegree[j] -= 1
if not res:
print("Impossible")
else:
print("".join(theOrder))
|
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FUNC_DEF EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FUNC_CALL STRING VAR STRING STRING VAR VAR VAR STRING IF VAR EXPR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
def ex():
print("Impossible")
exit()
abc = "abcdefghijklmnopqrstuvwxyz"
edges = {i: set() for i in abc}
used = set()
def fillchar(name, prev_name):
for current, prev in zip(name, prev_name):
if current != prev:
if current in edges[prev]:
ex()
if prev not in edges[current]:
edges[current].add(prev)
return True
return False
n = int(input())
prev_name = input()
for i in range(1, n):
name = input()
if not fillchar(name, prev_name) and len(prev_name) > len(name):
ex()
prev_name = name
def dfs(v, result):
used.add(v)
for c in edges[v]:
if c not in used:
dfs(c, result)
result.append(v)
def topsort():
result = []
for c in abc:
if c not in used:
dfs(c, result)
return result
result = topsort()
for node in edges:
for edge in edges[node]:
if result.index(node) < result.index(edge):
ex()
print("".join(result))
|
FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = int(input())
nrLit = ord("z") - ord("a") + 1
v = [[(0) for i in range(nrLit)] for j in range(nrLit)]
s0 = input()
posibil = True
for i in range(1, n):
if not posibil:
break
s = input()
mini = min(len(s0), len(s))
for j in range(mini):
a = ord(s0[j]) - ord("a")
b = ord(s[j]) - ord("a")
if s0[j] != s[j]:
if v[a][b] <= 0 and v[b][a] >= 0:
v[a][b] = -1
v[b][a] = 1
for k in range(nrLit):
if v[k][a] == -1:
if v[b][k] >= 0:
v[k][b] = -1
v[b][k] = 1
else:
posibil = False
else:
posibil = False
break
else:
if s0 > s:
posibil = False
s0 = s
if not posibil:
print("Impossible")
else:
marcat = set()
s = ""
for lit in range(nrLit):
for i in range(nrLit):
if i in marcat:
continue
for j in range(nrLit):
if v[i][j] == 1 and j not in marcat:
break
else:
s = s + chr(i + ord("a"))
marcat.add(i)
break
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = int(input())
cur_line = ""
next_line = ""
next_line = input()
edges = [[(0) for _ in range(27)] for _ in range(27)]
for i in range(n - 1):
cur_line = next_line
next_line = input()
first = ""
second = ""
for j in range(min(len(cur_line), len(next_line))):
if cur_line[j].lower() != next_line[j].lower():
first = cur_line[j].lower()
second = next_line[j].lower()
break
if not first or not second:
if len(cur_line) > len(next_line):
print("Impossible")
exit()
else:
edges[ord(second) - ord("a")][ord(first) - ord("a")] = 1
gray_edges = set()
black_edges = []
def dfs(start):
global gray_edges
global black_edges
global edges
found_next = False
for letter in range(26):
if edges[start][letter]:
if letter in gray_edges:
return "Impossible"
elif letter not in black_edges:
gray_edges.add(letter)
found_next = True
if dfs(letter) == "Impossible":
return "Impossible"
black_edges.append(start)
gray_edges.remove(start)
if not found_next:
return "Proceed with next letter"
for i in range(26):
if i not in gray_edges and i not in black_edges:
gray_edges.add(i)
if dfs(i) == "Impossible":
print("Impossible")
exit()
print("".join(list(map(lambda x: chr(x + ord("a")), black_edges))))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR IF VAR VAR RETURN STRING IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR STRING RETURN STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
def char2num(c):
return ord(c) - ord("a")
def dfs(v):
vis[v] = 0
for w in adj[v]:
if vis[w] == 0:
return False
elif vis[w] == -1:
if not dfs(w):
return False
alf.append(v)
vis[v] = 1
return True
n, last = int(input()), input()
adj = [[] for _ in range(26)]
for _ in range(1, n):
next_ = input()
if last.startswith(next_):
print("Impossible")
break
if not next_.startswith(last):
i = 0
while next_[i] == last[i]:
i += 1
adj[char2num(last[i])].append(char2num(next_[i]))
last = next_[:]
else:
alf = []
vis = [-1] * 26
for i in range(26):
if vis[i] == -1:
if not dfs(i):
print("Impossible")
break
else:
print(*list([chr(ord("a") + i) for i in reversed(alf)]), sep="")
|
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR STRING
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = int(input())
s = [input() for i in range(n)]
f = [[(0) for i in range(26)] for j in range(26)]
for i in range(n):
for k in range(i + 1, n):
if len(s[k]) < len(s[i]) and s[k] == s[i][: len(s[k])]:
print("Impossible")
exit(0)
for j in range(min(len(s[k]), len(s[i]))):
if s[k][j] != s[i][j]:
if f[ord(s[k][j]) - ord("a")][ord(s[i][j]) - ord("a")] == 1:
print("Impossible")
exit(0)
if f[ord(s[i][j]) - ord("a")][ord(s[k][j]) - ord("a")] == -1:
print("Impossible")
exit(0)
f[ord(s[k][j]) - ord("a")][ord(s[i][j]) - ord("a")] = -1
f[ord(s[i][j]) - ord("a")][ord(s[k][j]) - ord("a")] = 1
break
def dfs(k, used, f, res):
if used[k] == 1:
print("Impossible")
exit(0)
used[k] = 1
for i in range(26):
if used[i] != 2 and f[k][i] == 1:
dfs(i, used, f, res)
used[k] = 2
res.append(k)
used = [(0) for i in range(26)]
res = []
for i in range(25, -1, -1):
if used[i] == 0:
dfs(i, used, f, res)
for i in range(26):
print(chr(res[25 - i] + ord("a")), end="")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER FUNC_DEF IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR STRING STRING
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
def f():
s, n = 1, int(input())
a, b = [], []
t, e = "", "Impossible"
d = [0] * 26
u = input()
for i in range(n - 1):
v = input()
x, y = len(u), len(v)
j, k = 0, min(x, y)
while j < k and u[j] == v[j]:
j += 1
if j == k:
if x >= y:
return e
else:
q = ord(u[j]) - 97, ord(v[j]) - 97
a.append(q)
u, d[q[1]] = v, s
while a:
b = [q for q in a if d[q[0]] == s]
s += 1
for q in b:
d[q[1]] = s
if len(a) == len(b):
return e
a, b = b, []
for x in sorted(enumerate(d), key=lambda x: x[1]):
t += chr(x[0] + 97)
return t
print(f())
|
FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR STRING STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR WHILE VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
def found(x, a):
for i in range(len(a)):
if a[i] == x:
return i
return None
n = int(input())
a = []
L = 0
for i in range(n):
x = input()
a.append(x)
L = max(L, len(x))
d = dict()
u = dict()
for i in range(97, 123):
d[chr(i)] = set()
u[chr(i)] = set()
for i in range(n - 1):
ind = 0
while ind < min(len(a[i]), len(a[i + 1])):
if a[i][ind] != a[i + 1][ind]:
d[a[i][ind]].add(a[i + 1][ind])
u[a[i + 1][ind]].add(a[i][ind])
ind = 10**10
else:
ind += 1
if ind == min(len(a[i]), len(a[i + 1])):
if len(a[i]) > len(a[i + 1]):
print("Impossible")
exit()
del_u = set()
del_d = set()
for i in d:
if len(d[i]) == 0:
del_d.add(i)
for i in u:
if len(u[i]) == 0:
del_u.add(i)
for i in del_d:
del d[i]
for i in del_u:
del u[i]
g = dict()
for i in u:
g[i] = 120
if len(d) == 0:
print("abcdefghijklmnopqrstuvwxyz")
exit()
v = set()
for i in d:
v.add(i)
for i in u:
v.discard(i)
for i in v:
g[i] = 0
posetil = set()
for i in v:
sosed = d[i]
glub = 1
while len(sosed) > 0:
sosed2 = set()
for j in sosed:
if g[j] == 120:
g[j] = glub
elif g[j] < glub:
g[j] = glub
if j in d:
for k in d[j]:
sosed2.add(k)
sosed = sosed2.copy()
glub += 1
ans = dict()
for i in range(40):
ans[i] = set()
for i in g:
if g[i] == 120:
print("Impossible")
exit()
ans[g[i]].add(i)
answer = []
for i in range(40):
if len(ans[i]) != 0:
for j in ans[i]:
answer.append(j)
lol = set(answer)
for i in range(97, 123):
if chr(i) not in lol:
answer.append(chr(i))
if len(v) == 0:
print("Impossible")
else:
print(*answer, sep="")
|
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
def isPrefix(sa, sb):
if len(sa) >= len(sb):
return False
return sa == sb[0 : len(sa)]
def getOrder(sa, sb):
for i in range(min(len(sa), len(sb))):
if sa[i] != sb[i]:
return sa[i], sb[i]
test = False
if test:
fp = open("in.txt", "r")
n = int(fp.readline().strip())
names = [fp.readline().strip() for _ in range(n)]
fp.close()
else:
n = int(input().strip())
names = [input().strip() for _ in range(n)]
res = True
g = [([False] * 26) for _ in range(26)]
def printG():
print(" abcdefghijklmnopqrstuvwxyz")
for i in range(0, 26):
print(chr(ord("a") + i), "".join([("1" if x else "0") for x in g[i]]), sep="")
for i in range(n - 1):
if names[i] == names[i + 1] or isPrefix(names[i], names[i + 1]):
continue
elif isPrefix(names[i + 1], names[i]):
res = False
break
else:
ca, cb = getOrder(names[i], names[i + 1])
if g[ord(cb) - ord("a")][ord(ca) - ord("a")]:
res = False
break
else:
a = ord(ca) - ord("a")
b = ord(cb) - ord("a")
g[a][b] = True
if not res:
print("Impossible")
else:
def getZeroIndegreeNode():
for i in range(26):
if not vis[i] and Indegree[i] == 0:
return i
return -1
strOrder = []
vis = [False] * 26
Indegree = [0] * 26
for i in range(26):
ithIndegree = 0
for j in range(26):
if g[j][i]:
ithIndegree += 1
Indegree[i] = ithIndegree
for i in range(26):
ZeroIndegreeNode = getZeroIndegreeNode()
if ZeroIndegreeNode == -1:
res = False
break
else:
strOrder.append(chr(ord("a") + ZeroIndegreeNode))
vis[ZeroIndegreeNode] = True
for i in range(26):
if g[ZeroIndegreeNode][i]:
Indegree[i] -= 1
if not res:
print("Impossible")
else:
print("".join(strOrder))
|
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FUNC_CALL STRING VAR STRING STRING VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
from sys import stdin, stdout
n = int(stdin.readline().strip())
d = {
"a": 0,
"b": 1,
"c": 2,
"d": 3,
"e": 4,
"f": 5,
"g": 6,
"h": 7,
"i": 8,
"j": 9,
"k": 10,
"l": 11,
"m": 12,
"n": 13,
"o": 14,
"p": 15,
"q": 16,
"r": 17,
"s": 18,
"t": 19,
"u": 20,
"v": 21,
"w": 22,
"x": 23,
"y": 24,
"z": 25,
}
arr = []
for i in range(n):
arr.append(stdin.readline().strip())
cycle = False
edge_matrix = [[(0) for _ in range(26)] for i in range(26)]
for i in range(1, len(arr)):
word1 = arr[i - 1]
word2 = arr[i]
ctr = 0
if len(word1) > len(word2) and word1[: len(word2)] == word2:
cycle = True
break
while ctr < min(len(word1), len(word2)) and word1[ctr] == word2[ctr]:
ctr += 1
if not ctr == min(len(word1), len(word2)):
edge_matrix[d[word1[ctr]]][d[word2[ctr]]] = 1
ansstack = []
visited = [(0) for i in range(26)]
def topological_sort(curr):
global cycle, visited, ansstack
if not cycle:
visited[curr] = 1
for i in range(len(edge_matrix[curr])):
if edge_matrix[curr][i] == 1:
if visited[i] == 0:
topological_sort(i)
elif visited[i] == 1:
cycle = True
break
visited[curr] = 2
ansstack.insert(0, curr)
def doshit():
for i in range(26):
if not cycle and visited[i] == 0 and sum(edge_matrix[i]) > 0:
topological_sort(i)
doshit()
if cycle:
stdout.write("Impossible\n")
else:
for i in range(26):
if i not in ansstack:
stdout.write(chr(ord("a") + i))
for i in ansstack:
stdout.write(chr(ord("a") + i))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} ≠ t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string name_{i} (1 ≤ |name_{i}| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
-----Output-----
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
-----Examples-----
Input
3
rivest
shamir
adleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Output
Impossible
Input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
Output
acbdefhijklmnogpqrstuvwxyz
|
n = int(input())
a = [[] for i in range(26)]
b = [(0) for i in range(26)]
ans = True
for i in range(n):
t = input()
if i > 0:
ans = ans and s[: len(t)] != t
if not ans:
break
if t[: len(s)] != s:
x, y = next(filter(lambda x: x[0] != x[1], zip(s, t)))
a[ord(x) - 97].append(ord(y) - 97)
b[ord(y) - 97] += 1
s = t
if ans:
ans = []
q = []
l = 0
for i in range(26):
if not b[i]:
q.append(i)
while l < len(q):
x = q[l]
ans.append(chr(x + 97))
l += 1
for y in a[x]:
b[y] -= 1
if not b[y]:
q.append(y)
if ans and len(ans) == 26:
print("".join(ans))
else:
print("Impossible")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
|
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.
A common divisor for two positive numbers is a number which both numbers are divisible by.
But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.
You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.
Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
Examples
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
|
a, b = map(int, input().split())
n = int(input())
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def bs(l, r, val, a):
l1 = l
r1 = r
res = 0
while l1 <= r1:
mid = (l1 + r1) // 2
if a[mid] <= val:
l1 = mid + 1
res = a[mid]
else:
r1 = mid - 1
return res
t = gcd(a, b)
wish = []
for i in range(1, t + 1):
if t // i < i:
break
if t % i == 0:
wish.append(i)
if t // i != i:
wish.append(t // i)
wish.sort()
for i in range(0, n):
low, high = map(int, input().split())
temp = bs(0, len(wish) - 1, high, wish)
if temp < low:
temp = -1
print(temp)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.
A common divisor for two positive numbers is a number which both numbers are divisible by.
But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.
You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.
Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
Examples
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
|
a, b = map(int, input().split())
arr = []
arr_len = 0
cnt = 0
a, b = min(a, b), max(a, b)
for x in range(1, int(a**0.5) + 1):
if a % x == 0:
arr.append(x)
arr.append(a // x)
arr_len += 2
for x in range(arr_len):
if b % arr[x] != 0:
arr[x] = -1
cnt += 1
arr_len -= 1
arr = list(set(sorted(arr)[cnt:]))
for i in range(int(input())):
res = -1
l, r = map(int, input().split())
for d in arr:
if d >= l and d <= r:
res = max(res, d)
print(res)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.
A common divisor for two positive numbers is a number which both numbers are divisible by.
But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.
You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.
Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
Examples
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
|
a, b = list(map(int, input().split()))
c = []
for i in range(1, int(a**0.5) + 1):
if a % i == b % i == 0:
c.append(i)
if b % (a // i) == a % (a // i) == 0 and i != a // i:
c.append(a // i)
c.sort()
d = len(c)
for i in range(int(input())):
e, h = list(map(int, input().split()))
l, r = -1, d
while r - l - 1:
m = (l + r) // 2
if c[m] > h:
r = m
else:
l = m
print(c[r - 1] if h >= c[r - 1] >= e else -1)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR WHILE BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER
|
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.
A common divisor for two positive numbers is a number which both numbers are divisible by.
But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.
You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.
Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
Examples
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
|
from sys import stdin, stdout
def __gcd(a, b):
if b == 0:
return a
else:
return __gcd(b, a % b)
def main():
a, b = map(int, stdin.readline().split())
gcd = __gcd(a, b)
i = 1
factor = []
while i * i <= gcd:
if gcd % i == 0:
factor.append(i)
if i * i != gcd:
factor.append(gcd // i)
i += 1
q = int(stdin.readline())
for i in range(q):
res = -1
l, r = map(int, stdin.readline().split())
for j in range(len(factor)):
if factor[j] >= l and factor[j] <= r:
res = max(res, factor[j])
stdout.write(str(res) + "\n")
main()
|
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.
A common divisor for two positive numbers is a number which both numbers are divisible by.
But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.
You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.
Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
Examples
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
|
def main():
A, B = map(int, input().split())
if A > B:
A, B = B, A
divisors = set()
div = 1
while div * div <= A:
if A % div == 0:
x = div
y = A // div
if B % x == 0:
divisors.add(x)
if B % y == 0:
divisors.add(y)
div = div + 1
Q = int(input())
divs = list(divisors)
divs.sort()
for i in range(0, Q):
low, high = map(int, input().split())
s, e, mid, sol = 0, len(divs) - 1, 0, -1
while s <= e:
mid = s + (e - s) // 2
if divs[mid] <= high:
sol = mid
s = mid + 1
else:
e = mid - 1
if divs[sol] < low:
print(-1)
else:
print(divs[sol])
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.
A common divisor for two positive numbers is a number which both numbers are divisible by.
But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.
You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.
Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
Examples
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
|
def gcd(num1, num2):
if num2 == 0:
return num1
return gcd(num2, num1 % num2)
a, b = map(int, input().split())
n = int(input())
k = gcd(a, b)
res1 = []
i = 1
while i * i <= k:
if k % i == 0:
res1.append(i)
if i != k // i:
res1.append(k // i)
i += 1
for _ in range(n):
l, r = map(int, input().split())
ans = -1
for d in res1:
if l <= d <= r:
ans = max(ans, d)
print(ans)
|
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.
A common divisor for two positive numbers is a number which both numbers are divisible by.
But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.
You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.
Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
Examples
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
|
import sys
lines = sys.stdin.readlines()
m, n = map(int, lines[0].strip().split(" "))
def gcd(a, b):
if a == 0:
return b
if a > b:
return gcd(b, a)
return gcd(b % a, a)
G = gcd(m, n)
tmp = G
factors = []
i = 2
while i**2 <= tmp:
if tmp % i == 0:
factors.append(i)
tmp //= i
else:
i += 1
if tmp != 1:
factors.append(tmp)
count = {}
for f in factors:
if f not in count:
count[f] = 0
count[f] += 1
primes = list(count.keys())
primes.sort()
factors = [1]
for p in primes:
tmp = []
for f in factors:
for i in range(1, count[p] + 1):
tmp.append(f * p**i)
factors += tmp
factors.sort()
L = len(factors)
Q = int(lines[1].strip())
for q in range(2, Q + 2):
lo, hi = map(int, lines[q].strip().split(" "))
l, r = 0, L
while l < r - 1:
mid = (r - l) // 2 + l
if factors[mid] > hi:
r = mid
else:
l = mid
if factors[l] >= lo:
print(factors[l])
else:
print(-1)
|
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER STRING FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.
A common divisor for two positive numbers is a number which both numbers are divisible by.
But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.
You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.
Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
Examples
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
|
def factors(n):
i = 2
res = list()
while i * i <= n:
if n % i == 0:
cnt = 0
while n % i == 0:
n //= i
cnt += 1
res.append((i, cnt))
i += 1
if not n == 1:
res.append((n, 1))
return res
def generate_divisors(factors, ind, result, divisors):
if ind == len(factors):
divisors.append(result)
return
for i in range(factors[ind][1] + 1):
generate_divisors(factors, ind + 1, result, divisors)
result *= factors[ind][0]
if ind == 0:
return divisors
def search(s, e, x, y):
while s < e:
mid = s + (e - s) // 2
if y >= common[mid]:
s = mid + 1
else:
e = mid + 0
return common[s - 1]
a, b = map(int, input().strip().split())
div_a = generate_divisors(factors(a), 0, 1, [])
div_b = generate_divisors(factors(b), 0, 1, [])
common = list(set(div_a).intersection(set(div_b)))
common = sorted(common)
c_size = len(common)
c_max, c_min = common[c_size - 1], common[0]
n = int(input())
for _ in range(n):
x, y = map(int, input().strip().split())
if x > c_max or y < c_min:
print(-1)
else:
ans = search(0, c_size, x, y)
if ans < x or ans > y:
print(-1)
else:
print(ans)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER 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 FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.
A common divisor for two positive numbers is a number which both numbers are divisible by.
But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.
You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.
Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
Examples
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
|
n, m = [int(i) for i in input().split()]
l = []
for i in range(1, int(n**0.5) + 1):
if n % i == 0 and m % i == 0:
l.append(i)
if n % (n // i) == 0 and m % (n // i) == 0:
l.append(n // i)
l.sort()
n = int(input())
for i in range(n):
q, w = [int(j) for j in input().split()]
mx = -1
for j in range(len(l) - 1, -1, -1):
if q <= l[j] <= w:
mx = l[j]
break
print(mx)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.
A common divisor for two positive numbers is a number which both numbers are divisible by.
But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.
You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.
Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).
Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
Examples
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9
|
from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
mp = lambda: list(map(int, cin().split()))
def chars():
s = cin()
return list(s[: len(s) - 1])
def pl(a):
for val in a:
cout(val + "\n")
def binSearch(lo, hi):
high = len(l) - 1
low = 0
ans = -1
while low <= high:
mid = (low + high) // 2
if hi >= l[mid] and l[mid] >= lo:
ans = l[mid]
low = mid + 1
elif l[mid] < lo:
low = mid + 1
elif l[mid] > hi:
high = mid - 1
return ans
a, b = sorted(mp())
(n,) = mp()
s = set()
for i in range(1, int(a**0.5) + 1):
if not a % i and not b % i:
s.add(i)
if not a % (a // i) and not b % (a // i):
s.add(a // i)
l = sorted(s)
for _ in range(n):
lo, hi = mp()
ans = binSearch(lo, hi)
cout(str(ans) + "\n")
|
ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def calSumTransfer(m):
global a
sum_transfer = 0
for i in range(n):
sum_transfer += max(a[i] - m, 0)
return sum_transfer
n, k = map(int, input().split())
a = list(map(int, input().split()))
left = min(a)
right = max(a)
sum_energy = sum(a)
mid = left
while right - left > 10**-6:
mid = (left + right) / 2
sum_lost = calSumTransfer(mid) * k / 100
if mid * n < sum_energy - sum_lost:
left = mid
else:
right = mid
print("%.9f" % mid)
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
EPS = 10**-10
n, k = map(int, input().split())
rate = 100 / (100 - k) - 1
a = list(map(int, input().split()))
total = 0
for x in a:
total += x
lo = 0.0
hi = 1000.0
while abs(hi - lo) > EPS:
mi = (hi + lo) / 2
need = mi * n
for i in range(n):
if a[i] < mi:
need += (mi - a[i]) * rate
if need > total:
hi = mi
else:
lo = mi
print("{:.9f}".format(lo))
|
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
nk = list(input().split())
n, k = int(nk[0]), float(nk[1])
energy = list(map(float, input().split()))
avg = sum(energy) / len(energy)
while 1:
sum_lf = 0
sum_rg = 0
cnt_lf = 0
cnt_rg = 0
lf = []
flag = False
for i in range(n):
if energy[i] < avg:
cnt_lf += 1
sum_lf += energy[i]
lf.append(energy[i])
else:
cnt_rg += 1
sum_rg += energy[i]
x = (cnt_lf * sum_rg - cnt_rg * sum_lf) / (cnt_rg * (100 - k) / 100 + cnt_lf)
avg = (sum_rg - x) / cnt_rg
for i in range(len(lf)):
if lf[i] > avg:
flag = True
if not flag:
break
print("%.9f" % avg)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
n, k = map(int, input().split())
A = list(map(int, input().split()))
def f(x):
over = 0
need = 0
for a in A:
if a > x:
over += a - x
else:
need += x - a
return need <= over * (1 - k / 100)
left = 0
right = max(A)
while right - left > 1e-07:
m = (left + right) / 2
if f(m):
left = m
else:
right = m
rounded = round(right, 8)
if int(rounded) == rounded:
rounded = int(rounded)
print(rounded)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
(n, k), t = map(int, input().split()), [0] + sorted(list(map(int, input().split())))
i, d, s = 1, t[1], sum(t)
while i < n:
i += 1
d += t[i]
if 100 * (s - t[i] * n) < k * (s - d - t[i] * (n - i)):
d -= t[i]
i -= 1
break
print(
t[i]
+ (100 * (s - t[i] * n) - k * (s - d - t[i] * (n - i))) / (100 * n - k * (n - i))
)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR IF BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def energy(n, k, arr):
arr = sorted(arr)
max_energy = -1
left = min(arr)
right = max(arr)
while abs(left - right) > 10**-6:
X = (left + right) / 2
a = sum(arr)
thua = 0
for i in arr:
if i > X:
thua += (i - X) * k / 100
if a - thua == n * X:
max_energy = X
elif a - thua - n * X > 0:
left = X
else:
right = X
print(left)
n, k = list(map(int, input().strip().split(" ")))
arr = list(map(int, input().strip().split(" ")))
energy(n, k, arr)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
left, right = 0.0, 1000.0
while right - left > 1e-07:
mid = (right + left) / 2
transfer = 0
for i in range(n):
if a[i] > mid:
transfer += (a[i] - mid) * (1 - k / 100)
else:
transfer += a[i] - mid
if transfer >= 0:
left = mid
else:
right = mid
print("%.9lf" % left)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def isEnough(x):
receiving = 0
giving = 0
for acc in accumulators:
if acc < x:
receiving += x - acc
else:
giving += (acc - x) * (100 - k) / 100
return receiving <= giving
def BinarySreach(l, r):
left = l
right = r
ans = r
while left <= right:
mid = (left + right) / 2
if isEnough(mid):
ans = mid
left = mid + 10**-10
else:
right = mid - 10**-10
return ans
n, k = map(int, input().split())
accumulators = list(map(int, input().split()))
print("{:.9f}".format(BinarySreach(0, max(accumulators))))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
n, k = map(int, input().split())
accumulators = list(map(int, input().split()))
low = 0
high = 1000
for i in range(100):
less = 0
more = 0
mid = low + (high - low) / 2
for i in range(n):
if accumulators[i] > mid:
more += accumulators[i] - mid
else:
less += mid - accumulators[i]
if more - k * more / 100 >= less:
low = mid
else:
high = mid
print("{0:.9f}".format(low))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
left = 0
right = a[-1]
for i in range(100):
mid = (left + right) / 2.0
s1 = sum([(x - mid) for x in a if x >= mid]) * (100 - k) / 100.0
s2 = sum([(mid - x) for x in a if x < mid])
if s1 >= s2:
left = mid
else:
right = mid
print(left)
|
ASSIGN 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 ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def check(a, e, k):
need = 0.0
have = 0.0
for x in a:
if x > e:
have += x - e
else:
need += e - x
if have - have * k / 100 >= need:
return True
else:
return False
def bs(a, k):
low = 0.0
hight = 1000.0
res = 0.0
while abs(low - hight) >= 1e-09:
mid = (low + hight) / 2
if check(a, mid, k):
res = mid
low = mid
else:
hight = mid
return res
n, k = map(int, input().split())
a = list(map(int, input().split()))
print("%.6f" % bs(a, k))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def isPossibleWithAmount(arr, e, k):
total = 0
for number in arr:
if number > e:
total += number - e
else:
x = (e - number) * 100 / (100 - k)
total -= x
return total >= 0
n, k = map(int, input().split())
arr = list(map(int, input().split()))
left = min(arr)
right = max(arr)
while abs(left - right) > 10**-7:
mid = (right + left) / 2
test = isPossibleWithAmount(arr, mid, k)
if test == True:
left = mid
else:
right = mid
print(left)
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
n, k = map(int, input().split())
energy = sorted(list(map(int, input().split())))
factor = (100 - k) / 100
low = 0
high = sum(energy) / n
while high - low > 1e-06:
mid = (low + high) / 2
balance = 0
for x in energy:
if x > mid:
balance -= factor * (x - mid)
else:
balance += mid - x
if balance < 0:
low = mid
else:
high = mid
print(high)
|
ASSIGN 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 ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def energy(acc, level, perc_loss):
surplus = sum([max(x - level, 0) for x in acc])
en_cost = sum([(abs(min(0, x - level)) * 100 / (100 - perc_loss)) for x in acc])
return surplus >= en_cost
class CodeforcesTask68BSolution:
def __init__(self):
self.result = ""
self.n_k = []
self.accumulators = []
def read_input(self):
self.n_k = [int(x) for x in input().split(" ")]
self.accumulators = [int(x) for x in input().split(" ")]
def process_task(self):
l = 0
r = sum(self.accumulators) / self.n_k[0]
while r - l >= 1e-07:
mid = l + (r - l) / 2
if energy(self.accumulators, mid, self.n_k[1]):
l = mid
else:
r = mid
self.result = str(r)
def get_result(self):
return self.result
Solution = CodeforcesTask68BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN 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 FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def ok(value):
low, up = 0, 0
for i in range(n):
if a[i] < value:
low += value - a[i]
else:
up += a[i] - value
return up - up * k / 100.0 >= low
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
s = sum(a)
low, high = 0, 1000.0
for i in range(100):
mid = (low + high) / 2
if ok(mid):
low = mid
else:
high = mid
print("%.9lf\n" % low)
|
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN 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 FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
n, k = list(map(int, input().split()))
arr = list(map(int, input().split()))
def bins():
l = 0
r = 10000000000000
for i in range(100):
mid = (l + r) / 2
if f(mid):
l = mid
else:
r = mid
print(l)
def f(s):
sent = 0
sentR = 0
for i in range(n):
if arr[i] < s:
sentR += (s - arr[i]) * 100 / (100 - k)
elif arr[i] > s:
sent += arr[i] - s
return sent >= sentR
bins()
|
ASSIGN 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 FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def check(bound):
more, less = 0, 0
for x in a:
if x > bound:
more += x - bound
else:
less += bound - x
return more * (100 - k) / 100 - less >= 0
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
res, l, r = 0, 0, 1000
if max(a) == min(a):
res = a[0]
for loops in range(200):
m = (l + r) / 2
if check(m):
res = m
l = m
else:
r = m
print(f"{res:.8f}")
|
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def get_sum_transfer(accumulators_energy, middle):
sum_transfer = 0
for accumulator_energy in accumulators_energy:
if accumulator_energy > middle:
sum_transfer += accumulator_energy - middle
return sum_transfer
def solve():
accumulators_count, percent_energy_lost = map(int, input().split())
accumulators_energy = list(map(int, input().split()))
sum_energy = sum(accumulators_energy)
left = min(accumulators_energy)
right = max(accumulators_energy)
while right - left > 1e-07:
middle = (left + right) / 2
sum_transfer = get_sum_transfer(accumulators_energy, middle)
sum_lost = sum_transfer * percent_energy_lost / 100
if middle * accumulators_count < sum_energy - sum_lost:
left = middle
else:
right = middle
print("{:.9f}".format(left))
solve()
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def check(a, k, cur):
need = 0
have = 0
for x in a:
if x < cur:
need += cur - x
else:
have += (x - cur) * (1 - k / 100)
return need <= have
n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
l = 0
r = 1000
for i in range(100):
cur = (l + r) / 2
if check(a, k, cur):
l = cur
else:
r = cur
print((l + r) / 2)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def main():
n, k = map(int, input().split())
k = 1.0 - k / 100
a = list(map(int, input().split()))
ans = 0
l, r = 0, 1000
for rep in range(100):
mid = (l + r) / 2
need, excess = 0, 0
for energy in a:
if energy > mid:
excess += (energy - mid) * k
else:
need += mid - energy
if excess >= need:
ans = mid
l = mid
else:
r = mid
print(ans)
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
import sys
input = sys.stdin.readline
rInt = lambda: int(input())
mInt = lambda: map(int, input().split())
rList = lambda: list(map(int, input().split()))
m, k = mInt()
a = rList()
lo = 0
hi = 1000
for _ in range(100):
test = (lo + hi) / 2
bar = 0
for v in a:
if v > test:
bar += (100 - k) * (v - test)
else:
bar += 100 * (v - test)
if bar > 0:
lo = test
else:
hi = test
print(test)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
def check(x):
a = 0
b = 0
up = 0
dn = 0
for i in e:
if i < x:
b += 1
dn += i
elif i > x:
a += 1
up += i
left = (up - a * x) * (100 - k) / 100.0
right = x * b - dn
if abs(left - right) <= 10**-7:
return 1
elif left > right:
return 2
else:
return 3
n, k = list(map(int, input().split()))
e = list(map(int, input().split()))
st = 0
ed = 1000
test = 0
while st <= ed:
x = (st + ed) / 2
checker = check(x)
if checker == 1:
test = 1
print(x)
break
elif checker == 2:
st = x + 1e-07
elif checker == 3:
ed = x - 1e-07
if test == 0:
print(x)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)
eps = 1e-10
rem = (100 - k) / 100
left = min(a)
right = max(a)
guess = left
while right - left > eps:
guess = left + (right - left) / 2
transferred = 0.0
missing = 0.0
for ai in a:
if ai - guess > 0:
transferred += rem * (ai - guess)
else:
missing += guess - ai
if missing < transferred:
left = guess
else:
right = guess
print(guess)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.