Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import sys, random, math
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
def main():
inf = 10 ** 20
t = int(input())
# t, a, b = map(int, input().split())
# t = 1
for _ in range(1, t+1):
# print("Case #{}: ".format(_), end = '')
h, c, t = map(int, input().split())
if((h+c)/2 >= t):
if(abs((h+c)/2 - t) < abs(h-t)):
print(2)
else:
print(1)
else:
l, r = 0, inf
close = inf
while(l <= r):
mid = (l+r) // 2 * 2 + 1
total = h * (mid // 2 + mid % 2) + c * (mid//2)
# print(l, r, mid, total, avg)
if(total > t * mid):
l = ((l+r) // 2) + 1
else:
r = ((l+r) // 2) - 1
close = min(mid, close)
if(close == 1):
print(1)
continue
t0 = h * (close // 2 + close % 2) + c * (close//2)
t1 = h * (close // 2 + close % 2-1) + c * (close//2-1)
# print(close, abs(t0/close-t), abs(t1/(close-2)-t))
def gcd(a, b):
if(not b):
return a
return gcd(b, a % b)
lcmab = close // gcd(close, close-2) * (close-2)
if(2*t*lcmab < (t1*lcmab//(close-2)+t0*lcmab//(close))):
print(close)
else:
print(close - 2)
main() | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
int T;
cin >> T;
for (int i = 0; i < T; i++) {
long long h, c, d;
cin >> h >> c >> d;
if (d == h) {
cout << "1"
<< "\n";
} else if (d <= (h + c) / 2) {
cout << "2"
<< "\n";
} else {
long long hotter = 1;
long long coldereq = 1e6;
while (hotter + 1 != coldereq) {
long long mid = (hotter + coldereq) / 2;
long long num = (h * mid + c * (mid - 1));
long long den = (mid * 2 - 1);
if (d * den < num) {
hotter = mid;
} else {
coldereq = mid;
}
}
long long num1 = (h * hotter + c * (hotter - 1));
long long den1 = (hotter * 2 - 1);
long long dif1_n = (num1 - den1 * d);
long long dif1_d = den1;
long long num2 = (h * coldereq + c * (coldereq - 1));
long long den2 = (coldereq * 2 - 1);
long long dif2_n = (d * den2 - num2);
long long dif2_d = (den2);
if (dif2_n * dif1_d < dif1_n * dif2_d) {
cout << coldereq * 2 - 1 << "\n";
} else {
cout << hotter * 2 - 1 << "\n";
}
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #!/usr/bin/env python3
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
# $$ t = (n * h + (n - 1) * c) / (2 * n - 1) $$
# $$ 2 * n * t - t = h * n + c * n - c $$
# $$ n * (2 * t - h - c) = t - c $$
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))
# print("t0", t0)
# print("t1", t1)
# print("n0", n0)
# print("n1", n1)
if t0 <= t1:
print(2 * n0 - 1)
else:
print(2 * n1 - 1)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int tt;
long long h, c, t;
long long k, a, b;
int main() {
scanf("%d", &tt);
while (tt--) {
scanf("%lld%lld%lld", &h, &c, &t);
if (t <= (h + c) / 2)
puts("2\n");
else {
k = (h - t) / (2 * t - h - c);
a = abs(k * (h + c) + h - t * (2 * k + 1)) * (2 * k + 3);
b = abs(((k + 1) * (h + c) + h) - t * (2 * k + 3)) * (2 * k + 1);
if (a > b) k++;
printf("%lld\n", k * 2 + 1);
}
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | def abc(i,j,c):
global a,b
ans=i
while i<=j:
mid = i+(j-i)//2
if (mid*b+(mid+1)*a)<=c*(2*mid+1):
ans=mid
i=mid+1
else:
j=mid-1
return ans
def bca(i,j,c):
global a,b
ans=i
while i<=j:
mid = i+(j-i)//2
if (mid*b+(mid+1)*a)>=c*(2*mid+1):
ans=mid
i=mid+1
else:
j=mid-1
return ans
return ans
t=int(input())
for _ in range(t):
a,b,c=map(int,input().split())
if a==b:
print(1)
continue
x=(a+b)/2
x=abs(c-x)
ans=2
if b>=a:
y=abc(0,10**7,c)
m =abs((y*b+(y+1)*a)-(2*y+1)*c)
y+=1
v=abs((y*b+(y+1)*a)-(2*y+1)*c)
if m==v:
if abs((y*b+(y+1)*a)/(2*y+1)-c)<x:
ans=2*y+1
else:
y-=1
m = (y*b+(y+1)*a)/(2*y+1)
if abs(m-c)<x:
ans = 2*y+1
x=abs(m-c)
y+=1
m = (y*b+(y+1)*a)/(2*y+1)
if abs(m-c)<x:
ans = 2*y+1
x=abs(m-c)
else:
y=bca(0,10**7,c)
#print(y)
m =abs((y*b+(y+1)*a)-(2*y+1)*c)
y+=1
v=abs((y*b+(y+1)*a)-(2*y+1)*c)
if m==v:
if abs((y*b+(y+1)*a)/(2*y+1)-c)<x:
ans=2*y+1
else:
y-=1
m = (y*b+(y+1)*a)/(2*y+1)
if abs(m-c)<x:
ans = 2*y+1
x=abs(m-c)
y+=1
m = (y*b+(y+1)*a)/(2*y+1)
if abs(m-c)<x:
ans = 2*y+1
x=abs(m-c)
print(ans)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import sys
from collections import defaultdict as dd
from collections import deque
from fractions import Fraction as f
def eprint(*args):
print(*args, file=sys.stderr)
zz=1
from math import *
import copy
#sys.setrecursionlimit(10**6)
if zz:
input=sys.stdin.readline
else:
sys.stdin=open('input.txt', 'r')
sys.stdout=open('all.txt','w')
def li():
return [int(x) for x in input().split()]
def fi():
return int(input())
def si():
return list(input().rstrip())
def mi():
return map(int,input().split())
def bo(i):
return ord(i)-ord('a')
tt=fi()
while tt>0:
tt-=1
h,c,t=mi()
if t==h:
print(1)
continue
ans=1
if abs(h-t)<=abs((h+c)/2-t):
ans=1
res=f(h-t)
else:
ans=2
res=abs(f((h+c),2)-f(t))
#print(ans,res)
#print(ans)
if 1:
l=1
r=10**7
while l<=r:
mid=l+(r-l)//2
z=f(h*mid+c*(mid-1),2*mid-1)
if z<=t:
if abs(z-t)<res:
res=abs(z-t)
ans=2*mid-1
#print(z,res,t,2*mid+1)
elif abs(z-t)==res:
ans=min(ans,2*mid-1)
r=mid-1
else:
l=mid+1
if abs(z-t)<res:
#print(z,res,t,2*mid+1)
res=abs(z-t)
ans=2*mid-1
elif abs(z-t)==res:
ans=min(ans,2*mid-1)
#print(l,r,ans,res)
print(ans) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import fractions
for _ in range(int(input())):
h,c,t = list(map(int,input().split()))
if(h-t>=t-c):
print(2)
else:
mind = abs(t-h)
ans = 1
tavg = (h+c+1)//2
if(abs(t-tavg)<mind):
ans = 2
l = 1
r = 10**9
F = fractions.Fraction
while l<=r:
m = (l+r)//2
num = 2*m+1
temp = F(((num+1)*h + (num-1)*c),2*num)
z = abs(t-temp)
if(z<mind):
mind = z
ans = 2*m+1
if(temp>t):
l = m+1
else:
r = m-1
print(ans) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void solve() {
long long a, b, r;
cin >> a >> b >> r;
if (a == r) {
cout << "1" << endl;
} else {
double avg = (a + b) / double(2);
if (r <= avg) {
cout << "2" << endl;
} else {
long long val1 = ceil(double(abs(r - a)) / abs(a + b - 2 * r));
long long val2 = (abs(r - a)) / abs(a + b - 2 * r);
double val11 = (double)(val1 * (a + b) + a) / (2 * val1 + 1);
double val22 = (double)(val2 * (a + b) + a) / (2 * val2 + 1);
if (abs(r - val11) < abs(r - val22)) {
cout << 2 * val1 + 1 << endl;
} else {
cout << 2 * val2 + 1 << endl;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) {
solve();
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | for test in range(int(input())):
h, c, t = map(int, input().split())
if h == t:
print(1)
elif t <= (h + c) / 2:
print(2)
elif abs(t - h) <= abs(t - (2 * h + c) / 3):
print(1)
else:
x = ((h - t) // (2 * t - h - c))
y = x + 1
dif2 = abs((2*y+1)*t-y*h-y*c-h)
dif1 = abs((2*x+1)*t-x*h-x*c-h)
if dif2 * (2 * x + 1) < dif1 * (2 * y + 1):
print(2 * y + 1)
else:
print(2 * x + 1) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
const int mod = 1000000007;
const long long INF = 9000000000000000000;
using namespace std;
void solve() {
double lb, ub, temp, x, y;
cin >> ub >> lb >> temp;
double H, L, mid;
H = ub, L = (lb + ub) / 2;
mid = (ub + ub + lb) / 3;
if (temp >= H)
cout << 1 << endl;
else if (temp <= L)
cout << 2 << endl;
else {
int k = (temp - lb) / (2 * temp - lb - ub);
int k1 = k + 1;
double temp1 = (k * ub + (k - 1) * lb) / (2 * k - 1);
double temp2 = (k1 * ub + (k1 - 1) * lb) / (2 * k1 - 1);
if (abs(temp1 - temp) <= abs(temp2 - temp)) {
cout << 2 * k - 1 << endl;
} else
cout << 2 * k1 - 1 << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long q = 1;
cin >> q;
while (q--) {
solve();
}
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 |
t=int(input())
for i in range (t):
s=input().split()
h=int(s[0])
c=int(s[1])
te=int(s[2])
if te==h:
print("1")
elif h-te==te-c:
print("2")
else:
sum=c+h
if h-te>te-c:
print("2")
else :
a=h-te
b=sum-2*te
f=int(a/b)
f=abs(f)
k=abs(2*f+1)
l=[k-2,k,k+2]
p=[0,0,0]
for j in range (3):
p[j]=(a+(b*(l[j]-1))/2)/(l[j])
p[j]=abs(p[j])
print(l[p.index(min(p))])
'''print(p)
print(l)
print(k)
print(f)
print(a)
print(b)'''
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
inline int read() {
register int x;
register char c(getchar());
register bool k;
while ((c < '0' || c > '9') && c ^ '-')
if ((c = getchar()) == EOF) exit(0);
if (c ^ '-')
x = c & 15, k = 1;
else
x = 0, k = 0;
while (c = getchar(), c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c & 15);
return k ? x : -x;
}
void write(register int a) {
if (a < 0) putchar('-'), a = -a;
if (a <= 9)
putchar(a | '0');
else
write(a / 10), putchar((a % 10) | '0');
}
void read_s(char *s) {
char ch = getchar();
while (ch == '\n') ch = getchar();
int p = 0;
while (ch != '\n') {
s[p++] = ch;
ch = getchar();
}
s[p] = 0;
}
int main() {
int t = read();
while (t--) {
int a = read(), b = read(), x = read();
if (x == a)
cout << 1 << '\n';
else if (x * 2 <= a + b)
cout << 2 << '\n';
else {
int n = (a - x) / (2 * x - a - b);
double tmp1 =
fabs(((double)a * (n + 1) + (double)b * n) / (2 * n + 1) - x);
double tmp2 =
fabs(((double)a * (n + 2) + (double)b * (n + 1)) / (2 * n + 3) - x);
if (tmp1 <= tmp2)
cout << 2 * n + 1 << '\n';
else
cout << 2 * n + 3 << '\n';
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import static java.lang.Math.abs;
public class C_rational {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int test = in.nextInt();
for (int t = 1; t <= test; t++) {
long h = in.nextInt(), c = in.nextInt(), temp = in.nextInt();
Rational TEMP = new Rational(temp, 1);
long lowerEqual = lowerOrEqual(h, c, temp);
long greaterEqual = greaterOrEqual(h, c, temp);
Rational lowerTemp = calcTemp(lowerEqual, h, c), higherTemp = calcTemp(greaterEqual, h, c);
Rational tempFor2 = calcTemp(2, h, c);
Pair pair[] = new Pair[3];
pair[0] = new Pair((TEMP.sub(tempFor2)).abs(), 2);
pair[1] = new Pair((TEMP.sub(lowerTemp)).abs(), lowerEqual);
pair[2] = new Pair((TEMP.sub(higherTemp)).abs(), greaterEqual);
Arrays.sort(pair);
pw.println(pair[0].s);
}
pw.close();
}
static Rational calcTemp(long moves, long h, long c) {
long temp = h * (moves / 2);
if (moves % 2 == 1) temp += h;
temp += c * (moves / 2);
return new Rational(temp, moves);
}
static long lowerOrEqual(long h, long c, long t) {
long l = 0, r = (long) 1e7, res = r;
Rational TEMP = new Rational(t, 1);
while (l <= r) {
long mid = (l + r) / 2;
long moves = (mid * 2) + 1;
Rational temp = calcTemp(moves, h, c);
if (temp.compareTo(TEMP) <= 0) {
res = moves;
r = mid - 1;
} else {
l = mid + 1;
}
}
return res;
}
static long greaterOrEqual(long h, long c, long t) {
long l = 0, r = (long) 1e7, res = r;
Rational TEMP = new Rational(t, 1);
while (l <= r) {
long mid = (l + r) / 2;
long moves = (mid * 2) + 1;
Rational temp = calcTemp(moves, h, c);
if (temp.compareTo(TEMP) >= 0) {
res = moves;
l = mid + 1;
} else {
r = mid - 1;
}
}
return res;
}
static class Pair implements Comparable<Pair> {
Rational f;
long s;
Pair(Rational F, long S) {
f = F;
s = S;
}
public int compareTo(Pair p) {
int cmp = f.compareTo(p.f);
if(cmp != 0) return cmp;
return Long.compare(s, p.s);
}
public String toString() {
return f + " " + s;
}
}
static class Rational implements Comparable<Rational> {
static final Rational ZERO = new Rational(0, 1);
long num, den;
public Rational(long numerator, long denominator) {
long g = gcd(numerator, denominator);
num = numerator / g;
den = denominator / g;
// needed only for negative numbers
if (den < 0) {
den = -den;
num = -num;
}
}
// return the numerator and denominator of (this)
public long numerator() {
return num;
}
public long denominator() {
return den;
}
// return double precision representation of (this)
public double toDouble() {
return (double) num / den;
}
// return string representation of (this)
public String toString() {
if (den == 1) return num + "";
else return num + "/" + den;
}
// return { -1, 0, +1 } if a < b, a = b, or a > b
public int compareTo(Rational b) {
Rational a = this;
BigInteger x = BigInteger.valueOf(a.num).multiply(BigInteger.valueOf(b.den)),
y = BigInteger.valueOf(a.den).multiply(BigInteger.valueOf(b.num));
return x.compareTo(y);
}
// is this Rational object equal to y?
public boolean equals(Object y) {
if (y == null) return false;
if (y.getClass() != this.getClass()) return false;
Rational b = (Rational) y;
return compareTo(b) == 0;
}
// hashCode consistent with equals() and compareTo()
// (better to hash the numerator and denominator and combine)
public int hashCode() {
return this.toString().hashCode();
}
// create and return a new rational (r.num + s.num) / (r.den + s.den)
public static Rational median(Rational r, Rational s) {
return new Rational(r.num + s.num, r.den + s.den);
}
// return gcd(|m|, |n|)
private static long gcd(long m, long n) {
if (m < 0) m = -m;
if (n < 0) n = -n;
if (0 == n) return m;
else return gcd(n, m % n);
}
// return lcm(|m|, |n|)
private static long lcm(long m, long n) {
if (m < 0) m = -m;
if (n < 0) n = -n;
return m * (n / gcd(m, n)); // parentheses important to avoid overflow
}
// return a * b, staving off overflow as much as possible by cross-cancellation
public Rational multiply(Rational b) {
Rational a = this;
// reduce p1/q2 and p2/q1, then multiply, where a = p1/q1 and b = p2/q2
Rational c = new Rational(a.num, b.den);
Rational d = new Rational(b.num, a.den);
return new Rational(c.num * d.num, c.den * d.den);
}
// return a + b, staving off overflow
public Rational add(Rational b) {
Rational a = this;
// special cases
if (a.compareTo(ZERO) == 0) return b;
if (b.compareTo(ZERO) == 0) return a;
// Find gcd of numerators and denominators
long f = gcd(a.num, b.num);
long g = gcd(a.den, b.den);
// add cross-product terms for numerator
Rational s = new Rational((a.num / f) * (b.den / g) + (b.num / f) * (a.den / g),
lcm(a.den, b.den));
// multiply back in
s.num *= f;
return s;
}
// return -a
public Rational negate() {
return new Rational(-num, den);
}
// return |a|
public Rational abs() {
if (num >= 0) return this;
else return negate();
}
// return a - b
public Rational sub(Rational b) {
Rational a = this;
return a.add(b.negate());
}
public Rational reciprocal() {
return new Rational(den, num);
}
// return a / b
public Rational divide(Rational b) {
Rational a = this;
return a.multiply(b.reciprocal());
}
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
} | JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long z[300005];
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long h, c, t;
cin >> h >> c >> t;
if (h + c >= 2 * t) {
cout << 2 << endl;
} else if (h == t)
cout << 1 << endl;
else {
long long l = 1;
long long r = 1000005;
long long mm = 0;
double mi = 100000000.0;
while (l <= r) {
long long m = (l + r) / 2;
double xx = (1.0 * m * h + 1.0 * (m - 1) * c) / (1.0 * (2 * m - 1));
if (xx > t) {
l = m + 1;
} else {
r = m - 1;
}
if (fabs(xx - t) <= mi) {
mi = fabs(xx - t);
mm = m;
}
xx = (1.0 * l * h + 1.0 * (l - 1) * c) / (1.0 * (2 * l - 1));
if (fabs(xx - t) <= mi) {
mi = fabs(xx - t);
mm = l;
}
xx = (1.0 * r * h + 1.0 * (r - 1) * c) / (1.0 * (2 * r - 1));
if (fabs(xx - t) <= mi) {
mi = fabs(xx - t);
mm = r;
}
}
cout << mm * 2 - 1 << endl;
}
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 |
import java.io.*;
import java.util.*;
public class Main {
private static final boolean N_CASE = true;
private void solve() {
long h = sc.nextInt();
long c = sc.nextInt();
long t = sc.nextInt();
if (2 * t <= h + c) {
out.println(2);
return;
}
long k = (h - c) / (2 * t - (h + c));
k = k % 2 == 0 ? k - 1 : k;
if ((k + 2) * k * (4 * t - 2 * (h + c)) - (h - c) * (2 * k + 2) < 0) {
out.println(k+2);
} else {
out.println(k);
}
}
private void run() {
int T = N_CASE ? sc.nextInt() : 1;
for (int t = 0; t < T; ++t) {
solve();
}
}
private static MyWriter out;
private static MyScanner sc;
private static class MyScanner {
BufferedReader br;
StringTokenizer st;
private MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] nextArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
List<Integer> nextList(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(nextInt());
}
return list;
}
}
private static class MyWriter extends PrintWriter {
private MyWriter(OutputStream outputStream) {
super(outputStream);
}
void printArray(int[] a) {
for (int i = 0; i < a.length; ++i) {
print(a[i]);
print(i == a.length - 1 ? '\n' : ' ');
}
}
void printlnArray(int[] a) {
for (int v : a) {
println(v);
}
}
void printList(List<Integer> list) {
for (int i = 0; i < list.size(); ++i) {
print(list.get(i));
print(i == list.size() - 1 ? '\n' : ' ');
}
}
void printlnList(List<Integer> list) {
list.forEach(this::println);
}
}
public static void main(String[] args) {
out = new MyWriter(new BufferedOutputStream(System.out));
sc = new MyScanner();
new Main().run();
out.close();
}
} | JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | 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
# print(check(41,15,2,30))
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):
# twodarr = [[int(x) for x in input().split()] for i in range(rows)] # 2D array row-wise input
# n = int(input())
h, c, t = [int(x) for x in input().split()]
# arr = [int(x) for x in input().split()]
print(solve(h, c, t))
'''
rows, cols = (5, 5)
# 2D array initialization
arr = [[0 for i in range(cols)] for j in range(rows)]
# list created by spliting about spaces
b=input().split()
twodarr = [[int(b[cols*i+j]) for x in range(cols)]
for i in range(rows)] # 2D array Linear Input
# no of rows/cols for 2D array
rows,cols=len(twodarr),len(twodarr[0])
# empty dictionary
dist={}
# empty set
s=set()
import sys
# initializing infinity
a=sys.maxsize
# initializing -infinity
b=-a
'''
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 2e6 + 1;
const long long INF = 9e18;
const int M = 2e3 + 5;
void solve() {
long long h, c, t;
cin >> h >> c >> t;
if ((h + c) >= t * 2) {
cout << 2 << '\n';
return;
}
long long l = 0, r = 1e6;
while ((r - l) >= 0) {
long long mid = (r + l) >> 1;
if ((mid + 1) * h + mid * c >= t * (mid * 2 + 1))
l = mid + 1;
else
r = mid - 1;
}
long long best = 0;
double long s = MOD;
--l;
for (int i = max(0ll, l - 100); i <= l + 100; ++i) {
double long x = i;
double long cur =
(x + (double long)1) * (double long)h + x * (double long)c;
cur /= (double long)(x * 2 + 1);
double m = (double long)t;
if (s > abs(cur - m)) {
s = abs(cur - m);
best = i;
}
}
cout << best * 2 + 1 << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import sys
input = sys.stdin.buffer.readline
for nt in range(int(input())):
h,c,t=map(int,input().split())
m=(h+c)/2
if m>=t:
print (2)
continue
if h<t:
print (1)
continue
# h*x + c*(x-1) = t*(2*x-1)
# x*(h+c)=2tx-t+c
# t-c=x*(2t-h-c)
m=10**18
x = (t-c)/(2*t-h-c)
# print (x)
for i in range(max(1,int(x-10)),int(x+10)):
# print (i,abs(t*(2*i-1)-((h*i+c*(i-1)))),t*(2*i-1),h*i+c*(i-1))
if abs(t*(2*i-1)-((h*i+c*(i-1))))/(2*i-1)<m:
m=abs(t*(2*i-1)-((h*i+c*(i-1))))/(2*i-1)
ans=2*i-1
print (ans) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | /*
*
* @Author Ajudiya_13(Bhargav Girdharbhai Ajudiya)
* Dhirubhai Ambani Institute of Information And Communication Technology
*
*/
import java.util.*;
import java.io.*;
import java.lang.*;
public class Code38
{
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int test_case = in.nextInt();
for(int t=0;t<test_case;t++)
{
long h = in.nextLong();
long c = in.nextLong();
long temp = in.nextLong();
long diff = h+c-2*temp;
if(diff>=0){
pw.println(2);
}
else{
long a = h-temp;
long b = 2*temp - h - c;
long k = 2*(a/b) + 1;
long val1 = (long)Math.abs((k/2)*c + ((k+1)/2)*h - k*temp);
long val2 = (long)Math.abs(((k+2)/2)*c + ((k+3)/2)*h - (k+2)*temp);
long ans = val1*(k+2) <= val2*k ? k : k+2;
pw.println(ans);
}
}
pw.flush();
pw.close();
}
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static int[] suffle(int[] a,Random gen)
{
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
return a;
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
public static HashSet<Integer> primeFactorization(int n)
{
HashSet<Integer> a =new HashSet<Integer>();
for(int i=2;i*i<=n;i++)
{
while(n%i==0)
{
a.add(i);
n/=i;
}
}
if(n!=1)
a.add(n);
return a;
}
public static void sieve(boolean[] isPrime,int n)
{
for(int i=1;i<n;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<n;i++)
{
if(isPrime[i] == true)
{
for(int j=(2*i);j<n;j+=i)
isPrime[j] = false;
}
}
}
public static int GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static void extendedEuclid(int A,int B)
{
if(B==0)
{
d = A;
p = 1 ;
q = 0;
}
else
{
extendedEuclid(B, A%B);
int temp = p;
p = q;
q = temp - (A/B)*q;
}
}
public static long LCM(long a,long b)
{
return (a*b)/GCD(a,b);
}
public static int LCM(int a,int b)
{
return (a*b)/GCD(a,b);
}
public static int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static int modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
public static long modInverse(long A,long M)
{
return modularExponentiation(A,M-2,M);
}
public static boolean isPrime(int n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0)
return false;
for (int i=5; i*i<=n; i=i+6)
{
if (n%i == 0 || n%(i+2) == 0)
return false;
}
return true;
}
static class pair implements Comparable<pair>
{
Integer x, y;
pair(int x,int y)
{
this.x=x;
this.y=y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
return result;
}
public String toString()
{
return x+" "+y;
}
public boolean equals(Object o)
{
if (o instanceof pair)
{
pair p = (pair)o;
return (Math.abs(p.x-x)==0 && Math.abs(p.y-y)==0);
}
return false;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode();
}
}
} | JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | for _ in range(int(input())):
h,c,t=[int(i) for i in input().split()]
if t<=(h+c)/2:
print(2)
else:
l=1
r=10**12
mid=(l+h)/2
while l<r:
mid=(l+r)//2
sum=(h*(mid)+c*(mid-1))/(2*mid-1)
#print(mid,sum)
if sum>t:
l=mid+1
else:
r=mid
l11=abs((h*(l)+c*(l-1))-t*(2*l-1))*(2*(l-1)-1)
l-=1
l22=abs((h*(l)+c*(l-1))-t*(2*l-1))*(2*(l+1)-1)
if l11<l22:
print(2*(l+1)-1)
else:
print(2*l-1)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
bool desc(int a, int b) { return (a > b); }
bool pairone(pair<int, int> &a, pair<int, int> &b) {
return (a.first < b.first);
}
void show(vector<int> vec) {
for (auto x : vec) cout << x << " ";
cout << endl;
;
}
double tn(double h, double c, int n) {
if (n % 2 == 0) {
return (h + c) / 2;
} else {
return (((h + c) / 2) + ((h - c) / (2 * (double)n)));
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int t;
cin >> t;
while (t--) {
int x, y, z;
double h, c, t;
cin >> x >> y >> z;
h = x;
c = y;
t = z;
double t1 = h;
double t2 = tn(h, c, 2);
double t3 = tn(h, c, 3);
if (t <= t2)
cout << 2;
else {
double n = (h - c) / ((2 * t) - (h + c));
int n_down = floor(n);
int n_up = ceil(n);
if (n_down % 2 == 0) n_down--;
if (n_up % 2 == 0) n_up++;
double t_down = 1e9;
double t_up = 1e9;
if (n_down > 0) t_down = tn(h, c, n_down);
t_up = tn(h, c, n_up);
if (abs(t_up - t) < abs(t_down - t))
cout << n_up;
else if (abs(t_down - t) < abs(t_up - t))
cout << n_down;
else if (abs(t_down - t) == abs(t_up - t))
cout << n_down;
}
cout << endl;
;
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | for _ in range(int(input())):
h, c, t = map(int, input().split())
if h==t:
print(1)
elif (t-c<=h-t):
print(2)
else:
temp = (t-h)/(h+c-2*t)
if (int(temp)==temp):
print(int(2*temp+1))
else:
a = int(temp)
b = 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):
print(2*a+1)
else:
print(2*b+1) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
double h, c, t;
cin >> h >> c >> t;
if (h == t) {
cout << 1 << "\n";
} else if (2 * t <= h + c) {
cout << 2 << "\n";
} else {
int x = (c - t) / (h + c - (2 * t));
int y = x + 1;
double a1 = ((h * x) + c * (x - 1)) / (1.0 * (2 * x - 1));
double a2 = ((h * y) + c * (y - 1)) / (1.0 * (2 * y - 1));
if (abs(t - a1) <= abs(t - a2)) {
cout << 2 * x - 1 << "\n";
} else {
cout << 2 * y - 1 << "\n";
}
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | T=int(input())
for _ in range(T):
h,c,t=map(int,input().split())
d=10**9
if t<=(h+c)/2:
print(2)
continue
k=(h-t)//(2*t-h-c)
a=abs((k*(h+c)+h)-(2*k+1)*t)*(2*k+3)
b=abs(((k+1)*(h+c)+h)-(2*k+3)*t)*(2*k+1)
print([2*k+1,2*k+3][a>b]) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | tq=int(input())
for x in range(tq):
m,x,y=list(map(int,input().split()))
if ((m+x)/2>=y):
print(2)
else:
#(k+1)*m+k*x=(2*k+1)*y
k=(m-y)//(2*y-m-x)
ta=((k+1)*m+k*x)*(2*k+3)
tb=((k+2)*m+(k+1)*x)*(2*k+1)
#print((ta-y)*10**6,(tb-y)*10**6,y)
if (abs(tb-y*(2*k+1)*(2*k+3))<abs(ta-y*(2*k+1)*(2*k+3))):
print(2*k+3)
#print("**")
else:
print(2*k+1)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
#pragma GCC target("sse4.2")
long long gcd(long long n, long long m) {
if (n == 0) return m;
return gcd(m % n, n);
}
bool ifprime(long long n) {
if (n == 1) return false;
long long i;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
void disp(vector<long long> v) {
for (auto u : v) cout << u << " ";
cout << "\n";
}
void remleadzero(vector<int>& v) {
vector<int> u;
int i = 0;
while (v[i] == 0) i++;
while (i < v.size()) {
u.push_back(v[i]);
i++;
}
v = u;
}
long long fast_expo(long long n, long long m, long long md) {
long long a = 1;
while (m > 0) {
if (m & 1) a = (a * n) % md;
n = (n * n) % md;
m /= 2;
}
return a % md;
}
inline long long sqroot(long long n) {
long double N = n;
N = sqrtl(N);
long long sq = N - 2;
sq = max(sq, 0LL);
while (sq * sq < n) {
sq++;
}
if ((sq * sq) == n) return sq;
return -1;
}
inline long long sqroot1(long long n) {
long double N = n;
N = sqrtl(N);
long long sq = N - 2;
sq = max(sq, 0LL);
while (sq * sq < n) {
sq++;
}
if ((sq * sq) == n) return sq;
return sq - 1;
}
inline long long cbroot(long long n) {
long double N = n;
N = cbrtl(N);
long long cb = N - 2;
cb = max(cb, 0LL);
while (cb * cb * cb < n) {
cb++;
}
if ((cb * cb * cb) == n) return cb;
return -1;
}
inline long long cbroot1(long long n) {
long double N = n;
N = cbrtl(N);
long long cb = N - 2;
cb = max(cb, 0LL);
while (cb * cb * cb < n) {
cb++;
}
if ((cb * cb * cb) == n) return cb;
return cb - 1;
}
int dig_sum(long long n) {
int ret = 0;
while (n > 0) {
ret += (n % 10);
n /= 10;
}
return ret;
}
int sq(int x) { return x * x; }
int gcd(int n, int m) {
if (n == 0) return m;
return gcd(m % n, n);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rand_num(long long n) {
long long random_number = uniform_int_distribution<long long>(0, n - 1)(rng);
return random_number;
}
chrono::high_resolution_clock::time_point start;
chrono::high_resolution_clock::time_point cur_time;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
void solve_case() {
long double h, c, t;
cin >> h >> c >> t;
if (t == (h + c) / 2)
cout << 2 << '\n';
else {
int val = (h - t) / (2 * t - c - h);
int ans = 1;
long double diff = abs(h - t);
if (abs((c + h) / 2 - t) < diff) {
ans = 2;
diff = abs((c + h) / 2 - t);
}
for (int i = val - 1; i < val + 2; i++) {
long double cur = ((i + 1) * h + i * c) / (2 * i + 1);
if (i >= 0 and abs(cur - t) <= diff) {
if (abs(cur - t) == diff)
ans = min(2 * i + 1, ans);
else
ans = 2 * i + 1;
diff = abs(cur - t);
}
}
cout << ans << '\n';
}
}
void precompute() {}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
;
chrono::high_resolution_clock::time_point start =
chrono::high_resolution_clock::now();
precompute();
int T = 1;
cin >> T;
cout << fixed << setprecision(10);
for (int t = 1; t <= T; t++) {
solve_case();
}
chrono::high_resolution_clock::time_point end =
chrono::high_resolution_clock::now();
long double time_spent =
(chrono::duration_cast<chrono::duration<long double>>(end - start))
.count();
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
long long int power(long long int a, long long int b,
long long int m = 1000000007) {
long long int ans = 1;
a = a % m;
while (b > 0) {
if (b & 1) ans = (1LL * a * ans) % m;
b >>= 1;
a = (1LL * a * a) % m;
}
return ans;
}
long long int ncr(long long int n, long long int r) {
long long int res = 1;
if (r > n - r) r = n - r;
for (long long int i = 0; i < r; i++) {
res *= n - i;
res /= i + 1;
}
return res;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
clock_t time_p = clock();
void rtime() {
time_p = clock() - time_p;
cerr << "******************\nTime taken : "
<< (long double)(time_p) / CLOCKS_PER_SEC << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int tt = 1;
cin >> tt;
long long int ii = 0;
while (tt-- && ++ii) {
long double h, c, t;
cin >> h >> c >> t;
long double temp = (h + c) / 2;
if (t <= temp) {
cout << 2 << "\n";
continue;
}
long double val = (c - t) / (h + c - t - t);
long long int d = val;
long long int u = val + 1;
long double uu = (h * u + (u - 1) * c) / (2 * u - 1);
long double dd = (h * d + (d - 1) * c) / (2 * d - 1);
if (abs(t - dd) <= abs(t - uu))
cout << 2 * d - 1 << "\n";
else
cout << 2 * u - 1 << "\n";
}
rtime();
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int mpow(int base, int exp);
void ipgraph(long long n, long long m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
vector<long long> g[N];
double calc(long long x, double h, double c) {
return ((c + h) * x + h) / (2 * x + 1);
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
long long t, i, x, j, k, p, q, y, u, v, n, m;
cin >> x;
while (x--) {
double h, c, t;
cin >> h >> c >> t;
if (t <= (h + c) / 2) {
cout << 2 << "\n";
continue;
}
long long l = 0, r = 1e9;
while (l < r) {
long long mid = (l + r) / 2;
double val = calc(mid, h, c);
if (val < t)
r = mid;
else
l = mid + 1;
}
if (l == 0) {
cout << "1\n";
} else if (t - calc(l, h, c) < calc(l - 1, h, c) - t)
cout << 2 * l + 1 << "\n";
else
cout << 2 * l - 1 << "\n";
}
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((long long)result * base) % mod;
base = ((long long)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(long long n, long long m) {
int i, u, v;
while (m--) {
cin >> u >> v;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
}
void dfs(int u, int par) {
for (int v : g[u]) {
if (v == par) continue;
dfs(v, u);
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class Solution {
static long h,c,te;
static long gcd(long x,long y){
if(y==0)
return x;
return gcd(y,x%y);
}
static long binary(){
long l=1,r=(long)1e7;
while(l<=r){
if(r-l<3){
long minn=h-te;
long mind=1;
long ans=1;
for(long i=l;i<=r;i++){
long z=calc(i);
if(z<0)
z=z*-1;
long y=2*i+1;
long gcd=gcd(z,y);
z=z/gcd;
y=y/gcd;
if(z*mind<y*minn){
minn=z;
mind=y;
ans=2*i+1;
}
}
return ans;
}
long m=(l+r)/2;
if(calc(m)>0)
r=m;
else
l=m;
}
return 1/0;
}
static long calc(long k){
long time=2*k+1;
long temp=h*(k+1) + c*k;
return te*time-temp;
}
public static void main(String args[]) throws IOException {
FastReader in = new FastReader(System.in);
StringBuilder sb = new StringBuilder();
int i, j;
boolean testcase=true;
int t=testcase?in.nextInt():1;
while(t-->0){
h=in.nextInt();
c=in.nextInt();
te=in.nextInt();
if(te==h){
sb.append("1\n");
}
else if(te<=(h+c)/2)
sb.append("2\n");
else{
long z=binary();
sb.append(z).append("\n");
}
}
System.out.print(sb);
}
}
class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}
| JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
const long double pi = 3.1415926536;
using namespace std;
long double hot, cold, t;
long double avg(long double x, long double y) {
return (long double)(x + y) / 2.0;
}
long double f(int i) {
return (long double)(i * hot + (i - 1) * cold) / (long double)(2.0 * i - 1);
}
void solve() {
int ans = 1;
cin >> hot >> cold >> t;
if (avg(hot, cold) >= t) {
cout << 2 << "\n";
return;
}
if (hot == t) {
cout << 1 << "\n";
return;
}
int low = 0, high = 1000000000, mid;
while (low <= high) {
mid = low + (high - low) / 2;
if (f(mid) > t) {
low = mid + 1;
} else {
high = mid - 1;
}
}
long double eps = (long double)INT_MAX;
for (int i = max(0, low - 5); i < low + 5; i++) {
if (abs(f(i) - t) < eps) {
eps = abs(f(i) - t);
ans = i;
}
}
cout << 2 * ans - 1 << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void solve() {
long long h, c, t;
cin >> h >> c >> t;
if ((h + c) / 2 >= t) {
cout << 2 << '\n';
return;
}
long long a = h - t;
long long b = 2 * t - h - c;
long long k = 2 * (a / b) + 1;
long long val2 =
abs((k + 2) / 2 * 1ll * c + (k + 3) / 2 * 1ll * h - t * 1ll * (k + 2));
long long val1 = abs(k / 2 * 1ll * c + (k + 1) / 2 * 1ll * h - t * 1ll * k);
if (val1 * (k + 2) <= val2 * k) {
cout << k << '\n';
} else
cout << k + 2 << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) solve();
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long double h, c, k, sum = 0, x, l = 1, g = 1, d, y, s, r, q, o;
cin >> h >> c >> k;
if (h == k)
cout << "1\n";
else {
sum = (h + c) / 2;
s = abs(sum - k);
if (sum == k)
cout << "2\n";
else {
g = h - k;
d = 2 * k;
d = d - h - c;
x = ceil(g / d);
y = floor(g / d);
l = (x * (h + c) + h) / (2 * x + 1);
r = abs(k - l);
o = (y * (h + c) + h) / (2 * y + 1);
q = abs(k - o);
if (r < s && r < q && x >= 0)
cout << (long long int)(2 * x + 1) << "\n";
else if (q < s && r >= q && y >= 0)
cout << (long long int)(2 * y + 1) << "\n";
else
cout << "2\n";
}
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | def temp(h,c,cups,k):
if cups%2==0:
return (h+c)//2;
n=cups//2;
return abs((h+c)*n+h-k*cups),cups;
def lessthanore(a,b):
return a[0]*b[1]<=a[1]*b[0];
def check(n,k,h,c):
correct=abs(temp(h,c,2*n+1)-k);
before=abs(temp(h,c,2*n-1)-k);
after=abs(temp(h,c,2*n+3)-k);
print(correct,before,after)
return correct<before and correct<after;
def simcups(h,c,n):
i=0;
temp=0;
while i<=n:
if i%2==0:
temp+=h;
else:
temp+=c;
i+=1;
print(temp/i);
def mincups(h,c,k):
avg=(h+c)/2;
if k<=avg:
return 2;
if k>=h:
return 1;
n=(h-k)/(2*k-h-c)
a=temp(h,c,2*int(n)+1,k);
b=temp(h,c,2*int(n)+3,k);
#print(a,b)
if lessthanore(a,b):
return 2*int(n)+1;
return 2*int(n)+3;
t=int(input())
for i in range(t):
h,c,k=map(int,input().split());
print(mincups(h,c,k)); | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.awt.*;
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Abc {
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int tt=sc.nextInt();
while (tt-->0){
int h=sc.nextInt(),c=sc.nextInt(),t=sc.nextInt();
if (h==t){
System.out.println(1);
continue;
}else if (2*t<=h+c){
System.out.println(2);
continue;
}else {
long fl=(c-t)/(h+c-2*t),cl=(int) Math.ceil((double) (c-t)/(h+c-2*t));
long min1=Long.MAX_VALUE,min2=Long.MAX_VALUE,x1=Long.MAX_VALUE,x2=Long.MAX_VALUE;
// if (fl>0){
x1= Math.abs(fl*h+ (fl-1)*c-(2*fl-1)*t)*(2*cl-1);
// }
// if (cl>0){
x2= Math.abs(cl*h+ (cl-1)*c-(2*cl-1)*t)*(2*fl-1);
// }
if (x1==x2){
System.out.println(Math.min(2*fl-1,2*cl-1));
}else if (x1<x2){
System.out.println(2*fl-1);
}else System.out.println(2*cl-1);
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 1005;
char g[N][N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
long double h, c, t;
cin >> h >> c >> t;
if (t >= h) {
cout << "1\n";
continue;
}
if ((h + c) / 2 >= t) {
cout << "2\n";
continue;
}
int x = 0;
x = (h - t) / (2 * t - h - c);
long double curd = 100000000.0;
int res = -1;
for (int i = max(0, x - 50); i <= x + 50; ++i) {
long double xx = (i + 1) * h + i * c;
xx = 1.0 * xx / (2 * i + 1);
xx = abs(xx - t);
if (xx < curd) {
curd = xx;
res = i;
}
}
cout << 2 * res + 1 << '\n';
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import math
num_cases = int(input())
for _ in range(num_cases):
li = input().strip().split()
h,c,t = [int(i) for i in li]
diff = abs((h+c)/2 - t)
if (h==c):
print(1)
elif (diff==0):
print(2)
else:
n = (t-c)/abs(h+c-2*t)
n1 = max(1,int(n))
n2 = max(1,math.ceil(n))
diff1 = abs(((h+c-2*t)*n1 - c + t)/(2*n1-1))
diff2 = abs(((h+c-2*t)*n2 - c + t)/(2*n2-1))
if (diff1 <= diff2 and diff1 <= diff):
if (diff1 == diff and n1>1):
print(2)
else:
print(2*n1-1)
elif (diff2 < diff):
print(2*n2-1)
else:
print(2)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void solve() {
int h, c, d;
cin >> h >> c >> d;
long double avg = (h + c) / 2;
long double even = abs((long double)d - avg);
long double temp, temp1;
long double odd = 1ll << 60, odd1 = 1ll << 60;
if ((2 * d - h - c) <= 0) {
cout << "2"
<< "\n";
return;
} else {
if ((h - d) % (2 * d - h - c) == 0) {
temp = (h - d) / (2 * d - h - c);
temp1 = temp;
odd = ((temp + 1) * (long double)h + (temp * (long double)c)) /
(2 * temp + 1);
} else {
temp = (h - d) / (2 * d - h - c);
temp = floor(temp);
temp++;
temp1 = temp;
odd = ((temp + 1) * (long double)h + (temp * (long double)c)) /
(2 * temp + 1);
if (temp > 0) {
temp--;
odd1 = ((temp + 1) * (long double)h + (temp * (long double)c)) /
(2 * temp + 1);
}
}
}
odd = abs((long double)d - odd);
odd1 = abs((long double)d - odd1);
if (odd < even || odd1 < even) {
if (odd < odd1) {
cout << 2 * temp1 + 1 << "\n";
} else {
cout << 2 * temp + 1 << "\n";
}
} else {
if (odd == even) {
cout << min(2 * temp + 1, (long double)2) << "\n";
} else {
cout << "2"
<< "\n";
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t;
cin >> t;
while (t--) {
solve();
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import math
t=int(input())
for _ in range(t):
h,c,t=map(int,input().split())
even_last=h+c-t*2
if(even_last>=0):
print(2)
else:
a=h-t
b=2*t-(h+c)
k=(2*(a//b)+1)
v1=abs((k//2*c+(k+1)//2*h)-k*t)
v2=abs(((k+2)//2*c+(k+3)//2*h)-(k+2)*t)
if(v1*(k+2)<=v2*k):
print(k)
else:
print(k+2)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | for _ in range(int(input())):
h,c,t=[int(x) for x in input().split()]
if t==h:
print(1)
elif (h+c)/2>=t:
print(2)
else:
m=int((t-c)/(2*t-c-h));temp=abs((m*(h+c-2*t)+t-c)/(2*m-1));temp1=abs(((m+1)*(h+c-2*t)+t-c)/(2*(m+1)-1))
print(2*m-1 if temp<=temp1 else 2*m+1)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import sys
import string
input = sys.stdin.readline
import math
#import numpy
#letters = list(string.ascii_lowercase)
from decimal import Decimal
l = int(input())
for i in range(l):
n = list(map(int, input().split()))
a,b = n[0] - n[1], n[2] - n[1]
#print(a,b)
try:
s = int(2/(2 - a/b)) - 3
#s = abs(s)
except:
s = 0
#print(s)
best = 99999999
for j in range(max(s//2,0), s//2 + 3):
p = Decimal((a * (j+1)))
r = Decimal((2*j + 1))
m_num = p/r
#print(m_num)
m_num = abs(m_num - b)
#print(j, 'j', m_num)
if m_num < best:
best = m_num
ans = 2*j + 1
if a<=b:
print(1)
elif b<=a/2:
print(2)
else:
if best < abs(a-b):
print(ans)
else:
print(1)
#print((24999 * 10**5) / 49999)
#print((25000 * 10**5) / 50000)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | # ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io --------------------
import decimal
import math
testcases=int(input())
for j in range(testcases):
h,c,t=map(int,input().split())
#we see that this converges to (h+c)/2, if after 2 more cup pours and the abs difference increases then we stop
val1=h+c+t
if h+c-2*t==0:
val1=2
val2=h+c+t
if h+c-2*t!=0:
val2=(t-h)/(h+c-2*t)
n1=(t-h)//(h+c-2*t)
n2=math.ceil((t-h)/(h+c-2*t))
if abs((2*n2+1)*(((n1+1)*h+n1*c)-(2*n1+1)*t))<=abs((2*n1+1)*(((n2+1)*h+(n2)*c)-(2*n2+1)*t)):
val2=max(2*((t-h)//(h+c-2*t))+1,1)
else:
val2=max(2*math.ceil((t-h)/(h+c-2*t))+1,1)
val3=h+c+t
if t==h:
val3=1
if abs(t-h)<=abs(t-c):
print(min(val1,val2,val3))
else:
print(2) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | // Working program with FastReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class C_Mixing_Water{
public static void main(String[] args) {
FastReader sc=new FastReader();
PrintWriter out = new PrintWriter(System.out);
int test = sc.nextInt();
for(int t=0;t<test;t++){
double a = sc.nextInt();
double b = sc.nextInt();
double c = sc.nextInt();
if(c>=a)out.println(1);
else{
// double diff = Math.abs(a-c);
// double currentDiff = Double.MAX_VALUE;
// int n = 1;
// int m = 0;
// boolean hot = false;
// while(true){
// if(hot){
// n++;
// hot = false;
// double temp = ((n*a)+((m*b)))/(n+m);
// currentDiff = Math.abs(temp-c);
// if(currentDiff>=diff){
// n--;
// break;
// }
// else {
// diff = currentDiff;
// }
// }
// else{
// m++;
// hot = true;
// double temp = ((n*a)+((m*b)))/(n+m);
// currentDiff = Math.abs(temp-c);
// if(currentDiff>=diff){
// m--;
// break;
// }
// else {
// diff = currentDiff;
// }
// }
// }
// out.println(n+m);
double diff = Math.abs(a-c);
int ans = 1;
double avg = (double)(a+b)/(double)2;
if(diff>Math.abs(avg-c)){
diff = Math.abs(avg-c);
ans = 2;
}
// n n-1
double num = b-c;
double dem = a+b-(2*c);
double div = num/dem;
int n = (int)Math.round(div);
double temp = (double)((n*a)+((n-1)*b))/(double)(2*n-1);
double temp1 = Math.abs(temp-c);
if(diff>temp1){
diff = temp1;
ans = n+n-1;
}
n++;
temp = (double)((n*a)+((n-1)*b))/(double)(2*n-1);
temp1 = Math.abs(temp-c);
if(diff>temp1){
diff = temp1;
ans = n+n-1;
}
// n+1 n
num = c-a;
dem = a+b-(2*c);
div = num/dem;
n = (int)Math.round(div);
temp = (double)((n*b)+((n+1)*a))/(double)(2*n+1);
temp1 = Math.abs(temp-c);
if(diff>temp1){
diff = temp1;
ans = n+n+1;
}
n++;
temp = (double)((n*b)+((n+1)*a))/(double)(2*n+1);
temp1 = Math.abs(temp-c);
if(diff>temp1){
diff = temp1;
ans = n+n+1;
}
if(ans<0){
ans = 2;
}
out.println(ans);
}
}
out.close();
}
public static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | from fractions import Fraction
T = int(input())
for _ in range(T):
H,C,T = map(int,input().split())
if T*2 <= H+C:
print(2)
continue
else:
def is_ok(k):
return T*(2*k-1) <= H*k + C*(k-1)
ok = 1
ng = 10**18
while ng-ok > 1:
m = (ok+ng)//2
if is_ok(m):
ok = m
else:
ng = m
h1 = Fraction(H*ok + C*(ok-1), 2*ok-1)
h2 = Fraction(H*ng + C*(ng-1), 2*ng-1)
d1,d2 = abs(T-h1), abs(T-h2)
if d1 <= d2:
print(ok*2-1)
else:
print(ng*2-1)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import math
from decimal import *
def more_precise(t, C, h, r):
return abs(Decimal(t - C) - Decimal((h - C) / r))
def get_closer(t, C, h, r1, r2):
d1 = abs(t - (C + ((h - C) / r1)))
d2 = abs(t - (C + ((h - C) / r2)))
#print(r1, r2, d1, d2, d1 == d2)
if d1 == d2:
d1 = more_precise(t, C, h, r1)
d2 = more_precise(t, C, h, r2)
#print(r1, r2, d1, d2, d1 == d2)
if d1 <= d2:
return r1
else:
return r2
def main():
T = int(input())
for _ in range(T):
h, c, t = map(int, input().split())
C = (h + c) / 2
if C >= t:
print(2)
continue
elif h <= t:
print(1)
continue
r = ((h - C) / (t - C))
ceil, floor = math.ceil(r), math.floor(r)
if ceil % 2 == 0:
ceil += 1
if floor % 2 == 0:
floor -= 1
print(get_closer(t, C, h, floor, ceil))
main() | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import sys
from decimal import *
input=sys.stdin.buffer.readline
def tempDec(nHot,h,c): #for higher precision
return Decimal(nHot*h+(nHot-1)*c)/(2*nHot-1) #for more precision
nTests=int(input())
for _ in range(nTests):
h,c,t=[int(zz) for zz in input().split()]
#Observe that for every nHot==nCold (nCups//2==0), finalt=(h+c)/2.
#if t<=(h+c)/2, ans=2
#else, find temp(nHot) just larger than t and find temp(nHot+1) or temp(nHot) is closer
if t<=(h+c)/2:
print(2)
elif t==h:
print(1)
else:
nHot=(t-c)//(2*t-h-c)
a,b = abs(tempDec(nHot,h,c)-t),abs(tempDec(nHot+1,h,c)-t)
if b<a:
print((nHot+1)*2-1)
else:
print(nHot*2-1) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import fractions
def calc_temp(h, c, curr):
numerator = h * curr[0] + c * curr[1]
denominator = curr[0] + curr[1]
return fractions.Fraction(numerator, denominator)
def solve(h, c, t):
if h == t or t >= (c + 5 / 6 * (h - c)):
return 1
if t <= (h + c) / 2:
return 2
start = 1
end = pow(10, 10)
curr = None
while start <= end:
mid = (start + end) // 2
curr = (mid + 1, mid)
current_temperature = calc_temp(h, c, curr)
if current_temperature == t:
return curr[0] + curr[1]
if current_temperature < t:
end = mid - 1
if current_temperature > t:
start = mid + 1
ans = None
deviation = h - t
for i in [-1, 0, 1]:
temp = (curr[0] + i, curr[1] + i)
current_temperature = calc_temp(h, c, temp)
if abs(current_temperature - t) < deviation:
deviation = abs(current_temperature - t)
ans = temp[0] + temp[1]
return ans
def main():
t = int(input())
test_case_number = 1
while test_case_number <= t:
h, c, temp = map(int, input().split())
print(solve(h, c, temp))
test_case_number += 1
main()
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
h, c, t = map(int, input().split())
if t*2<=h+c:
print(2)
continue
f = lambda x: x*h + (x-1)*c
ok = 1
ng = 10**6
while abs(ok-ng)>1:
mid = (ok+ng)//2
if f(mid)>=t*(mid*2-1):
ok = mid
else:
ng = mid
if abs(t*(ok*2-1)-f(ok))*(2*ok+1)<=abs(t*(ok*2+1)-f(ok+1))*(2*ok-1):
print(ok*2-1)
else:
print(ok*2+1) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | T = int(input())
for _ in range(T):
h, c, t = map(int, input().split())
s = (h + c) / 2
if s >= t:
print(2)
continue
s = t - s
a = (h - c) / (2 * s)
k = int((a + 1) // 2)
if (h - c) / (4 * k - 2) - s <= s - (h - c) / (4 * k + 2):
print(2 * k - 1)
else:
print(2 * k + 1) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import sys
input = sys.stdin.readline
from fractions import *
def go(mid, h, c):
return Fraction(h * mid + c * (mid - 1), mid + mid - 1)
MAX_HI = 3010101
def solve_case(input_text):
h, c, t = map(int, input_text.rstrip().split())
if Fraction(h + c, 2) >= t:
return 2
lo, hi = 1, MAX_HI
while lo + 1 != hi:
mid = (lo + hi) // 2
if t <= go(mid, h, c):
lo = mid
else:
hi = mid
ans = lo
anst = abs(t - go(lo, h, c))
for i in range(max(lo - 3, 1), lo + 3):
tmp = abs(t - go(i, h, c))
if tmp < anst :
ans = i
anst = tmp
return ans * 2 - 1
tc = int(input())
for tci in range(tc):
print(solve_case(input()))
# tc = 30000
# for tci in range(tc) :
# print(solve_case('1000000 1 512345')) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | for _ in range(int(input())):
h,c,t=map(int,input().split())
if(h==t):
print(1)
else:
avg=(h+c)/2
if(avg==t):
print(2)
elif(avg>t):
print(2)
else:
dif=(t-avg)
j=int(abs((c-h)//(2*dif)))
if(j%2==0):
j+=1
if(dif-(h-c)/(2*j)>=abs(dif-(h-c)/(2*(j-2)))):
print(j-2)
else:
print(j) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | def ab(a,b):
if a>b:
return a-b
return b-a
def abc(a):
if a>=0:
return a
return -a
for _ in range(input()):
h,c,t=map(int,raw_input().split())
h-=c;t-=c;
if (2*t)<=(h):
print 2
continue
p=(h-t)*(1.0)/(2*t-h)
if p==(int(p)):
print int(2*p+1);continue
mans=ab(t,(h)/2.0)
ans=2
p=int(p)
for j in range(2):
k=p+j
if k<0:
continue
v=((k+1)*h)-((2*k+1.0)*t)
if abc(v)<(mans*(2*k+1)):
mans=abc(v)/(2*k+1.0);ans=(2*k+1)
print ans
| PYTHON |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | from collections import defaultdict as dd
from collections import deque
import bisect
import heapq
def ri():
return int(input())
def rl():
return list(map(int, input().split()))
def solve():
h, c, t = rl()
h, c, t = h - c, 0, t - c
if 2*t <= h:
print (2)
return
elif 6 * t >= 5 * h:
print (1)
return
else:
x = t // (2 * t - h)
while h * x <= t * (2 * x - 1):
x -= 1
while h * (x+1) > t * (2 * x + 1):
x += 1
y = x + 1
# if (x * h) / (2 * x - 1) - t <= t - (y * h) / (2 * y - 1):
if 2 * t * (2*x-1) * (2*y-1) >= x*h*(2*y-1) + y*h*(2*x-1):
print (2 * x - 1)
else:
print (y * 2 - 1)
mode = 'T'
if mode == 'T':
t = ri()
for i in range(t):
solve()
else:
solve()
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.io.*;
import java.io.IOException;
import java.util.*;
//import javafx.util.Pair;
//import java.util.concurrent.LinkedBlockingDeque;
//import sun.net.www.content.audio.wav;
public class Codeforces {
public static long mod = (long)Math.pow(10, 9)+7 ;
public static double epsilon=0.00000000008854;//value of epsilon
public static InputReader sc = new InputReader(System.in);
public static PrintWriter pw = new PrintWriter(System.out);
public static int firstNode(int a,HashMap<Integer,Integer> p){
if(p.containsKey(a)){
p.put(a, firstNode(p.get(a), p));
}
else return a;
return p.get(a);
}
public static void Union(int a,int b,HashMap<Integer,Integer> p){
//int a=firstNode(a1, p);
//int b=firstNode(b1, p);
/*if(a!=b){
if(r[a]<r[b]){
p[a]=b;
}
else if(r[a]>r[b]){
p[b]=a;
}
else{
r[b]++;
p[a]=b;
}
}*/
a=firstNode(a, p);
b=firstNode(b, p);
if(a!=b)
p.put(a, b);//if no rank than
}
public static ArrayList<ArrayList <Integer>> GetGraph(int n,int m){
ArrayList<ArrayList <Integer>> a=new ArrayList<>();
for(int i=0;i<n;i++){
a.add(new ArrayList<>());
}
for(int i=0;i<m;i++){
int u=sc.nextInt()-1;
int v=sc.nextInt()-1;
a.get(u).add(v);
a.get(v).add(u);
}
return a;
}
public static void dfs(char a[][],int vis[][],int x,int y){
int n=a.length;
int m=a[0].length;
if(x<0||x>=n||y<0||y>=m){
return;
}
if(vis[x][y]==1)
return;
if(a[x][y]=='G'||a[x][y]=='.'){
vis[x][y]=1;
dfs(a, vis, x+1, y);
dfs(a, vis, x-1, y);
dfs(a, vis, x, y+1);
dfs(a, vis, x, y-1);
}
}
public static void main(String[] args) {
// code starts..
int q=sc.nextInt();
while(q-->0){
long min=Long.MAX_VALUE;
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
long t=2;
min=Math.abs(a+b-2*c);
long l=0,r=100000000;
while(l<r){
long mid=(l+r)/2;
//pw.println("kefnkenf "+mid);
long v=(2*mid+1)*c-mid*(a+b)-a;
if(v>0){
r=mid-1;
}
else{
l=mid+1;
}
if((2*mid+1)*min>t*Math.abs(v)||((2*mid+1)*min==t*Math.abs(v)&&t>(2*mid+1))){
t=2*mid+1;
min=Math.abs(v);
}
// pw.println(t+" "+min);
}
long v=(2*l+1)*c-l*(a+b)-a;
if((2*l+1)*min>t*Math.abs(v)||((2*l+1)*min==t*Math.abs(v)&&t>(2*l+1))){
t=2*l+1;
min=Math.abs(v);
}
//pw.println(t+" "+min);
pw.println(t);
}
// Code ends...
pw.flush();
pw.close();
}
public static Comparator<Integer[]> column(int i){
return
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
return o1[i].compareTo(o2[i]);//for ascending
//return o2[i].compareTo(o1[i]);//for descending
}
};
}
public static String reverseString(String s){
StringBuilder input1 = new StringBuilder();
input1.append(s);
input1 = input1.reverse();
return input1.toString();
}
public static int[] scanArray(int n){
int a[]=new int [n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
return a;
}
public static long[] scanLongArray(int n){
long a[]=new long [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLong();
return a;
}
public static String [] scanStrings(int n){
String a[]=new String [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLine();
return a;
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class SegmentTree{
int s[],n;
SegmentTree(int a[]){
n=a.length;
int l=(int)Math.ceil(Math.log(n)/Math.log(2));
l=2*(int)Math.pow(2,l)-1;
s=new int[l];
createSegmentTreeUtil(a, 0, 0, a.length-1);
}
int createSegmentTreeUtil(int a[],int root,int l,int r){
if(l==r)
s[root]=a[l];
else
s[root]= Compare(createSegmentTreeUtil(a, 2*root+1, l, (l+r)/2), createSegmentTreeUtil(a,2*root+2, (l+r)/2+1,r));
return s[root];
}
int getValue(int gl,int gr){
return getValueUtil(0, 0, n-1, gl, gr);
}
int getValueUtil(int root,int l,int r,int gl,int gr){
if(l>=gl&&r<=gr){
return s[root];
}
if(l>gr||r<gl){
return 0;
}
return Compare(getValueUtil(2*root+1, l, (l+r)/2, gl, gr), getValueUtil(2*root+2, (l+r)/2+1, r, gl, gr));
}
void update(int value,int index){
updateUtil(value, index,0,0,n-1);
}
int updateUtil(int p,int k,int root,int l,int r){
if(l==r&&l==k){
return s[root]=p;
}
else if(l>k||r<k)
return s[root];
else{
return s[root]=Compare(updateUtil(p, k, 2*root+1, l, (l+r)/2), updateUtil(p, k, 2*root+2,(l+r)/2+1,r ));
}
}
int Compare(int a,int b){
return Math.max(a, b);
}
}
| JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | from fractions import Fraction as f
for tc in xrange(input()):
H, C, T = map(int, raw_input().split())
ans = f(H + C, 2)
cups = 2
if abs(H - T) <= abs(ans - T):
ans = H
cups = 1
if H + C != 2 * T:
n = (T - H) / (H + C - 2 * T)
for itr in range(max(0, n - 2), n + 2):
a, b = itr + 1, itr
temp = f(a * H + b * C, a + b)
if abs(temp - T) < abs(ans - T):
ans = temp
cups = a + b
print cups | PYTHON |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | # -*- coding: utf-8 -*-
import sys
from collections import defaultdict, deque
def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]
from fractions import Fraction as F
# sys.setrecursionlimit(int(1e8))
def f(h, c, m, t):
return h * (m + 1) + c * m
def solve():
h, c, t = [int(x) for x in input().split()]
if (h + c) >= t * 2:
print(2)
return
delta = F(abs(t - h))
ans = 1
l = 0
r = 10 ** 18
while l + 1 < r:
m = (l+r) // 2
if f(h,c,m,t) > t * (2*m+1):
l = m
else:
r = m
for l in range(l, r+3):
if abs(F(t)-F(f(h,c,l,t),(2*l+1))) < delta:
delta = abs(F(t)-F(f(h,c,l,t),(2*l+1)))
ans = (2*l+1)
print(ans)
t = 1
t = int(input())
for case in range(1,t+1):
ans = solve()
"""
"""
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
float avg1(long long int n, long long int h, long long int c) {
return (float)((n + 1) * h + (n - 1) * c) / (2 * n);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt;
cin >> tt;
for (int pp = 0; pp < tt; pp++) {
long long int h, c, t;
cin >> h >> c >> t;
long long int ans = 0;
double avg = (h + c) / 2;
if (t >= h) {
ans = 1;
} else if (t <= avg) {
ans = 2;
} else {
double n = (float)(c - h) / (h + c - 2 * t);
long long int r = ceil(n);
long long int l = floor(n);
float diff1, diff2;
if (l % 2 == 0) l--;
if (r % 2 == 0) r++;
diff1 = avg1(l, h, c) - (float)t;
diff2 = (float)t - avg1(r, h, c);
if (diff1 <= diff2)
ans = l;
else {
ans = r;
}
}
cout << ans << '\n';
}
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int test;
scanf("%d", &test);
while (test--) {
float h, c, t;
scanf("%f %f %f", &h, &c, &t);
if (h == t) {
printf("1\n");
} else if (2 * t <= h + c) {
printf("2\n");
} else {
int x = (t - c) / (2 * t - c - h);
int y = x + 1;
float temp1, temp2;
temp1 = (h * x + c * (x - 1)) / (2 * x - 1);
temp2 = (h * y + c * (y - 1)) / (2 * y - 1);
float diff1, diff2;
diff1 = abs(temp1 - t);
diff2 = abs(temp2 - t);
if (diff1 <= diff2) {
printf("%d\n", 2 * x - 1);
} else {
printf("%d\n", 2 * y - 1);
}
}
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import math
from decimal import *
t = int(input())
def tg(h, c, x):
return Decimal(h+((h+c)*x))/Decimal((2*x)+1)
for _ in range(t):
h, c, t = map(int, input().split())
# print(Decimal((t-h)/(h+c-(2*t))))
if h == t:
print(1)
elif (h+c)/2 >= t:
print(2)
else:
x = math.floor(Decimal((t-h)/(h+c-(2*t))))
y = math.ceil(Decimal((t-h)/(h+c-(2*t))))
if abs(t-tg(h, c, x)) <= abs(t-tg(h, c, y)):
print(1+int(2*x))
else:
print(1+int(2*y))
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class First {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
//int a = 1;
int t;
//t = in.nextInt();
t = 1;
while (t > 0) {
//out.print("Case #"+(a++)+": ");
solver.call(in, out);
t--;
}
out.close();
}
static class TaskA {
long h, c, t;
long[] arr;
public void call(InputReader in, PrintWriter out) {
arr = new long[1000000];
long j = 1;
for (int i = 0; i < 1000000; i++) {
arr[i] = j;
j+=2;
}
int t1;
t1 = in.nextInt();
//t = 1;
while (t1 > 0) {
//out.print("Case #"+(a++)+": ");
h = in.nextLong();
c = in.nextLong();
t = in.nextLong();
int l = -1, r = 1000000, mid;
while (l + 1 < r) {
mid = (l + r) / 2;
if (ans(mid) >= arr[mid] * t) {
l = mid;
} else {
r = mid;
}
}
if(r==1000000){
out.println(2);
}
else {
if(Math.abs(ans(l) - t*arr[l]) * arr[r] > Math.abs(ans(r) - t*arr[r]) * arr[l]){
out.println(arr[r]);
}
else {
out.println(arr[l]);
}
}
t1--;
}
}
public long ans(int mid){
long a;
long b = arr[mid]/2;
a = (b+1)* h + (b)*c;
return a;
}
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static class answer implements Comparable<answer>{
int a;
int b;
public answer(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
return o.a - this.a;
}
}
static class answer1 implements Comparable<answer1>{
int a, b, c;
public answer1(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public int compareTo(answer1 o) {
return this.a - o.a;
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static final Random random=new Random();
static void shuffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | from collections import *
from bisect import *
from math import *
from heapq import *
from fractions import *
import sys
input=sys.stdin.readline
t1=int(input())
while(t1):
t1-=1
h,c,t=map(int,input().split())
if(h==t):
print(1)
continue
try:
x=(c-t)/(h+c-(2*t))
except:
print(2)
continue
if((h+c)/2>=t):
print(2)
continue
x=int(x)
avg1=(x+1)*h
avg1+=x*c
avg1=Fraction(avg1,2*x+1)
ans=2*x+1
r=abs(avg1-t)
x-=1
avg1=(x+1)*h
avg1+=x*c
avg1=Fraction(avg1,2*x+1)
if(abs(avg1-t)<=r):
ans=2*x+1
r=abs(avg1-t)
print(ans)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | from fractions import Fraction
from sys import exit
for _ in range(int(input())):
h, c, t = map(int, input().split())
# h, c, t = map(int, '41 15 30'.split())
if t >= h:
print(1)
continue
# n = int(input())
# arr = list(map(int, input().split()))
sr = Fraction(h + c, 2)
if t <= sr:
print(2)
continue
p = Fraction(t - h, h + c - 2 * t)
aa = None
pans = -1
kek = int(p)
for pp in range(max(0, kek - 3), max(1, kek + 3)):
a = Fraction(pp * (h + c) + h, 2 * pp + 1)
if aa is None or abs(a - t) < abs(aa - t):
pans = pp
aa = a
print(2 * pans + 1)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | def f(x):
return abs(((x-1)*(c+h)+h)/(2*x-1)-temp)
t=int(input())
for you in range(t):
l=input().split()
h=int(l[0])
c=int(l[1])
temp=int(l[2])
if(temp<=(c+h)/2):
print(2)
elif(temp>=h):
print(1)
else:
poss=int((temp-c)/(2*temp-h-c))
num1=2*temp*(2*poss-1)*(2*poss+1)
num2=((poss-1)*(c+h)+h)*(2*poss+1)+(poss*(c+h)+h)*(2*poss-1)
if(num1>=num2):
print(2*poss-1)
else:
print(2*poss+1)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import math
from fractions import Fraction
def dist(x, y):
return abs(x-y)
for _ in range(int(input())):
h, c, t = map(int, input().strip().split())
c0 = (c-t)/(h+c-2*t) if h+c != 2*t else 0
c1 = max(int(math.ceil(c0)), 1)
c2 = max(int(math.floor(c0)), 1)
x1 = (dist(t, Fraction(h+c, 2)), 2)
x2 = (dist(t, Fraction(c1*h+(c1-1)*c, 2*c1-1)), 2*c1-1)
x3 = (dist(t, Fraction(c2*h+(c2-1)*c, 2*c2-1)), 2*c2-1)
print(min(x1, x2, x3)[1])
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class q3 {
public static void main(String[] args) {
FastReader s = new FastReader();
int t = s.nextInt();
while(t-- > 0)
{
long h = s.nextInt();
long c = s.nextInt();
long temp = s.nextInt();
if(temp <= (h+c)/2)
System.out.println(2);
else
{
double k = (h - temp)/(double)(2*temp - h - c);
long kint = (long)Math.floor(k);
double l = ( (kint+1)*h + kint*c)*(2*kint+3) - temp*(2*kint+3) * (2*kint+1);
double r = ( (kint+2)*h + (kint+1)*c) * (2*kint+1) - temp*(2*kint+1)*(2*kint+3);
if(Math.abs(l) <= Math.abs(r))
System.out.println(2*kint+1);
else System.out.println(2*kint+3);
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | from fractions import Fraction
import sys
input = sys.stdin.readline
t=int(input())
g= [list(map(Fraction,input().split())) for i in range(t)]
ans=[0]*t
for i in range(t):
h=g[i][0]
c=g[i][1]
x=g[i][2]
if x>=h:
ans[i]=1
elif x<=(h+c)/2:
ans[i]=2
else:
l = -1
r = pow(10, 8)
while r - l > 1:
# rは常にOK、lは常にNG。
mid = l + (r - l) // 2
# なんらかの判定
v=2*mid+1
if Fraction((c*mid+h*(mid+1))/v)<x:
r = mid # 判定がOKのとき→範囲を左に狭める
else:
l = mid # 判定がNGのとき→範囲を右に狭める
if Fraction(abs(x-(c*r+h*(r+1))/(r*2+1)))<Fraction(abs(x-(c*(r-1)+h*r)/(r*2-1))):
ans[i]=r*2+1
else:
ans[i]=r*2-1
for i in range(t):
print(ans[i]) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | for _ in range(int(input())):
h,c,t=map(int,input().split())
if(h==t):
print(1)
else:
xx=(h+c)/2
if(xx==t):
print(2)
elif(xx>t):
print(2)
else:
dif=(t-xx)
j=int(abs((c-h)//(2*dif)))
if(j%2==0):
j+=1
if(dif-(h-c)/(2*j)>=abs(dif-(h-c)/(2*(j-2)))):
print(j-2)
else:
print(j) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const long long mod = 1e9 + 7;
const int mxN = 5e5 + 9;
void solve() {
long long x, y, z;
cin >> x >> y >> z;
vector<pair<double, long long> > ans;
ans.push_back(make_pair((double)abs(z - ((x + y) / 2)), 2));
long long A = y - z;
long long B = x + y - 2 * z;
if (B != 0 && A % B == 0 && (A / B) >= 1) {
long long a1 = A / B;
ans.push_back(make_pair((double)0, 2 * a1 - 1));
} else if (B != 0 && (A / B) >= 1) {
long long a1 = A / B;
long long a2 = a1 + 1;
double r1 = a1 * x + (a1 - 1) * y;
double r2 = (2 * a1 - 1);
double r3 = a2 * x + (a2 - 1) * y;
double r4 = 2 * a2 - 1;
ans.push_back(make_pair(abs(r1 / r2 - ((double)z)), 2 * a1 - 1));
ans.push_back(make_pair(abs(r3 / r4 - ((double)z)), 2 * a2 - 1));
}
sort(ans.begin(), ans.end());
cout << ans[0].second << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll power(ll x, ll y);
const ll MOD = 1000000007;
double h, c, t;
int f() {
if (2 * t <= h + c) return 2;
int L = 1, R = h, M;
while (L <= R) {
M = (L + R) / 2;
if (((h + c) * M - c) / (2 * M - 1) > t)
L = M + 1;
else
R = M - 1;
}
double A = fabs(((h + c) * L - c) / (2 * L - 1) - t);
double B = fabs(((h + c) * R - c) / (2 * R - 1) - t);
if (R == 0 || A < B) return 2 * L - 1;
return 2 * R - 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ts;
cin >> ts;
while (ts--) {
cin >> h >> c >> t;
cout << f() << "\n";
}
}
ll power(ll x, ll y) {
ll res = 1;
x %= MOD;
while (y > 0) {
if (y & 1) res = (res * x) % MOD;
y = y >> 1, x = (x * x) % MOD;
}
return res;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.util.*;
import java.io.*;
public class GFG {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int t1=sc.nextInt();
while (t1-->0){
long h=sc.nextLong();
long c=sc.nextLong();
long t=sc.nextLong();
if(t<=(h+c)/2){
System.out.println(2);continue;}
long k=(h-t)/(2*t-h-c);
long a=(long)Math.abs(k*(h+c)+h-t*(2*k+1));
long b=(long)Math.abs((k+1)*(h+c)+h-t*(2*k+3));
if(a*(2*k+3)<=b*(2*k+1)){
System.out.println(2*k+1);continue;}
System.out.println(2*k+3);
}
}
} | JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | for i in range(int(input())):
h,c,t=map(int,input().split())
bb=(h+c)/2
if t<=bb:
print(2)
else:
k= (h-t)/(2*t-h-c)
k=int(k)
if abs((k*(h+c)+h)-t*(2*k+1))*(2*k+3)<=abs(((k+1)*(h+c)+h)-t*(2*k+3))*(2*k+1):
print(2*k+1)
else:
print(2*(k+1)+1)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long T;
cin >> T;
while (T--) {
long double h, c, t;
cin >> h >> c >> t;
if (t == h) {
cout << 1 << "\n";
continue;
}
if (t <= (h + c) / 2) {
cout << 2 << "\n";
continue;
}
long double n = (t - c) / (2 * t - (h + c));
long long sly = ceil(n);
long long fly = floor(n);
long double t1 = ((sly - 1) * (h + c) + h) / (2 * sly - 1);
long double t2 = ((fly - 1) * (h + c) + h) / (2 * fly - 1);
if (abs(t - t1) < abs(t - t2)) {
cout << 2 * sly - 1 << "\n";
} else
cout << 2 * fly - 1 << "\n";
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | from sys import stdin, stdout
from decimal import *
cin = stdin.readline
cout = stdout.write
for _ in range(int(cin())):
h, c, t = map(int, cin().split())
if max((h+c)/2, t) - min((h+c)/2, t) <= 1e-6:
cout('2\n')
continue
i = (c-t) // (h+c-t-t)
if i <= 0:
cout('2\n')
continue
diff = Decimal(max(h, t) - min(h, t) + 1)
#print(diff)
i = max(1, i)
#print('00001 ', i)
while True:
temp = Decimal(h*i + c*(i-1)) / Decimal(i+i-1)
if Decimal(max(temp, t) - min(temp, t)) < diff:
#print('i ', i)
i += 1
diff = Decimal(max(temp, t) - min(temp, t))
#print(diff)
else:
#p = (h + c)/2
#if max((h + c)/2, t) - min((h + c)/2,t) <= diff:
# cout('2\n')
#else:
cout(str(i+i-1-2) + '\n')
break
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | 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)) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.StringTokenizer;
public class MixingWater{
public static void main(String[] args)throws IOException{
FastReader sc = new FastReader();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out),"ASCII"),512);
int test = sc.nextInt();
while(test-- > 0){
long h = sc.nextInt();
long c = sc.nextInt();
long tg = sc.nextInt();
if (tg == h) {
out.write(1 + "");
} else if (2 * tg <= c + h) {
out.write(2 + "");
} else {
double m = (h + c) / 2d;
double tm = tg - m;
int counter = (int) Math.ceil((h - c) / (2d * tm));
counter = (counter % 2 != 0) ? counter : counter + 1;
double toUpper = Math.abs((h - c) / ((double) (2 * (counter - 2))) - tm);
double toLower = Math.abs((h - c) / ((double) (2 * counter)) - tm);
if (toUpper <= toLower) {
out.write((counter - 2) + "");
} else {
out.write(counter + "");
}
}
out.write('\n');
out.flush();
}
}
}
class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
}
| JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using namespace std;
int mod = 1000000007;
int fact(int n) {
int res = 1;
for (int i = 2; i <= n; i++) {
res = (res * 1ll * i) % mod;
}
return res;
}
int binpow(int x, int y) {
int res = 1;
x = x % mod;
if (x == 0) return 0;
while (y) {
if (y % 2 == 1) res = (1ll * res * x) % mod;
y = y >> 1;
x = (1ll * x * x) % mod;
}
return res;
}
int C(int n, int k) {
if (n < k) return 0;
int ans = (1ll * fact(n) * binpow(fact(k), mod - 2)) % mod;
ans = (ans * 1ll * binpow(fact(n - k), mod - 2)) % mod;
return ans;
}
void solve() {
ll h, c, t;
cin >> h >> c >> t;
ll l = h - t, r = t - c;
if (l >= r)
cout << 2 << endl;
else {
ll k = (r - 1) / (r - l);
double a =
abs((double)t - (double)((k + 1) * h + k * c) / (double)(2 * k + 1));
double b = a + 1;
if (k > 0)
b = abs((double)t - (double)(k * h + (k - 1) * c) / (double)(2 * k - 1));
if (a >= b)
cout << (2 * k - 1) << endl;
else
cout << (2 * k + 1) << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
final Scanner in = new Scanner(System.in);
final int T = in.nextInt();
for (int t = 0; t < T; t += 1) {
final long h = in.nextLong();
final long c = in.nextLong();
final long target = in.nextLong();
if (target >= h) {
System.out.println(1);
continue;
}
if (target <= (double)(h + c) / 2) {
System.out.println(2);
continue;
}
double k = (double)(h - target) / (2 * target - h - c);
long floor = (long)Math.floor(k);
long ceil = (long)Math.ceil(k);
long tFloor = ((floor + 1) * h + floor * c);
long tCeil = ((ceil + 1) * h + ceil * c);
// System.out.printf("h: %d, c: %d, target: %d\nk: %f, tFloor: %f, tCeil: %f\n", h, c, target, k, (double)tFloor / (2 * floor + 1), (double)tCeil / (2 * ceil + 1));
if (tFloor * (2 * ceil + 1) + tCeil * (2 * floor + 1) <= 2 * target * (2 * floor + 1) * (2 * ceil + 1)) {
System.out.println(2 * floor + 1);
} else {
System.out.println(2 * ceil + 1);
}
}
}
}
// tFloor / (2 * floor + 1) - target < target - tCeil / (2 * ceil + 1)
// tFloor / (2 * floor + 1) < 2*target - tCeil / (2 * ceil + 1)
// tFloor * (2 * ceil + 1) + tCeil * (2 * floor + 1) < 2*target * (2 * floor + 1) * (2 * ceil + 1)
// 7 - 6 < 6 - 15
| JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #debugging c1.py
#conceptual error hai mera - cant compare floats directly
#see
# >>> loss(499979, hot, cold, desired_temp)
# 2.0000734366476536e-06
# >>> loss(499981, hot, cold, desired_temp)
# 2.0000734366476536e-06
# asli mei the numbers may be different
import sys
def input():
return sys.stdin.readline().rstrip()
def gcd(a,b):
a,b = min(a,b), max(a,b)
if a == 0:
return b
else:
return gcd(b%a, a)
class pos_rational():
def __init__(self, a, b):
self.numerator = a
self.denominator = b
def __eq__(self, other):
val = self.numerator*other.denominator - other.numerator*(self.denominator)
return (val == 0)
# return ()
def __lt__(self, other):
val = self.numerator*other.denominator - other.numerator*(self.denominator)
return val < 0
def __gt__(self, other):
val = self.numerator*other.denominator - other.numerator*(self.denominator)
return val > 0
def __ge__(self, other):
return (self == other or self > other)
def __le__(self, other):
return (self == other or self < other)
# class pos_rational():
# def __init__(self,a,b):
# if a == 0:
# self.num = 0
# self.denom = 0
# else:
# my_gcd = gcd(a,b)
# self.num = a//my_gcd
# self.denom = b//my_gcd
# # self.num = a
# # self.denom = b
# def __eq__(self, other):
# return (self.num == other.num and self.denom == other.denom)
# def __gt__(self, other):
# if self == other:
# return False
# if self.denom == 0:
# return False
# elif other.denom == 0:
# return True
# else:
# return (self.num*other.denom - self.denom*other.num > 0)
# def __ge__(self, other):
# return (self == other or self > other)
# def __le__(self, other):
# return (other > self)
def loss(two_n_plus_one, hot, cold, desired):
n = two_n_plus_one//2
# if n == 0:
# return float('inf')
# else:
# return abs(desired - ((n+1)*hot + n*cold)/(2*n + 1))
numerator = int(abs(desired*(2*n + 1) -((n+1)*hot + n*cold)))
denominator = (2*n + 1)
return pos_rational(numerator, denominator)
testcases = int(input())
answers = []
for _ in range(testcases):
hot, cold, desired_temp = [int(i) for i in input().split()]
#required number of cups to get it to desired_temp
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:
#find n, -> num iters
#n + 1 hots, n colds
frac = (hot - desired_temp) / (desired_temp - mid_way)
frac /= 2
# option1 = int(frac)
# if frac%1 == 0:
# #khatam
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) >= pos_rational(hot - desired_temp, 1) and (hot -desired_temp) <= (desired_temp - mid_way):
answers.append(1)
elif min(l1, l2) >= pos_rational(desired_temp - mid_way, 1):
answers.append(2)
elif l1 <= l2:
answers.append(option1)
else:
answers.append(option2)
# if option1%2 == 0:
# option1 += 1 #taki odd ho jaye
# option2 = option1 - 2
# option3 = option1 + 2
# l1, l2, l3 = loss(option1, hot, cold, desired_temp),loss(option2, hot, cold, desired_temp),loss(option3, hot, cold, desired_temp)
# option4 = option1 - 1
# answers.append(min(loss(option1, hot, cold, desired_temp),
# loss(option2, hot, cold, desired_temp),
# loss(option3, hot, cold, desired_temp)))
print(*answers, sep = '\n') | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 |
import java.util.*;
public class codfor {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int T = s.nextInt();
while (T-- > 0) {
int h = s.nextInt(), c = s.nextInt(), t = s.nextInt();
if (2*t <= h+c) { System.out.println(2); continue; }
int k = (int)Math.floor((double)(h-t)/(2*t-h-c));
if (Math.abs((k*(h+c)+h)-t*(2*k+1))*(2*k+3)<=Math.abs(((k+1)*(h+c)+h)-t*(2*k+3))*(2*k+1)) {
System.out.println(2*k+1);
}
else {
System.out.println(2*(k+1)+1);
}
}
}
}
| JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long int tc, h, c, t;
cin >> tc;
while (tc--) {
cin >> h >> c >> t;
if (h + c >= 2 * t)
cout << 2 << "\n";
else {
long long int k = (t - h) / (h + c - 2 * t);
if (fabs(1.0 * ((h + c - 2 * t) * k + h - t) / (2 * k + 1)) >
fabs(1.0 * ((h + c - 2 * t) * (k + 1) + h - t) / (2 * k + 3)))
++k;
cout << 2 * k + 1 << "\n";
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | from sys import stdin, stdout
from decimal import Decimal
cin = stdin.readline
cout = stdout.write
#getcontext().prec = 50
for _ in range(int(cin())):
h, c, t = map(int, cin().split())
if (h+c)/2 >= t:
cout('2\n')
continue
i = (c-t) // (h+c-t-t)
if abs(t - Decimal(h*i + c*(i-1))/Decimal(i+i-1)) <= abs(t - Decimal(h*(i+1) + c*i)/Decimal(i+i+1)):
cout(str(i+i-1) + '\n')
#print(abs(t - Decimal((h*i + c*(i-1))/(i+i-1))), abs(t - Decimal((h*(i+1) + c*i)/(i+i+1))))
else:
cout(str(i+i+1) + '\n')
#credit - arif vai
'''
if max((h+c)/2, t) - min((h+c)/2, t) <= 1e-6:
cout('2\n')
continue
i = (c-t) // (h+c-t-t)
if i <= 0:
cout('2\n')
continue
diff = Decimal(max(h, t) - min(h, t) + 1)
#print(diff)
i = max(1, i)
#print('00001 ', i)
while True:
temp = Decimal(h*i + c*(i-1)) / Decimal(i+i-1)
if Decimal(max(temp, t) - min(temp, t)) < diff:
#print('i ', i)
i += 1
diff = Decimal(max(temp, t) - min(temp, t))
print('d ', diff)
else:
#p = (h + c)/2
#if max((h + c)/2, t) - min((h + c)/2,t) <= diff:
# cout('2\n')
#else:
cout(str(i+i-1-2) + '\n')
break
'''
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | T=int(input())
while T>0:
T-=1
h,c,t=map(int,input().split())
if 2*t<=h+c:
print(2)
continue
k=(h-t)//(2*t-h-c)
if abs((2*k+3)*t-k*h-2*h-k*c-c)*(2*k+1)<abs((2*k+1)*t-k*h-h-k*c)*(2*k+3): print(2*k+3)
else: print(2*k+1) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | from decimal import *
getcontext().prec = 30
T = int(input())
o = []
for i in range(T):
h, c, t = input().split()
h = int(h)
c = int(c)
t = int(t)
s = (h + c) / 2
if t >= h:
o.append(1)
elif t <= s:
o.append(2)
else:
i = (h - t) / (2 * t - (h + c))
#print(i)
if i == int(i):
i = int(i)
o.append(2 * i + 1)
else:
i = Decimal(int(i))
h = Decimal(h)
c = Decimal(c)
t = Decimal(t)
prev = ((i+1) * h + i * c) / (2 * i + 1)
i += 1
cur = ((i+1) * h + i * c) / (2 * i + 1)
#print(prev, cur, t)
#print(abs(cur - t), abs(prev - t))
if abs(cur - t) >= abs(prev - t):
o.append((i - 1) * 2 + 1)
else:
o.append(i * 2 + 1)
for i in range(T):
print(o[ i ])
#'499981', found: '499979'
# 999977 17 499998
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | // Working program with FastReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class C_Mixing_Water{
public static void main(String[] args) {
FastReader sc=new FastReader();
PrintWriter out = new PrintWriter(System.out);
int test = sc.nextInt();
for(int t=0;t<test;t++){
double a = sc.nextInt();
double b = sc.nextInt();
double c = sc.nextInt();
if(c>=a)out.println(1);
else{
// double diff = Math.abs(a-c);
// double currentDiff = Double.MAX_VALUE;
// int n = 1;
// int m = 0;
// boolean hot = false;
// while(true){
// if(hot){
// n++;
// hot = false;
// double temp = ((n*a)+((m*b)))/(n+m);
// currentDiff = Math.abs(temp-c);
// if(currentDiff>=diff){
// n--;
// break;
// }
// else {
// diff = currentDiff;
// }
// }
// else{
// m++;
// hot = true;
// double temp = ((n*a)+((m*b)))/(n+m);
// currentDiff = Math.abs(temp-c);
// if(currentDiff>=diff){
// m--;
// break;
// }
// else {
// diff = currentDiff;
// }
// }
// }
// out.println(n+m);
double diff = Math.abs(a-c);
int ans = 1;
double avg = (double)(a+b)/(double)2;
if(diff>Math.abs(avg-c)){
diff = Math.abs(avg-c);
ans = 2;
}
// n n-1
double num = b-c;
double dem = a+b-(2*c);
double div = num/dem;
int n = (int)Math.round(div);
double temp = (double)((n*a)+((n-1)*b))/(double)(2*n-1);
double temp1 = Math.abs(temp-c);
if(diff>temp1){
diff = temp1;
ans = n+n-1;
}
n++;
temp = (double)((n*a)+((n-1)*b))/(double)(2*n-1);
temp1 = Math.abs(temp-c);
if(diff>temp1){
diff = temp1;
ans = n+n-1;
}
if(ans<0){
ans = 2;
}
out.println(ans);
}
}
out.close();
}
public static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 |
import java.util.*;
import java.io.*;
public class Main {
static FastReader in=new FastReader();
static StringBuilder Sd=new StringBuilder();
static List<Integer>Gr[];
static long Mod=998244353;
static Map<Integer,Integer>map=new HashMap<>();
public static void main(String [] args) {
//Dir by MohammedElkady
int t=in.nextInt();
while(t-->0) {
long h=in.nextLong(),c=in.nextLong(),tt=in.nextLong();
if(h==tt) {Soutln("1");}
else if((h+c)>=tt*2) {Soutln("2 ");}
else {
long ans=1L,diff=Long.MAX_VALUE-1;
diff=h-tt;
if(diff*3<=Math.abs(tt*3-(h+h+c))) {
Soutln("1");
}
else {
long x=h-tt,xx=0;
x/=(2*tt-h-c);
xx=x+1;
long lop=Math.abs((tt*(x*2+1)-x*(h+c)-h)*(xx*2+1)),cop=Math.abs((tt*(xx*2+1)-xx*(h+c)-h)*(x*2+1));
if(lop>cop) {Soutln(2*xx+1+" ");}
else Soutln(2*x+1+" ");
}
}
}
Sclose();
}
static String Make_best(StringBuilder Sl) {
StringBuilder Sx=Sl,Sy=Sl,Sw=Sl;
while(!Sx.toString().equals(new StringBuilder(Sw.toString()).reverse().toString())) {
Sx.replace(0, 1, "");
Sx=Sw;
System.out.println(Sx.toString());
}
while(!Sy.toString().equals(new StringBuilder(Sy.toString()).reverse().toString())) {
Sy.replace(Sy.length()-2, Sy.length()-1, "");
}
if(Sx.length()>Sy.length())Sl=Sx;
else Sl=Sy;
return Sl.toString();
}
static long power(long x, long y, long p)
{
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0)
{
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p)
{
return power(n, p-2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(int n, int r,
long p)
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long[] fac = new long[n+1];
fac[0] = 1;
for (int i = 1 ;i <= n; i++)
fac[i] = fac[i-1] * i % p;
return (fac[n]* modInverse(fac[r], p)
% p * modInverse(fac[n-r], p)
% p) % p;
}
static long fac(int n , int m,int l) {
long res=1;
for(int i=l,u=1;i<=n||u<=m;i++,u++) {
if(i<=n) {res*=i;}
if(u<=m) {res/=u;}
while(res>Mod)
res-=Mod;
}
return res;
}
static long posation(int n) {
long res=1;
for(int i=0;i<n-3;i++) {res*=2L;
while(res>Mod)
res-=Mod;
while(res<=0)res+=Mod;}
return res;
}
static long gcd(long g,long x){
if(x<1)return g;
else return gcd(x,g%x);
}
//array fill
static long[]filllong(int n){long a[]=new long[n];for(int i=0;i<n;i++)a[i]=in.nextLong();return a;}
static int[]fillint(int n){int a[]=new int[n];for(int i=0;i<n;i++)a[i]=in.nextInt();return a;}
//OutPut Line
static void Sout(String S) {Sd.append(S);}
static void Soutln(String S) {Sd.append(S+"\n");}
static void Soutf(String S) {Sd.insert(0, S);}
static void Sclose() {System.out.println(Sd);}
static void Sclean() {Sd=new StringBuilder();}
}
class node implements Comparable<node>{
int x,t;
node(int x,int p){
this.x=x;
this.t=p;
}
@Override
public int compareTo(node o) {
return (t-o.t);
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
class Sorting{
public static node[] bucketSort(node[] array, int bucketCount) {
if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count");
if (array.length <= 1) return array; //trivially sorted
int high = array[0].t;
int low = array[0].t;
for (int i = 1; i < array.length; i++) { //find the range of input elements
if (array[i].t > high) high = array[i].t;
if (array[i].t < low) low = array[i].t;
}
double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket
ArrayList<node> buckets[] = new ArrayList[bucketCount];
for (int i = 0; i < bucketCount; i++) { //initialize buckets
buckets[i] = new ArrayList();
}
for (int i = 0; i < array.length; i++) { //partition the input array
buckets[(int)((array[i].t - low)/interval)].add(array[i]);
}
int pointer = 0;
for (int i = 0; i < buckets.length; i++) {
Collections.sort(buckets[i]); //mergeSort
for (int j = 0; j < buckets[i].size(); j++) { //merge the buckets
array[pointer] = buckets[i].get(j);
pointer++;
}
}
return array;
}
} | JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long double l, h, t;
long double fu(long long a) {
long double x1 = a / 2 + a % 2;
long double y1 = a / 2;
long double ans1 = (x1 * h + y1 * l) / (long double)a;
return ans1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin >> T;
while (T--) {
cin >> h >> l >> t;
long double temp = (l + h) / 2.0;
temp = 2 * (t - temp);
long long count = (h - l) / temp;
long double ans = 2, min1 = abs(t - (l + h) / 2);
for (int i = count - 15; i <= count + 15; i++) {
if (i > 0) {
long double p = fu(i);
if (abs(t - p) < min1) {
min1 = abs(t - p);
ans = i;
}
}
}
cout << ans << endl;
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | t = int(input())
for i in range(0, t):
s = input().split()
h = int(s[0])
c = int(s[1])
t = int(s[2])
if h+c == t*2:
print(2)
else:
x = (t-h)/(h+c-t*2)
l = int(x)
r = l + 1
#print(2*l+1, "ansL")
#print(2*r+1, "ansR")
al = abs( ( (h*(l+1) + c*l) ) - t*(2*l+1))*(2*r+1)#*(2*l+1))#*(2*r+1)
ar = abs( ( (h*(r+1) + c*r) ) - t*(2*r+1))*(2*l+1)#*(2*r+1))#*(2*l+1)
am = abs((h + c) / 2 - t)*(2*l+1)*(2*r+1)#*(2*l+1)*(2*r+1)
#print(ans(l, h, c), "tempL")
#print(ans(r, h, c), "tempR")
#print(al, "`al")
#print(ar, "`ar")
#print(am, "am")
#print(abs((h + c) / 2-t), "hl")
if min(al, ar, am) == al and x >= 0:
print(2 * l + 1)
elif min(al, ar, am) == ar and x >= 0:
print(2 * r + 1)
else:
print(2)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void test_case() {
double h, c, t;
cin >> h >> c >> t;
int cnt = 1;
double dif = abs(h - t);
if (abs((h + c) / 2 - t) < dif) {
cnt = 2;
dif = abs((h + c) / 2 - t);
}
if (2 * t != h + c) {
double eq = fabs((double)(c - t) / (h + c - (2 * t)));
double x1 = ceil(eq);
double x2 = floor(eq);
if (x2 > 1) {
double calc = abs((h * x2 + (x2 - 1) * c) / (2 * x2 - 1) - t);
if (calc < dif) {
cnt = 2 * x2 - 1;
dif = calc;
}
}
if (x1 > 1) {
double calc = abs((h * x1 + (x1 - 1) * c) / (2 * x1 - 1) - t);
if (calc < dif) {
cnt = 2 * x1 - 1;
dif = calc;
}
}
}
cout << cnt << "\n";
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
test_case();
}
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | t = int(input())
ans=[0]*t
for i in range(t):
h,c,obj=map(int, input().split())
ave=(h+c)/2
if obj<=ave:
ans[i]=2
else:
x=obj-ave
y=(h-c)/2
z=y//x
if z%2==0:
z-=1
#print(x,y,z,ave)
temp0=abs(obj-ave-y/z)
temp1=abs(obj-ave-y/(2+z))
#print(temp0,temp1,y/z)
if temp0<=temp1:
ans[i]=z
else:
ans[i]=z+2
for i in range(t):
print(int(ans[i])) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | for _ in range(int(input())):
h, c, t = map(int, input().split())
if h + c >= t * 2:
print(2)
elif h <= t:
print(1)
else:
k = (h - t) // (t * 2 - h - c)
x = k * k * 2 + k * 5
y = k * k * 2 + k * 3
if (x + 3 + x + 2) * h + (y + y + 1) * c > 2 * (4 * k * k + 8 * k + 3) * t:
k += 1
print(k * 2 + 1)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #import math
#from functools import lru_cache
#import heapq
#from collections import defaultdict
#from collections import Counter
#from collections import deque
#from sys import stdout
#from sys import setrecursionlimit
#setrecursionlimit(10**7)
from sys import stdin
input = stdin.readline
INF = 10**9 + 7
MAX = 10**7 + 7
MOD = 10**9 + 7
for Ti in range(int(input().strip())):
h,c,t = [int(x) for x in input().strip().split()]
avg = (h+c)/2
if(t<=avg):
print('2')
continue
ti = (t - h)//(h + c - 2*t)
l = (2*ti + 3)*((ti*(h+c) + h) - 2*ti*t - t)
r = (2*ti + 1)*(((ti+1)*(h+c) + h) - 2*ti*t - 3*t)
#print(ti, l, r)
if(abs(l)<=abs(r)):
print(2*ti + 1)
else:
print(2*ti + 3)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long h, c;
const double eps = 1e-8;
double getT(long long k) {
double x = 1.0;
return x * ((h + c) * k - c) / (2 * k - 1);
}
int main() {
int T;
long long t;
scanf("%d", &T);
while (T--) {
scanf("%lld%lld%lld", &h, &c, &t);
if (t == h) {
puts("1");
continue;
}
if (h + c >= 2 * t) {
puts("2");
continue;
}
long long l = 1, r = 1e18;
long long ans1, ans2, ans;
while (l <= r) {
long long mid = (l + r) / 2;
if (getT(mid) >= t) {
ans1 = mid;
l = mid + 1;
} else
r = mid - 1;
}
l = 1, r = 1e18;
while (l <= r) {
long long mid = (l + r) / 2;
if (getT(mid) <= t) {
ans2 = mid;
r = mid - 1;
} else
l = mid + 1;
}
if (fabs(getT(ans1) - t) <= fabs(getT(ans2) - t))
ans = ans1;
else
ans = ans2;
printf("%lld\n", ans * 2 - 1);
}
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.util.*;
import java.io.*;
import java.math.*;
public class EdC {
public static void main(String[] args) throws Exception{
int num = 998244353;
// TODO Auto-generated method stub
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(bf.readLine());
for(int i = 0;i<t;i++){
// String input1 = bf.readLine().trim();
// String input2 = bf.readLine().trim();
StringTokenizer st = new StringTokenizer(bf.readLine());
double h=Double.parseDouble(st.nextToken());
double c=Double.parseDouble(st.nextToken());
double temp=Double.parseDouble(st.nextToken());
if (h-temp >= temp-c){
out.println(2);
}
else if (h-temp < temp-c){
double ans=0;
double avg=(h+c+0.0)/2.0;
double asdf=Math.floor( (h-c)/(2.0*(temp-avg)) );
if(asdf%2==0)
ans=asdf+1.0;
else{
double middle=(asdf*(asdf+2.0))/(asdf+1.0);
if(( (h-c)/(2.0*(temp-avg)) )>middle)
ans=asdf+2;
else
ans=asdf;
}
out.println((int)(ans));
}
}
out.close();
}
}
//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is | JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long mod = 1000000000 + 7;
int main() {
long long T;
cin >> T;
start:
while (T--) {
double a, b, c;
cin >> b >> a >> c;
if (b == a) {
cout << 1 << endl;
goto start;
}
if (a + b == 2 * c) {
cout << 2 << endl;
goto start;
}
long long ans = 1;
double diff = abs(c - b);
double tdiff = abs((a + b) / 2.0 - c);
if (tdiff < diff) {
ans = 2;
diff = tdiff;
}
if (c - a < b - c) {
cout << ans << endl;
goto start;
}
if (c - a > b - c) {
long long d = (b - c) / (2 * c - a - b);
for (long long i = max((long long)1, d - 4); i <= d + 4; i++) {
double temp = (a * i + b * (i + 1)) / (1.0 * (2 * i + 1));
if (abs(temp - c) < diff) {
ans = 2 * i + 1;
diff = abs(temp - c);
}
}
cout << ans << endl;
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | for _ in range(int(input())):
(a,b,c) = map(int,input().split())
if a==c:
print(1)
continue
if(a+b)/2 == c:
print(2)
continue
temp = -1
l = 2
r = c+1
x = 2
while(l<r):
mid = (l+r)//2
# print(l,r,mid,(mid*a+(mid-1)*b)/(2*mid-1))
if (mid*a+(mid-1)*b)/(2*mid-1) <= c:
x = mid
r = mid
else:
l = mid+1
y = x-1
ans2 = (a+b)/2
ans1 = (a*x-a+b*y-b)*(x+y)
ans = (a*x+b*y)*(x+y-2)
pk = (x+y)*(x+y-2)
if 2*c*pk>=ans+ans1:
if abs(ans2-c)<=abs(c-(a*x-a+b*y-b)/(x+y-2)):
print(2)
else:
print(x+y-2)
else:
if abs(ans2-c)<=abs(c-(a*x+b*y)/(x+y)):
print(2)
else:
print(x+y)
| PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* https://codeforces.com/problemset/problem/1359/C
* 3
* 30 10 20
* 41 15 30
* 18 13 18 should output 2, 7, and 1, each on a newline
*/
public final class WaterMix {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int testNum = Integer.parseInt(read.readLine());
for (int t = 0; t < testNum; t++) {
StringTokenizer info = new StringTokenizer(read.readLine());
int hot = Integer.parseInt(info.nextToken());
int cold = Integer.parseInt(info.nextToken());
int target = Integer.parseInt(info.nextToken());
if (cold > hot) {
throw new IllegalArgumentException("invalid water temperatures lol");
}
// this is actually a fraction lmao
long[] minError = new long[] {Math.abs(hot - target), 1};
int cupNum = 1;
double avg = (double) (hot + cold) / 2;
int twoCupDiff = Math.abs(hot + cold - 2 * target);
if (fracLessThan(twoCupDiff, 2, minError[0], minError[1])) {
cupNum = 2;
minError = new long[] {twoCupDiff, 2};
}
if (avg < target) {
int theoretical = (cold - hot) / (hot + cold - 2 * target);
int loOdd = (theoretical - 1) / 2 * 2 + 1;
long loIntTarget = (long) target * loOdd;
long loTotalTemp = (long) (loOdd - 1) * (hot + cold) / 2 + hot;
if (fracLessThan(Math.abs(loTotalTemp - loIntTarget), loOdd, minError[0], minError[1])) {
cupNum = loOdd;
minError = new long[] {loTotalTemp - loIntTarget, loOdd};
}
int hiOdd = loOdd + 2;
long hiIntTarget = (long) target * hiOdd;
long hiTotalTemp = (long) (hiOdd - 1) * (hot + cold) / 2 + hot;
if (fracLessThan(Math.abs(hiTotalTemp - hiIntTarget), hiOdd, minError[0], minError[1])) {
cupNum = hiOdd;
}
}
System.out.println(cupNum);
}
}
private static boolean fracLessThan(long numA, long denomA, long numB, long denomB) {
// System.out.println(numA + " " + denomA + " " + numB + " " + denomB);
return numA * denomB < numB * denomA;
}
}
| JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | tests=int(input())
for _ in range(tests):
h,c,t=map(int,input().split());
if(t==h):
print(1)
continue
if(t<=(h+c)/2):
print(2)
continue
target=t-(h+c)/2
dif=h-c
tmp=int(dif//(2*target));
if(tmp<=0):
tmp=1;
if(tmp%2==0):
tmp=tmp-1
val=500000000
ans=0
if(abs(target- dif/(2*tmp))<val):
val=abs(target- dif/(2*tmp));
ans=tmp
if(abs(target -dif/(2*(tmp+2)))<val):
val=abs(target -dif/(2*(tmp+2)))
ans=tmp+2
if(abs(target-dif/(2*(tmp+4)))<val):
ans=tmp+4
print(ans) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
using li = long int;
using lli = long long int;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int _;
cin >> _;
while (_--) {
double h, c, t;
cin >> h >> c >> t;
if (t <= (h + c) / 2) {
cout << 2 << "\n";
continue;
}
if (t >= h) {
cout << 1 << "\n";
continue;
}
lli l = 0, r = 1e15, mid, ans;
while (l <= r) {
mid = l + (r - l) / 2;
double temp = (mid * (h + c) + h) / (mid * 2 + 1);
if (temp < t) {
r = mid - 1;
} else {
ans = mid;
l = mid + 1;
}
}
auto temp = (ans * (h + c) + h) / (ans * 2 + 1);
auto temp1 = ((ans + 1) * (h + c) + h) / ((ans + 1) * 2 + 1);
if (abs(t - temp) <= abs(t - temp1)) {
cout << ans * 2 + 1 << "\n";
} else {
cout << ans * 2 + 3 << "\n";
}
}
}
| CPP |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.io.*;
import java.util.*;
public class C{
public static Reader sc = new Reader();
//public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static long t,h,c;
public static void main(String[] args) throws IOException {
//BufferedReader br = new BufferedReader(new FileReader("input.in"));
int q = sc.nextInt();
while (q-- > 0) {
h = sc.nextInt();
c = sc.nextInt();
t = sc.nextInt();
if (t== h) {
out.println(1);
}
else if (2*t <= h+c) {
out.println(2);
}
else {
double up = h-t;
double down = 2*t-h-c;
long k = (int)(up/down);
long v1 = (2*k+3)*Math.abs(t*(2*k+1)-(k*(h+c)+h));
long v2 = (2*k+1)*Math.abs(t*(2*k+3)-((k+1)*(h+c)+h));
if (v1 <= v2) {
out.println(2*k+1);
}else out.println(2*k+3);
}
}
out.close();
}
static long ceil(long a, long b) {
return (a + b - 1) / b;
}
static long powMod(long base, long exp, long mod) {
if (base == 0 || base == 1)
return base;
if (exp == 0)
return 1;
if (exp == 1)
return base % mod;
long R = powMod(base, exp / 2, mod) % mod;
R *= R;
R %= mod;
if ((exp & 1) == 1) {
return base * R % mod;
} else
return R % mod;
}
static long pow(long base, long exp) {
if (base == 0 || base == 1)
return base;
if (exp == 0)
return 1;
if (exp == 1)
return base;
long R = pow(base, exp / 2);
if ((exp & 1) == 1) {
return R * R * base;
} else
return R * R;
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, Integer.max(cnt - 1, 0));
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
}
| JAVA |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | # ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io --------------------
import decimal
import math
testcases=int(input())
for j in range(testcases):
h,c,t=map(int,input().split())
#we see that this converges to (h+c)/2, if after 2 more cup pours and the abs difference increases then we stop
val1=h+c+t
if h+c-2*t==0:
val1=2
val2=h+c+t
if h+c-2*t!=0:
val2=(t-h)/(h+c-2*t)
n1=(t-h)//(h+c-2*t)
n2=math.ceil((t-h)/(h+c-2*t))
if abs(((n1+1)*h+n1*c)*(2*n2+1)-(2*n1+1)*(2*n2+1)*t)<=abs(((n2+1)*h+(n2)*c)*(2*n1+1)-(2*n2+1)*(2*n1+1)*t):
val2=max(2*((t-h)//(h+c-2*t))+1,1)
else:
val2=max(2*math.ceil((t-h)/(h+c-2*t))+1,1)
val3=h+c+t
if t==h:
val3=1
if abs(t-h)<=abs(t-c):
print(min(val1,val2,val3))
else:
print(2) | PYTHON3 |
1359_C. Mixing Water | 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:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
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 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases.
Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ 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. | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CMixingWater solver = new CMixingWater();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class CMixingWater {
public void solve(int testNumber, FastReader s, PrintWriter w) {
int h = s.nextInt(), c = s.nextInt(), t = s.nextInt(), hf = (h + c) >> 1;
if (t <= hf) {
w.println(2);
return;
}
if (t >= h) {
w.println(1);
return;
}
long l = 1, r = (long) 1e6;
double min = Long.MAX_VALUE;
long ans = -1;
while (l <= r) {
long mid = l + r >> 1;
double val = Math.abs(mid * h + (mid - 1) * c - t * (2 * mid - 1));
val /= 2 * mid - 1;
if (t * (mid * 2 - 1) == mid * h + (mid - 1) * c) {
w.println(2 * mid - 1);
return;
}
if (val < min) {
ans = mid * 2 - 1;
min = val;
} else if (val == min)
ans = Math.min(ans, mid * 2 - 1);
if (t * (mid * 2 - 1) < mid * h + (mid - 1) * c) l = mid + 1;
else r = mid - 1;
}
w.println(ans);
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| JAVA |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.