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 input = sys.stdin.readline def solve(): h, c, t = map( int, input().split()) if h <= t: return 1 if 2*t <= h+c: return 2 if 2*h+c <= t*3: if abs(2*h+c-t*3) >= (h-t)*3: return 1 else: return 3 l = 0 r = 500000 while r-l > 1: m = (l+r)//2 if t*(2*m+1) < ((m+1)*h+m*c): l = m else: r = m # print(l,r) if abs(((l+1)*h+l*c)-t*(2*l+1))*(2*r+1) <= abs(((r+1)*h+r*c)-t*(2*r+1))*(2*l+1): return 2*l+1 else: return 2*r+1 def main(): T = int( input()) for _ in range(T): print(solve()) if __name__ == '__main__': main() #1000000 0 500001
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
cases = int(input()) for i in range(cases): l1 = input().split(' ') h = int(l1[0]) c = int(l1[1]) t = int(l1[2]) num = 0 #even number of hot and cold pos1 = (c+h)/2 if t <= pos1: ans = 2 print(ans) else: import math check = math.floor((h-t)/(2*t-h-c)) if abs((check*(h+c)+h)-t*(2*check+1))*(2*check+3)<=abs((check+1)*(h+c)+h - t*(2*check+3))*(2*check+1): ans = 2*check+1 else: ans = 2*check+3 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 input = sys.stdin.readline from collections import * def judge(x): return h*(2*x+1)-x*(h-c)>=(2*x+1)*t def binary_search(): l, r = 0, 10**18 while l<=r: m = (l+r)//2 if judge(m): l = m+1 else: r = m-1 return r T = int(input()) for _ in range(T): h, c, t = map(int, input().split()) if 2*t<=h+c: print(2) continue if t==h: print(1) continue b = binary_search() #up = h-b*(h-c)/(2*b+1) #low = h-(b+1)*(h-c)/(2*(b+1)+1) if 2*(2*b+1)*(2*b+3)*t>=2*(2*b+1)*(2*b+3)*h-b*(2*b+3)*(h-c)-(b+1)*(2*b+1)*(h-c): print(1+2*b) else: print(1+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
import java.util.Scanner; public class Prob3 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int T = scan.nextInt(); for (int i = 0; i < T; i++) { int h = scan.nextInt(); int c = scan.nextInt(); int t = scan.nextInt(); System.out.println(solve(h, c, t)); } } private static int solve(double h, double c, double t) { if (t == h) return 1; if (t <= (h + c) / 2) return 2; int n = (int) ((t - h) / (c + h - 2 * t)); if (Math.abs(n * (h + c) + h - (2 * n + 1) * t) * (2 * n + 3) <= Math.abs((n + 1) * (h + c) + h - (2 * n + 3) * t) * (2 * n + 1)) { return 2 * n + 1; } else { return 2 * n + 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
""" Python 3 compatibility tools. """ from __future__ import division, print_function import itertools import sys import os from io import BytesIO, IOBase if sys.version_info[0] < 3: input = raw_input range = xrange filter = itertools.ifilter map = itertools.imap zip = itertools.izip def is_it_local(): script_dir = str(os.getcwd()).split('/') username = "dipta007" return username in script_dir def READ(fileName): if is_it_local(): sys.stdin = open(f'./{fileName}', 'r') # region fastio 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") if not is_it_local(): sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion def input1(type=int): return type(input()) def input2(type=int): [a, b] = list(map(type, input().split())) return a, b def input3(type=int): [a, b, c] = list(map(type, input().split())) return a, b, c def input_array(type=int): return list(map(type, input().split())) def input_string(): s = input() return list(s) ############################################################## import math, fractions F = fractions.Fraction def main(): t = input1() for ci in range(t): h, c, t = input3() avg = F((h + c), 2) # # for i in range(40): # odd = i * 2 + 1 # m = (i * (h+c) + h) / odd # print(odd, m) # Just 2 ta nibo res = 2 mx = abs(t - avg) low = 0 high = 40000000 now = 0 while low <= high: mid = (low + high) // 2 odd = mid * 2 + 1 m = (mid * (h+c) + h) / odd if m <= t: high = mid - 1 now = mid else: low = mid + 1 # print(now) for i in range(max(0, now-10), now + 10): odd = i * 2 + 1 m = F((i * (h + c) + h), odd) if abs(m - t) < mx: mx = abs(m-t) res = odd elif abs(m - t) == mx: res = min(res, odd) print(res) pass if __name__ == '__main__': # READ('in.txt') 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
from fractions import Fraction as frac import sys, os input = sys.stdin.buffer.read().split(b'\n')[::-1].pop from heapq import heappush, heappop def i(): return input() def ii(): return int(input()) def iis(): return map(int, input().split()) def liis(): return list(map(int, input().split())) def pint(x): os.write(1, str(x).ecode('ascii')), os.write(1, b'\n') def print_array(a): os.write(1, b' '.join(str(x).encode('ascii') for x in a)), os.write(1, b'\n') Test = int(input()) while True: Test -= 1 if Test < 0: break [h, c, t] = [int(x) for x in input().split()] x = h+c if h+c >= 2*t: print(2) continue def eval(k): return abs(frac(k*x + h, 2*k+1) - t); k = (h-t)//(2*t - x) k = max(k-10, 0) ans = k for i in range(0, 21): if eval(k+i) < eval(ans): ans = k+i print(2*ans + 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.util.*; public class Main { public static void main(String ars[]) { Scanner sc=new Scanner(System.in); int test=sc.nextInt(); for(int xix=0;xix<test;xix++) { double h=sc.nextInt()+0.0; double c=sc.nextInt()+0.0; double t=sc.nextInt()+0.0; if(t<=(h+c)/2) {System.out.println(2);continue;} if(h==t) {System.out.println(1);continue;} double n=1/((2*(t-(h+c)/2))/(h-c)); int b=(int)(Math.floor(n)); if(b%2==0) {b++;} int a=b-2; int d=b+2; if(Math.abs(t-(h+c)/2-(h-c)/(2*b))>Math.abs(t-(h+c)/2-(h-c)/(2*a))) {b=a;} if(Math.abs(t-(h+c)/2-(h-c)/(2*b))>Math.abs(t-(h+c)/2-(h-c)/(2*d))) {b=d;} System.out.println(b); } sc.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
#include <bits/stdc++.h> using namespace std; using ll = long long; double h, c, t; double f(double i) { return (c * (i - 1) + h * (i)) / (2 * i - 1); } void solve() { cin >> h >> c >> t; if (t >= h) { cout << 1 << endl; return; } if (t <= (h + c) / 2) { cout << 2 << endl; return; } ll low = 1; ll high = 1ll << 38; ll steps; while (low <= high) { ll mid = (low + high) / 2; double cavg = f(mid); if (cavg >= t) { steps = mid; low = mid + 1; } else high = mid - 1; } ll ans = 2; double avg = (h + c) / 2.0; for (int i = 0; i <= 100; ++i) { double cavg = f(steps + i); if (fabs(t - cavg) < fabs(t - avg)) { ans = steps + i; avg = cavg; } } cout << 2 * ans - 1 << endl; } int main() { 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
from decimal import Decimal def solve(): h,c,t=[int(x) for x in input().split()] if h==t: print(1) return a=2*t-h-c b=h-t if a<=0: print(2) return else: c1=b//a c2=c1+1 tc1=Decimal((h*(c1+1)+c*c1))/Decimal((2*c1+1)) tc2=Decimal((h*(c2+1)+c*(c2)))/Decimal((2*c2+1)) try1,try2=abs(tc1-t),abs(tc2-t) if try1<=try2: print(2*c1+1) else: print(2*c2+1) t=int(input()) for i in range(t): 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
T = int(input()) for t_itr in range(T): H, C, T = list(map(int, input().rstrip().split())) if T >= H: print(1) elif 2 * T <= H + C: print(2) else: n = (C - T) // (H + C - 2 * T) n1 = n + 1 d1 = H * n + C * (n - 1) - T * (2 * n - 1) d2 = H * n1 + C * (n1 - 1) - T * (2 * n1 - 1) if abs(d1 / (2 * n - 1)) <= abs(d2 / (2 * n1 - 1)): print(2 * n - 1) else: print(2 * n1 - 1)
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 Decimal import sys input=sys.stdin.readline print=sys.stdout.writelines t=int(input()) for i in range(t): h,c,t=map(int,input().split()) avg=(h+c)/2 if t<=avg: k=2 if t>avg: k=int((h-avg)//(t-avg)) if k%2==0: k+=1 ans=k mn=10**7 for j in range(-2,3,2): an=k+j if abs(Decimal(abs(an-1)*avg+h)/Decimal(abs(an))-t)<mn and an>0: mn=abs(Decimal(abs(an-1)*avg+h)/Decimal(abs(an))-t) ans=an #print(mn,an) k=int(ans) print(str(k)) print('\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.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import static java.lang.Math.min; public class C implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { int testCases = in.ni(); while (testCases-- > 0) { long h = in.nl(), c = in.nl(), t = in.nl(); long a = h + c, b = 2; long cups = 2; if (h == t) { a = 0; b = 1; cups = 1; } if (h + c != 2 * t) { double d = (h - t) / (2. * t - h - c); long k = (long) Math.floor(d); if (k >= 0) { long moves = 2 * k + 1; long p = (k + 1) * h * b + k * c * b - b * t * moves, q = moves * a; if (p < q) { cups = moves; a = p / b; b = moves; } else if (p == q) { cups = min(cups, moves); } } k = (long) Math.ceil(d); if (k >= 0) { long moves = 2 * k + 1; long p = b * t * moves - b * (k + 1) * h - b * k * c, q = moves * a; if (p < q) { cups = moves; } else if (p == q) { cups = min(cups, moves); } } } out.println(cups); } } @Override public void close() throws IOException { in.close(); out.close(); } 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 ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (C instance = new C()) { instance.solve(); } } }
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; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int h, c, t; cin >> h >> c >> t; if (t >= h) { cout << 1 << endl; } else if (2 * t <= (h + c)) { cout << 2 << endl; } else { double difference = t - (double)(h + c) / 2; double x = (h - c) / (2 * difference); int a = (int)floor(x + 1) | 1; int b = a - 2; double aDif = difference - (double)(h - c) / (2 * a); double bDif = difference - (double)(h - c) / (2 * b); if (abs(aDif) < abs(bDif)) { cout << a << endl; } else { cout << b << 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
import math #------------------------------warmup---------------------------- 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") #-------------------game starts now---------------------------------------------------- import math from fractions import Fraction for i in range(int(input())): a,b,cs=map(int,input().split()) if a==cs: print(1) continue elif (a+b)/2>=cs: print(2) continue t=math.ceil((cs-b)/(2*cs-a-b)) t=int(t) #print(t) cal=Fraction((t*(a+b)-b),(2*t-1)) cala=(a+b)/2 t-=1 if t>0: calp=Fraction((t*(a+b)-b),(2*t-1)) else: calp=-9999999999999999999999 ans=2 if abs(cala-cs)<=abs(cal-cs) and abs(cala-cs)<=abs(calp-cs): ans=2 elif abs(calp-cs)<=abs(cal-cs) and abs(calp-cs)<abs(cala-cs): ans=2*t-1 elif abs(cal-cs)<abs(cala-cs) and abs(cal-cs)<abs(calp-cs): ans=2*(t+1)-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
from decimal import * getcontext().prec=20 def v1(k): num = Decimal((k*h)+((k-1)*c)) den = Decimal((2*k)-1) return num/den def v(k): return ((k*h)+((k-1)*c))/((2*k)-1) import sys # sys.stdin=open("input.txt",'r') # sys.stdout=open("output.txt","w") inf = float('inf') def low(t): l,r = 2,10000000 ans= -1 while l<r: mid=(l+r)>>1 v1=v(mid) if v1>=t: val=mid l=mid+1 ans=mid else: r=mid return ans def up(t): l,r = 2,10000000 ans= -1 while l<r: mid=(l+r)>>1 v1=v(mid) if v1<=t: val=mid r=mid ans=mid else: l=mid+1 return ans for _ in range(int(input())): h,c,t=map(int,input().split()) if h<=t: print(1) elif (h+c)//2 >= t: print(2) else: idx1=low(t) idx2=up(t) idx=-1 ans=inf # print(idx1,idx2) # print(v(idx1),v(idx2)) # print(abs(t-v(idx1)),abs(t-v(idx2))) for i in range(idx1,idx2+1): val=abs(t-v1(i)) if val<ans: idx=i ans=val print(2*idx-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; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int i, j, t, n, k, l, d, r, p, m, x, y; cin >> t; while (t--) { cin >> n >> m >> k; if (k <= (n + m) / 2) { cout << 2 << "\n"; } else { double n1 = n, m1 = m, k1 = k; long long int ans = (n1 - k1) / ((2 * k1) - n1 - m1); long long int ans1 = 2 * ans + 1; long long int ans2 = ans1 + 2; if ((n1 - m1) * (ans1 + ans2) <= ((2 * k1) - n1 - m1) * (ans1)*ans2 * 2) { cout << ans1 << "\n"; } else { cout << ans2 << "\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 sys # from functools import lru_cache, cmp_to_key # from heapq import merge, heapify, heappop, heappush from math import sqrt, ceil, factorial, inf from collections import defaultdict as dd, deque, Counter as C # from itertools import combinations as comb, permutations as perm # from bisect import bisect_left as bl, bisect_right as br, bisect # from time import perf_counter from decimal import Decimal # from fractions import Fraction # sys.setrecursionlimit(pow(10, 6)) # sys.stdin = open("input.txt", "r") # sys.stdout = open("output.txt", "w") mod = pow(10, 9) + 7 mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end) def l(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] m = 10 ** 7 for _ in range(int(data())): h, c, t = sp() if h == t: out(1) continue if (h + c) / 2 >= t: out(2) continue low, high = 1, m r = False answer = 0 while low <= high: mid = (low + high) // 2 temp: float = (mid * h + (mid - 1) * c) / (2 * mid - 1) if temp == t: out(2 * mid - 1) r = True break if temp >= t: low = mid + 1 answer = mid continue high = mid - 1 if not r: arr = [answer-3, answer-2, answer-1, answer, answer+1, answer+2, answer+3] diff = inf answer = 2 * answer - 1 for i in arr: if i <= 0: continue temp: float = Decimal(i * h + (i - 1) * c) / Decimal(2 * i - 1) if temp > t: minus = Decimal(temp - t) else: minus = Decimal(t - temp) if minus < diff: answer = 2 * i - 1 diff = minus out(answer)
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--) { long long h, c, t, cnt, ans; cin >> h >> c >> t; if (h + c >= t * 2) { cout << 2 << '\n'; continue; } cnt = (t - c) / (t * 2 - h - c); int x = cnt, y = cnt + 1; double m = (x * h + (x - 1) * c) * 1.0 / (2 * x - 1) - t * 1.0; double n = (y * h + (y - 1) * c) * 1.0 / (2 * y - 1) - t * 1.0; if (abs(m) > abs(n)) ans = y; else ans = x; cout << ans * 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
from math import * def r1(t): return t(input().strip()) def r2(t): return [t(i) for i in input().strip().split()] def r3(t): return [t(i) for i in input().strip()] for _ in range(int(input())): h, c, t = r2(int) ans1 = abs((h + c)/2 - t) if ans1 == 0 or 2*t <= (h + c): print(2) elif h == t: print(1) else: a = h + c - 2*t b = t - c x1 = max(1, -b / a) x1 = int(x1) x2 = x1 + 1 ans1 = abs((x1*h + (x1 - 1)*c) - (2*x1 - 1)*t) ans2 = abs((x2*h + (x2 - 1)*c) - (2*x2 - 1)*t) if ans1*(2*x2 - 1) <= ans2*(2*x1 - 1): print(2*x1 - 1) else: print(2*x2 - 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,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools # import time,random,resource # sys.setrecursionlimit(10**6) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 mod2 = 998244353 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def pe(s): return print(str(s), file=sys.stderr) def JA(a, sep): return sep.join(map(str, a)) def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a) def IF(c, t, f): return t if c else f def YES(c): return IF(c, "YES", "NO") def Yes(c): return IF(c, "Yes", "No") def bs(f, mi, ma): mm = -1 while ma > mi: mm = (ma+mi) // 2 if f(mm): mi = mm + 1 else: ma = mm if f(mm): return mm + 1 return mm def main(): t = I() rr = [] for _ in range(t): h,c,t = LI() if h == t: rr.append(1) elif (h+c) / 2 >= t: rr.append(2) else: def g(i): return abs(((h+c) * i + h) - (i*2+1) * t) / (i*2+1) k = int((h-c) / (t - (h+c) / 2) / 4) r = 2 m = abs((h+c)/2 - t) for i in range(max(0,k-10), k+10): u = g(i) if m > u: m = u r = i * 2 + 1 rr.append(r) return JA(rr, "\n") print(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; long long x, y, t; void solve() { cin >> x >> y >> t; if (t >= x) { cout << "1\n"; return; } if (t <= (x + y) / 2) { cout << "2\n"; return; } long long ans = (y - t) / (x + y - 2 * t); long long sol = ans; for (int i = max(1LL, ans - 10); i <= ans + 10; i++) { long double tri = (long double)(i * x + (i - 1) * y) / (2 * i - 1); long double tri1 = (long double)(ans * x + (ans - 1) * y) / (2 * ans - 1); if (abs(t - tri1) > abs(t - tri)) { ans = i; } } long double tri = abs(t - (x + y) / 2); long double tri1 = (long double)(ans * x + (ans - 1) * y) / (2 * ans - 1); if (abs(t - tri1) > abs(t - tri)) { ans = 2; } cout << 2 * ans - 1 << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); 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
for i in range (int(input())): a,b,c=map(int,input().split()) d=a-c; e=c-b; f=e-d; if d>=e: print(2) elif d==0: print(1) elif 2<=(e/d): if(c*3-(2*a+b))>=(a-c)*3: print(1) else: print(3) else: g=round((d+e)/(2*(e-d))) x1=abs((g*b+(g+1)*a)*(2*g-1)-(2*g-1)*(2*g+1)*c) x2=abs(((g-1)*b+g*a)*(2*g+1)-(2*g-1)*(2*g+1)*c) if(x2<=x1): print(2*g-1) else : print(2*g+1)
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
#!/usr/bin/env python # coding:utf-8 # Copyright (C) dirlt from sys import stdin def run(h, c, t): if (h + c - 2 * t) >= 0: return 2 a = h - t b = 2 * t - h - c k = int(a / b) val1 = abs((k + 1) * h + k * c - (2 * k + 1) * t) val2 = abs((k + 2) * h + (k + 1) * c - (2 * k + 3) * t) # val1 / (2k+1) <= val2 / (2k+3), return 2k+1 # print(val1, val2) if val1 * (2 * k + 3) <= val2 * (2 * k + 1): ans = 2 * k + 1 else: ans = 2 * k + 3 return ans def main(): cases = int(stdin.readline()) for _ in range(cases): h, c, t = [int(x) for x in stdin.readline().split()] ans = run(h, c, t) print(ans) if __name__ == '__main__': import os if os.path.exists('tmp.in'): stdin = open('tmp.in') 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; void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long int gcd(long long int x, long long int y) { if (y == 0) return x; return gcd(y, x % y); } long long int lcm(long long int a, long long int b) { return (a * b) / gcd(a, b); } long long int logx(long long int base, long long int num) { int cnt = 0; while (num != 1) { num /= base; ++cnt; } return cnt; } vector<long long int> vin(long long int n) { vector<long long int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; return a; } void out(vector<long long int> a) { for (int i = 0; i < (long long int)a.size(); i++) { cout << a[i] << " "; } cout << endl; } void in(long long int *a, long long int n) { for (long long int i = 0; i < n; i++) cin >> a[i]; } void out(long long int *a, long long int n) { for (long long int i = 0; i < n; i++) { cout << a[i] << " "; } cout << '\n'; } void out1(vector<long long int> v) { for (long long int i = 0; i < (long long int)v.size(); i++) { cout << v[i] << " "; } cout << endl; } const long long int maxN = (long long int)(1 * 1e6 + 6); void solve(); void solve() { double h, c, t; cin >> h >> c >> t; double diff = abs(h - t); long long int ans = 1; if (abs((h + c) / 2 - t) < diff) { ans = 2; diff = abs((h + c) / 2 - t); } if (2 * t != h + c) { double k = (t - c) / (2 * t - h - c); double x1 = ceil(k); double x2 = floor(k); if (x2 > 1) { double val = abs((x2 * h + (x2 - 1) * c) / (2 * x2 - 1) - t); if (val < diff) { ans = 2 * x2 - 1; diff = val; } } if (x1 > 1) { double val = abs((x1 * h + (x1 - 1) * c) / (2 * x1 - 1) - t); if (val < diff) { ans = 2 * x1 - 1; diff = val; } } } cout << ans << '\n'; } int main() { fast(); long long int test; cin >> test; while (test--) { 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
""" This template is made by Satwik_Tiwari. python programmers can use this template :)) . """ #=============================================================================================== #importing some useful libraries. from fractions import Fraction import sys import bisect import heapq from math import * from collections import Counter as counter # Counter(list) return a dict with {key: count} from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)] from itertools import permutations as permutate from bisect import bisect_left as bl #If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect #If the element is already present in the list, # the right most position where element has to be inserted is returned #=============================================================================================== #some shortcuts mod = pow(10, 9) + 7 def inp(): return sys.stdin.readline().strip() #for fast input def out(var): sys.stdout.write(str(var)) #for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def graph(vertex): return [[] for i in range(0,vertex+1)] def zerolist(n): return [0]*n def nextline(): out("\n") #as stdout.write always print sring. def testcase(t): for p in range(t): solve() def printlist(a) : for p in range(0,len(a)): out(str(a[p]) + ' ') def lcm(a,b): return (a*b)//gcd(a,b) #=============================================================================================== # code here ;)) import decimal def func(n,h,c): return ((n+1)*h)+((n-1)*c) def solve(): h,c,t = sep() lowest = (h+c)/2 highest = h if(t>=highest): out(1) nextline() elif(t<=lowest): out(2) nextline() else: temp = (h-c)/((2*t) - (h+c)) n1 = floor(temp) if(n1 %2 == 0): n1-=1 n2 = ceil(temp) if(n2%2==0): n2+=1 # print(n1,n2) ansn1 = func(n1,h,c) ansn2 = func(n2,h,c) # print(ansn1,ansn2) if((abs((2*n1*t) - ansn1)/(2*n1)) <= (abs((2*n2*t) - ansn2)/(2*n2))): if((abs((2*n1*t) - ansn1)/(2*n1)) < abs(t-highest) and (abs((2*n1*t) - ansn1)/(2*n1))<abs(t-lowest)): out(n1) nextline() else: if(abs(t-lowest)<abs(t-highest)): out(2) nextline() else: out(1) nextline() else: if((abs((2*n2*t) - ansn2)/(2*n2)) < abs(t-highest) and (abs((2*n2*t) - ansn2)/(2*n2))<abs(t-lowest)): out(n2) nextline() else: if(abs(t-lowest)<abs(t-highest)): out(2) nextline() else: out(1) nextline() testcase(int(inp()))
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
q = int(input()) for _ in range(q): h, c, t = map(int, input().split()) if 2 * t <= h + c: print(2) continue l = 1 r = 10 ** 20 while r - l > 1: m = (r + l) // 2 if (m * h + (m - 1) * c >= t * (2 * m - 1)): l = m else: r = m a = l b = l + 1 m = b * h + c * (b - 1) z = a * h + c * (a - 1) x = (2 * a - 1) * (2 * b - 1) if (2 * b - 1) * z - t * x <= t * x - (2 * a - 1) * m: 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
from sys import stdin from collections import Counter, deque from bisect import bisect_left input = stdin.buffer.readline T = int(input()) for _ in range(T): h, c, t = map(int, input().split()) if t*2 <= h+c: print(2); continue a = int((t-h)/(h-2*t+c)) b = a+1 tt = t*(2*a+1)*(2*b+1) aa = abs(tt-(a*(c+h)+h)*(2*b+1)) bb = abs(tt-(b*(c+h)+h)*(2*a+1)) print(2*a+1 if aa <= bb else 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
import math def solve(h, c, t): if (h+c)>=2*t: return 2 a = (c-t)/float(h+c-2*t) a1 = math.floor(a) a2 = math.ceil(a) tmp = (2*a2-1)*(a1*h+(a1-1)*c)+(2*a1-1)*(a2*h+(a2-1)*c)-2*t*(2*a1-1)*(2*a2-1) if tmp<=0: return 2*a1-1 else: return 2*a2-1 t = int(input()) for _ in range(t): h,c,t = list(map(int, input().split())) print(solve(h, c, t)) ''' a, b, c = 41, 15, 30 t1 = (3*a+2*b)/float(5) t2 = (4*a+3*b)/float(7) print(t1, t2) '''
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 math import inf input = sys.stdin.buffer.readline def prog(): for _ in range(int(input())): h,c,t = map(int,input().split()) lowest = (h+c)/2 if t <= lowest: print(2) elif abs(t-(2*h+c)/3) >= abs(t-h): print(1) else: closest = (h-c)//(t-lowest) closest -= (closest % 2) if closest % 4 == 0: closest -= 2 closest = int(closest) first = lowest - t + (h-c)/closest second = abs(t - lowest - (h-c)/(closest+4)) if first <= second: print(closest//2) else: print(closest//2 + 2) prog() ''' import sys from math import inf import math from decimal import Decimal input = sys.stdin.readline def prog(): for _ in range(int(input())): h,c,t = map(int,input().split()) lowest = (h+c)/2 if t <= lowest: print(2) elif abs(t-(2*h+c)/3) >= abs(t-h): print(1) else: closest = (h-c)//(t-lowest) closest -= (closest % 2) closest = int(closest) if closest % 4 == 0: closest -= 2 a = Decimal(lowest) + Decimal((h-c))/Decimal(closest) b = Decimal(lowest) + Decimal((h-c))/Decimal(closest+4) first = a-t second = t-b print(first) print(second) if first <= second: print(int(closest//2)) else: print(int(closest//2) + 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 sys input = sys.stdin.buffer.readline for _ in range(int(input())): h, c, t = map(int, input().split()) if h <= t: print(1) elif (h + c) // 2 >= t: print(2) else: n_c = (h - t) // (2 * t - h - c) n_h = n_c + 1 if abs((n_c * c + n_h * h) - t * (n_c + n_h)) * (n_c + n_h + 2) > abs((n_c * c + n_h * h + h + c) - t * (n_c + n_h + 2))*(n_c + n_h) : print(n_c + n_h + 2) else: print(n_c + n_h)
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 def minOdd (n,m,h,c,t): d = abs((n+1)*(2*m+1)*h + (2*m+1)*n*c - t*(2*n+1)*(2*m+1))-abs((m+1)*(2*n+1)*h + (2*n+1)*m*c - t*(2*n+1)*(2*m+1)) if d>0: return m elif d<0: return n else: return min(n,m) def ans (odd,h,c,t): d = abs(2*(odd+1)*h + 2*odd*c - 2*(2*odd+1)*t) - abs((h+c)*(2*odd+1) - 2*(2*odd+1)*t) if d < 0: return 2*odd+1 elif d > 0: return 2 else: return min (2, 2*odd+1) T = int(input()) for _ in range (T): h,c,t = map(int, input().split()) av = (h+c)/2 if t <= av: print (2) continue n = (h-t)/(2*t - (h+c)) if math.floor(n) == math.ceil(n): print (int(2*n + 1)) continue n1 = math.ceil(n) n2 = math.floor(n) m = minOdd (n1,n2,h,c,t) print(ans(m, 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
def solver(h, c, t): if h + c >= 2 * t: return 2 elif h == t: return 1 else: x = (t - c) // (2 * t - h - c) k1, k2 = abs((2*x-1)*t- ((h+c)*x-c))*(2*x+1), abs(t*(2*x+1) - ((h+c)*x+h))*(2*x-1) z = 2 * x + 1 if k2 < k1 else 2 * x - 1 return z for _ in range(int(input())): H, C, T = [int(i) for i in input().split()] print(solver(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
def solve(h, c, t): if t >= h: return 1 if 2 * t <= h + c: return 2 if (h - t) % (2 * t - h - c) == 0: return 2 * (h - t) // (2 * t - h - c) + 1 nn1 = int((h - t) / (2 * t - h - c)) nn2 = nn1 + 1 if (t * (2 * nn2 + 1) - nn2 * (h + c) - h) * (2 * nn1 + 1) - (nn1 * (h + c) + h - t * (2 * nn1 + 1)) * (2 * nn2 + 1) < 0: return 2 * nn2 + 1 return 2 * nn1 + 1 t = int(input()) for i_t in range(t): [h, c, tt] = list(map(int, input().split(" "))) print(solve(h, c, tt))
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 derrick20 */ import java.io.*; import java.util.*; public class MixingWaterDirect { public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); while (T-->0) { double h = sc.nextDouble(); double c = sc.nextDouble(); double t = sc.nextDouble(); if (t <= (c + h) / 2.0) { out.println(2); } else { // now we know h + c - 2t != 0 // this is the first integer the lets us exceed t if (h <= t) { out.println(1); } else { int a = (int) Math.floor((h - t) / (2 * t - h - c)); // this is safe, since it's just a single division // the below computations are changing the numerator and denominator by very small relative amounts, // so it's not detectable. double num1 = (a + 1) * h + (a) * c - t * (2 * (a) + 1); double denom1 = 2 * (a) + 1; double num2 = t * (2 * (a + 1) + 1) - ((a + 2) * h + (a + 1) * c); // negate to make positive double denom2 = 2 * (a + 1) + 1; // double d1 = calc(h, c, a - 1) - t; // double d2 = t - calc(h, c, a); out.println(num1 * denom2 <= num2 * denom1 ? 2 * (a) + 1 : 2 * (a + 1) + 1); } } } out.close(); } static double calc(double h, double c, double a) { return ((a + 1) * h + a * c) / (2 * a + 1); } static void generate() { int h = (int) (200 * Math.random()); int c = (int) (h * Math.random()); int t = (int) (((h + c) / 2) + (h - (h + c) / 2) * Math.random()); System.out.println(h + " " + c + " " + t); } static class FastScanner { private int BS = 1<<16; private char NC = (char)0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt=1; boolean neg = false; if(c==NC)c=getChar(); for(;(c<'0' || c>'9'); c = getChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=getChar()) { res = (res<<3)+(res<<1)+c-'0'; cnt*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=getChar(); while(c>32) { res.append(c); c=getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=getChar(); while(c!='\n') { res.append(c); c=getChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=getChar(); if(c==NC)return false; else if(c>32)return true; } } } static void ASSERT(boolean assertion, String message) { if (!assertion) throw new AssertionError(message); } }
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; double val(long long x, long long h, long long c) { return ((h * x) + (c * (x - 1))) / ((x * 2.0) - 1.0); } int main() { long long T, h, c, t; cin >> T; while (T--) { cin >> h >> c >> t; if (t <= (h + c) / 2) { cout << 2 << "\n"; continue; } double cur = (h + c) / 2.0; long long l = 1, hi = 1, ans; while (val(hi, h, c) > t) { hi *= 2; if (val(hi, h, c) - t < abs(t - cur)) cur = val(hi, h, c), ans = hi; else if (abs(val(hi, h, c) - t) == abs(t - cur) && hi < ans) ans = hi; } while (hi >= l) { long long m = (hi + l) / 2; if (val(m, h, c) == t) { ans = m; break; } if (val(m, h, c) > t) l = m + 1; else hi = m - 1; if (abs(t - val(m, h, c)) < abs(t - cur)) cur = val(m, h, c), ans = m; else if (abs(val(m, h, c) - t) == abs(t - cur) && m < ans) ans = m; } cout << (ans * 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
import sys from math import ceil, floor from functools import cmp_to_key input = sys.stdin.readline def main(): tc = int(input()) cmp = lambda a, b: a[0] * b[1] - a[1] * b[0] cmp = cmp_to_key(cmp) for _ in range(tc): h, c, t = map(int, input().split()) if h + c == 2 * t: print(2) else: x = max(0, (t - h) // (h + c - 2 * t)) cands = [ ((h + c) - 2 * t, 2), (x * (h + c) + h - (2 * x + 1) * t, 2 * x + 1), ((x + 1) * (h + c) + h - (2 * x + 3) * t, 2 * x + 3) ] cands = map(lambda t: (abs(t[0]), t[1]), cands) print(min(cands, key = cmp)[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
def check(h, c, t, n): return (n + 1) * h + n * c >= (2 * n + 1) * t def binsearch(h, c, t): left = 0 right = 1000000000 while right - left > 1: mid = (right + left) // 2 if check(h, c, t, mid): left = mid else: right = mid if check(h, c, t, right): return right else: return left T = int(input()) for z in range(0, T): h, c, t = [int(i) for i in input().split(' ')] if 2 * t <= h + c: print(2) else: n = binsearch(h, c, t) if (2 * n + 3) * ((n + 1) * h + n * c - (2 * n + 1) * t) <= (2 * n + 1) * (-(n + 2) * h - (n + 1) * c + (2 * n + 3) * t): print(2 * n + 1) else: print(2 * n + 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
t=int(input()) for test in range(t): inp=input().split() h=int(inp[0]) c=int(inp[1]) t=int(inp[2]) if t+t <= c+h: print(2) continue if t >= h: print(1) continue k = (h-t)//(t+t-c-h) if (h-t)%(t+t-c-h) == 0: print(k+k+1) continue k+=1 if (k+k+1)*(k*h + k*c - c) + (k+k-1)*(k*h + k*c + h) > (t+t)*(k+k-1)*(k+k+1): print(k+k+1) else: print(k+k-1)
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 Main { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); public static void main(String[] args) { new Main().run(); } void run() { for (int i = 0; i < n; i++) { long h = sc.nextLong(); long c = sc.nextLong(); long t = sc.nextLong(); if (h+c >= 2*t) { System.out.println(2); continue; } if (t >= h) { System.out.println(1); continue; } long l = 0; long r = (long)1e9; while (l<r) { long mid = (l+r) / 2; if (t*(mid*2+1) < (mid+1)*h + mid*c) l = mid + 1; else r = mid; } // System.out.println(l); long pre = l-1; long post = l+1; long diffPre = Math.abs((pre+1)*h + pre*c - t*(pre*2+1)); long diffMid = Math.abs((l+1)*h + l*c - t*(l*2+1)); long diffPost = Math.abs((post+1)*h + post*c - t*(post*2+1)); // System.out.println((double)diffPre/(double)(l*2-1)); // System.out.println((double)diffMid/(double)(l*2+1)); // System.out.println((double)diffPost/(double)(l*2+3)); if ((double)diffPre/(double)(l*2-1)<=(double)diffMid/(double)(l*2+1) && (double)diffPre/(double)(l*2-1)<=(double)diffPost/(double)(l*2+3)) { System.out.println(2*pre+1); continue; } if ((double)diffMid/(double)(l*2+1)<=(double)diffPost/(double)(l*2+3)) { System.out.println(2*l+1); continue; } System.out.println(2*post+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
for _ in range(int(input())): h,c,t=map(int,input().split()) h-=c t-=c if h==t: print(1) elif h/2>=t: print(2) else: #def f(mid):return h*(1+mid)/(1+2*mid) def p(a): return h*(1+a) def q(a): return 1+2*a a=0 b=10**10 while a+1!=b: mid=(a+b)//2 val=p(mid) T=t*q(mid) if val==T: print(1+2*mid) break if val<T: b=mid else: a=mid if val!=T: if 2*t*q(a)*q(b)>=p(a)*q(b)+p(b)*q(a): print(1+2*a) else: print(1+2*b)
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.util.*; import static java.lang.Math.*; public class Main { public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); // br = new BufferedReader(new FileReader("input.txt")); // PrintWriter pw = new PrintWriter("output.txt"); int T = nextInt(); for (int q = 0; q < T; q++) { long h = nextLong(); long c = nextLong(); long t = nextLong(); if (h + c - 2 * t >= 0) pw.println(2); else { long k = 2 * ((h - t) / (2 * t - h - c)) + 1; long left = abs(k / 2 * c + (k + 1) / 2 * h - t * k); long right = abs((k + 2) / 2 * c + (k + 3) / 2 * h - t * (k + 2)); if (left * (k + 2) <= right * k) pw.println(k); else pw.println(k + 2); } } pw.close(); } static long mod = 1000000007; static StringTokenizer st = new StringTokenizer(""); static BufferedReader br; static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } } /*2 1 1 2 1 2 3 1 1 3 1 2 3 2 2 3 1 3 3 2 3 4 1 1 4 1 2 4 2 2 4 1 3 4 2 3 4 3 3 4 1 4 4 2 4 4 3 4 5 1 1 5 1 2 5 2 2 5 1 3 5 2 3 5 3 3 5 1 4 5 2 4 5 3 4 5 4 4 5 1 5 5 2 5 5 3 5 5 4 5 6 1 1 6 1 2 6 2 2 6 1 3 6 2 3 6 3 3 6 1 4 6 2 4 6 3 4 6 4 4 6 1 5 6 2 5 6 3 5 6 4 5 6 5 5 6 1 6 6 2 6 6 3 6 6 4 6 6 5 6 7 1 1 7 1 2 7 2 2 7 1 3 7 2 3 7 3 3 7 1 4 7 2 4 7 3 4 7 4 4 7 1 5 7 2 5 7 3 5 7 4 5 7 5 5 7 1 6 7 2 6 7 3 6 7 4 6 7 5 6 7 6 6*/ // 7 1 7
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 math import ceil for _ in range(int(input())): h,c,t=map(int,input().split()) if((h+c)>=t*2): print(2) elif(h-t==0): print(1) else: ans=abs(2*t-c-h) temp=(ceil((h-t)/ans)) res=1000000 hh=0 cc=0 res2=1 for i in range(max(temp,1),temp+2): for j in range(i-1,i): newres=abs((h*i+c*j)-t*(i+j)) if(newres*res2<res*(i+j)): res=newres res2=(i+j) hh=i cc=j print(hh+cc)
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 i_by_t(h, c, t): return ((h - c) / (t - (h + c) / 2) + 2) / 2 for _ in range(int(input())): h, c, t = map(int, input().split()) mean = (h + c) / 2 def temp(i): half = i // 2 return (h * (i - i // 2) + c * half) / i T = temp(1) if t >= T: print(1) continue best_eps = abs(T - t) T = temp(2) if t <= T: print(2) continue best_i = int(i_by_t(h, c, t)) if best_i % 2 == 0: best_i -= 1 i = best_i best_i = 1 while True: T = temp(i) eps = abs(T - t) if eps < best_eps: best_i = i if eps <= best_eps: best_eps = eps else: print(best_i) break i += 2
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> #pragma GCC optimize("Ofast") using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long T, h, c, t; cin >> T; while (T--) { cin >> h >> c >> t; long double min_dif = fabs((h + c) / 2.0 - t); if (h - t >= t - c) { cout << "2\n"; continue; } long long ans = 1; long long L = 1, R = 1e18, M1, M2; while (L <= R) { M1 = R - (R - L) / 2; long long av = (h * M1 + c * (M1 - 1)) / (2 * M1 - 1); if ((long double)(h * M1 + c * (M1 - 1.0)) / (2.0 * M1 - 1.0) > t) { ans = M1; L = M1 + 1; } else { R = M1 - 1; } } min_dif = (long double)(h * ans + c * (ans - 1.0)) / (2.0 * ans - 1.0); if (fabs((long double)(h * (ans + 1.0) + c * ((ans + 1.0) - 1.0)) / (2.0 * ans + 1.0) - t) < fabs(min_dif - t)) { ans += 1; } cout << 2 * ans - 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
#include <bits/stdc++.h> using namespace std; bool sortinrev(const pair<long long, long long> &a, const pair<long long, long long> &b) { return (a.first > b.first); } long long power(long long x, unsigned long long y) { long long p = 998244353; long long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } void solve() { long long h, c, t; cin >> h >> c >> t; if (h == t) { cout << "1" << "\n"; return; } if (h + c >= 2 * t) { cout << "2" << "\n"; return; } long long p = t - c; long long l = (2 * t) - h - c; long long p1 = (2 * p) - l; long long high; if (p1 % l == 0) high = p1 / l; else high = (p1 / l) + 1; if (high % 2 == 0) high += 1; long long low = high - 2; double temp1 = (((low / 2) * (h + c)) + h) / (low * 1.0); double temp2 = (((high / 2) * (h + c)) + h) / (high * 1.0); if (abs(temp2 - t) > abs(t - temp1)) cout << low << "\n"; else if (abs(temp2 - t) == abs(t - temp1)) cout << low << "\n"; else cout << high << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long 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
#include <bits/stdc++.h> using namespace std; const int maxn = 100001; long long h, c, temp; long double f(long long m) { long double r = 1.0 * (m * h + (m)*c) / (2 * m); return (abs(r - temp)); } long double f12(long long m) { long double r = 1.0 * (m * h + (m - 1) * c) / (2 * m - 1); return (abs(r - temp)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; t = 1; cin >> t; while (t--) { long long i, j, k; cin >> h >> c >> temp; long long l = 1; long long r = 1000000000; while (r - l > 2) { long long m1 = (l * 2 + r) / 3; long long m2 = (l + 2 * r) / 3; if (f(m1) > f(m2)) l = m1; else r = m2; } long double ans = f(2); j = 2; for (i = l; i <= r; i++) { if (f(i) < ans) { j = 2 * i; ans = f(i); } } l = 1; r = 1000000000; while (r - l > 2) { long long m1 = (l * 2 + r) / 3; long long m2 = (l + 2 * r) / 3; if (f12(m1) > f12(m2)) l = m1; else r = m2; } for (i = l; i <= r; i++) { if (f12(i) < ans) { j = 2 * i - 1; ans = f12(i); } } cout << j << "\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 sys from decimal import * input = sys.stdin.readline def main(): t = int(input()) for _ in range(t): H, C, T = [int(x) for x in input().split()] if H == T: print(1) continue if (H + C) / 2 == T: print(2) continue if (H + C) / 2 > T: print(2) continue def isOK(mid): if ((H + C) * mid + H) / (2 * mid + 1) > T: return True else: return False ok = 0 ng = 10 ** 20 while abs(ok - ng) > 1: mid = (ok + ng) // 2 if isOK(mid): ok = mid else: ng = mid if abs(Decimal(str(T)) - Decimal(str((H + C) * ok + H)) / Decimal(str(2 * ok + 1))) > abs(Decimal(str(T)) - Decimal(str((H + C) * ng + H)) / Decimal(str(2 * ng + 1))): print(ng * 2 + 1) else: print(ok * 2 + 1) if __name__ == '__main__': 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 T; long long h, c, t; int main() { cin >> T; while (T--) { cin >> h >> c >> t; if (h + c >= t * 2) { cout << 2 << '\n'; continue; } long long res = 0; long long n = ceil((h - t) / (2.0 * t - (h + c))); long long res2 = ((n * (h + c) + h) - t * (2 * n + 1)) * (2 * n - 1); long long res1 = (((n - 1) * (h + c) + h) - t * (2 * n - 1)) * (2 * n + 1); if (res2 < 0) res2 = ~res2 + 1; if (res1 < 0) res1 = ~res1 + 1; if (n > 0 && res2 >= res1) { --n; } res = (n << 1) + 1; cout << res << '\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 io,os input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline import sys from math import floor,ceil def solve(h,c,t): if h==t:return 1 if 2*t<=h+c:return 2 if 6*t>=5*h+c:return 1 if 3*t>=2*h+c:return 3 if (h-t)%(2*t-(h+c))==0: cnt=(h-t)//(2*t-(h+c)) return 2*cnt+1 k,l=floor((h-t)/(2*t-(h+c))),ceil((h-t)/(2*t-(h+c))) t_k=abs((h*(k+1)+c*k)/(2*k+1)-t) t_l=abs((h*(l+1)+c*l)/(2*l+1)-t) if (2*l+1)*(h*(k+1)+c*k)+(2*k+1)*(h*(l+1)+c*l)<=2*t*(2*k+1)*(2*l+1): return 2*k+1 return 2*l+1 def main(): T=int(input()) for _ in range(T): h,c,t=map(int,input().split()) ans=solve(h,c,t) sys.stdout.write(str(ans)+'\n') if __name__=='__main__': 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::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int h, c, t; cin >> h >> c >> t; if (h + c >= 2 * t) { cout << "2\n"; } else { int k = (h - t) / (2 * t - h - c); if (abs((k + 1) * h + k * c - t * (2 * k + 1)) * (2 * k + 3) > abs((k + 2) * h + (k + 1) * c - t * (2 * k + 3)) * (2 * k + 1)) cout << 2 * (k + 1) + 1 << '\n'; else cout << 2 * k + 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 collections import Counter,defaultdict,deque #from heapq import * #import itertools #from operator import itemgetter #from itertools import count, islice #from functools import reduce #alph = 'abcdefghijklmnopqrstuvwxyz' #dirs = [[1,0],[0,1],[-1,0],[0,-1]] #from math import factorial as fact #a,b = [int(x) for x in input().split()] #sarr = [x for x in input().strip().split()] #import math from math import * import sys input=sys.stdin.readline #sys.setrecursionlimit(2**30) #mod = 10**9+7 def solve(): h,c,t = [int(x) for x in input().split()] avg = (h+c)/2 if avg>=t: print(2) return d = t-avg n1 = floor((h-avg)/d) if n1%2==0: n1-=1 n2 = n1+2 a1 = d-((h-avg)/n1) a2 = d-((h-avg)/n2) if abs(a1)<=abs(a2): print(n1) else: print(n2) tt = int(input()) for test in range(tt): 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
_ = int(input()) for T in range(_): h, c, t = [int(x) for x in input().split()] if t <= (h + c) / 2: print(2) continue x = (t - c) // (2 * t - h - c) v1 = x * h + (x - 1) * c v2 = (x + 1) * h + x * c if abs(v1 - (2 * x - 1) * t) / (2 * x - 1) <= abs(t * (2 * x + 1) - v2) / (2 * x + 1): print(2 * x - 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
import java.util.*; public class solution{ public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int t1=sc.nextInt(); while(t1-->0){ int h=sc.nextInt(); int c=sc.nextInt(); int t=sc.nextInt(); // case 1 n is NegativeArraySizeException if(h==t){ System.out.println("1"); }else if(2*t<=h+c){ System.out.println("2"); }else{ // long ans=0; long pd=(h-t)/(2*t-h-c); // double td1=(double)((pd+1)*h+pd*c)/(2*pd+1); double abs2=Math.abs(t*(2*pd+1)-((pd+1)*h+pd*c))/(2*pd+1.0); // ans=2*pd+1; pd++; double abs3=Math.abs(t*(2*pd+1)-((pd+1)*h+pd*c))/(2*pd+1.0); if(abs3>=abs2) pd--; System.out.println(2*pd+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; long long h, c, t; double f(long long num) { long long tot = (2 * num - 1); long long val = ceil(tot / 2.) * h + (tot / 2) * c; double avg = ((double)val / tot); double ret = fabs(avg - (double)t); return ret; } void test_cases() { scanf("%lld%lld%lld", &h, &c, &t); double cur = fabs(t - (h + c) / 2.); long long lo = 0, hi = 1000000., ans = 0; while (hi - lo > 1) { long long mid = (lo + hi) >> 1; if (f(mid) > f(mid + 1)) lo = mid; else hi = mid; } ans = 2 * (lo + 1) - 1; long long ret; double fans = f(ans); if (fans == cur) { if (ans < 2) ret = ans; else ret = 2; } if (fans < cur) ret = ans; else ret = 2; if (ret == 0) ret = 2; printf("%lld\n", ret); } int main() { int tc; scanf("%d", &tc); while (tc--) { test_cases(); } }
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() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int T; cin >> T; while (T--) { long long int h, c, t; cin >> h >> c >> t; if (t == h) { cout << "1" << "\n"; continue; } if (h + c == 2 * t || c == t) { cout << "2" << "\n"; continue; } if ((2 * t) < (h + c)) { cout << "2" << "\n"; continue; } long long int num = c - t; long long int den = h + c - (2 * t); long long int x1 = num / den; long long int x2 = x1 + 1; double y1 = ((double)((x1 * h) + ((x1 - 1) * c))) / ((double)((2 * x1) - 1)); double y2 = ((double)((x2 * h) + ((x2 - 1) * c))) / ((double)((2 * x2) - 1)); double d1 = y1 - t; if (d1 < 0) { d1 = -d1; } double d2 = y2 - t; if (d2 < 0) { d2 = -d2; } if (d1 <= d2) { cout << 2 * x1 - 1 << "\n"; continue; } else { cout << 2 * x2 - 1 << "\n"; continue; } } }
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; const int MOD = 1e8; int main() { int q; cin >> q; while (q--) { long long h, c, t, l, r, m; cin >> h >> c >> t; if (t >= h) { cout << "1\n"; continue; } l = 1; r = 1e9; while (r - l > 1) { m = (l + r) / 2; if ((m * h + (m - 1) * c) / (long double)(m * 2 - 1) > t) l = m; else r = m; } long double x1 = (l * h + (l - 1) * c) / (long double)(l * 2 - 1), x2 = (r * h + (r - 1) * c) / (long double)(r * 2 - 1); if (abs((h + c) / 2.L - t) <= abs(x1 - t) && abs((h + c) / 2.L - t) <= abs(x2 - t)) cout << "2\n"; else if (abs(x1 - t) <= abs(x2 - t)) cout << l * 2 - 1 << '\n'; else cout << r * 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
import sys input = sys.stdin.buffer.readline def I(): return(list(map(int,input().split()))) def sieve(n): a=[1]*n for i in range(2,n): if a[i]: for j in range(i*i,n,i): a[j]=0 return a for __ in range(int(input())): h,c,t=I() # for n in range(1,22,2): # if n%2: # avg=(h+c*(n-1)//2+h*(n-1)//2)/n # else: # avg=(h*n//2+c*n//2)/n # print(avg-(h+c)/2,n) if h==t: print(1) continue # if (h+c)/2>=t: # print(2) # continue # if (h+c)/2>t: # if (h+c)/2<h: # print(2) # else: # print(1) # continue if (h+c)/2>=t: print(2) continue if t>=(h*2+c)/3: x1=(h*2+c)/3 x2=h if abs(x2-t)<=abs(x1-t): print(1) else: print(3) continue l=1 r=10**30 ans=10**100 # print("---------") left=t-(h+c)/2 # print(left) while(l<=r): mid=(l+r)//2 n=mid delta=(h-c)/(2*(2*n+1)) # print(mid) # print(delta,2*n+1) if delta==left: ans=n break if delta<left: ans=n r=mid-1 else: l=mid+1 n=ans delta1=(h-c)/(2*(2*n+1)) delta2=(h-c)/(2*(2*n-1)) # print(delta1) if abs(delta1-left)<abs(delta2-left): print(2*n+1) else: print(2*n-1) # print(2*ans+1) # print("---------")
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 in(char &c) { return scanf("%c", &c); } int in(char s[]) { return scanf("%s", s); } int in(int &x) { return scanf("%d", &x); } int in(long long &x) { return scanf("%lld", &x); } int in(double &x) { return scanf("%lf", &x); } int in(pair<int, int> &p) { return scanf("%d%d", &p.first, &p.second); } int in(pair<long long, long long> &p) { return scanf("%lld%lld", &p.first, &p.second); } int in(pair<double, double> &p) { return scanf("%lf%lf", &p.first, &p.second); } int in(long double &a) { return scanf("%Lf", &a); } int in(int &x, long long &y) { return scanf("%d%lld", &x, &y); } int in(long long &x, int &y) { return scanf("%lld%d", &x, &y); } int in(int &x, int &y) { return scanf("%d%d", &x, &y); } int in(long long &x, long long &y) { return scanf("%lld%lld", &x, &y); } int in(int &n, char s[]) { return scanf("%d%d", &n, s); } int in(char a[], char b[]) { return scanf("%s%s", a, b); } int in(double &x, double &y) { return scanf("%lf%lf", &x, &y); } int in(int &x, int &y, int &z) { return scanf("%d%d%d", &x, &y, &z); } int in(long long &x, long long &y, long long &z) { return scanf("%lld%lld%lld", &x, &y, &z); } int in(double &x, double &y, double &z) { return scanf("%lf%lf%lf", &x, &y, &z); } int in(int &x, int &y, int &z, int &k) { return scanf("%d%d%d%d", &x, &y, &z, &k); } int in(long long &x, long long &y, long long &z, long long &k) { return scanf("%lld%lld%lld%lld", &x, &y, &z, &k); } int in(double &x, double &y, double &z, double &k) { return scanf("%lf%lf%lf%lf", &x, &y, &z, &k); } const int N = 3e5 + 10, M = 1e3 + 10, mod = 1e9 + 7; const double eps = 1e-6, Pi = acos(-1.0); double cost(long long x, long long h, long long c) { if (x <= 0) return 1e18; return 1.0 * (h * x + (x - 1) * c) / (2 * x - 1); } int main() { int T; in(T); while (T--) { long long h, c, t; in(h, c, t); if (h + c == t * 2 || c >= t || h + c >= 2 * t) printf("2\n"); else { double a = c - t; double b = h + c - 2 * t; long long x = a / b; double s[3]; s[0] = fabs(t - cost(x - 1, h, c)); s[1] = fabs(t - cost(x, h, c)); s[2] = fabs(t - cost(x + 1, h, c)); long long ans = x - 1; if (s[0] <= s[1] && s[0] <= s[2]) ans = x - 1; else if (s[1] <= s[0] && s[1] <= s[2]) ans = x; else if (s[2] <= s[0] && s[2] <= s[1]) ans = x + 1; 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 sys input = lambda: sys.stdin.readline().rstrip() T = int(input()) for _ in range(T): h, c, t = map(int, input().split()) a = h - t b = t - c if a <= 0: print(1) elif a >= b: print(2) else: mi = (a, 1) mii = 1 if abs((a+b) * mi[1]) < mi[0] * 2: mi = (abs(a+b), 2) mii = 2 s = max(b//(b-a), 1) for x in range(s, s + 2): t = abs(a * x - b * (x - 1)) if t * mi[1] < mi[0] * (2 * x - 1): mi = (t, 2 * x - 1) mii = x * 2 - 1 # print("mii =", mii, mi[0] / mi[1]) print(mii)
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 mod = 1000000007; const long long MOD = 998244353; const long long inf = 1e18; const long long MAX = 2e5 + 1; inline long long add(long long a, long long b) { return ((a % mod) + (b % mod)) % mod; } inline long long sub(long long a, long long b) { return ((a % mod) - (b % mod) + mod) % mod; } inline long long mul(long long a, long long b) { return ((a % mod) * (b % mod)) % mod; } vector<long long> fac(1e6); long long pwr(long long x, long long n) { x = x % mod; if (!n) return 1; if (n & 1) return mul(x, pwr(mul(x, x), (n - 1) / 2)); else return pwr(mul(x, x), n / 2); } long long modinv(long long n) { return pwr(n, mod - 2); } long long inv(long long i) { if (i == 1) return 1; return (MOD - (MOD / i) * inv(MOD % i) % MOD) % MOD; } long long ncr(long long n, long long r) { long long ans = 1; if (r != 0 && r != n) { ans = fac[n] * modinv(fac[r]) % MOD * modinv(fac[n - r]) % MOD; } return ans; } void pre() { fac[0] = 1; for (int i = 1; i < (int)fac.size(); i++) { fac[i] = (fac[i - 1] * i) % MOD; } } void solve() { long double h, c, t; cin >> h >> c >> t; vector<pair<long double, long double>> p; if (h == t) cout << 1 << endl; else if ((c + h) >= 2 * t) cout << 2 << endl; else { long long x = (long long)(abs(t - c) / abs(-h - c + 2 * t)); long double first = (x * h + (x - 1) * c) / (1.0 * (2 * x - 1)); long long y = x + 1; long double second = (y * h + (y - 1) * c) / (1.0 * (2 * y - 1)); p.push_back({abs(t - first), x}); p.push_back({abs(t - second), x + 1}); sort(p.begin(), p.end()); cout << (2 * p[0].second) - 1 << endl; } } int main() { 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
import java.io.*; import java.util.*; import java.lang.Math; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int T = scan.nextInt(); StringBuilder answer = new StringBuilder(); for (int i = 0; i < T; ++i) { int h = scan.nextInt(); int c = scan.nextInt(); int t = scan.nextInt(); double even = (h + c) / 2; long cups = 0; if (h == t) { cups = 1L; } else if ((double) t <= even) { cups = 2L; } else { double m = (double) (t - c) / (double) (2 * t - (h + c)); long ml = (long) Math.floor(m); long mh = (long) Math.ceil(m); // (2 * ml - 1) * t = hml + cml - c - 2mlt + t //ml (h + c - 2t) + t - c / 2 * ml - 1 // hml + cml - c - t * (2ml - 1); // hml + cml - c - 2tml + t long leftDiff = ml * (h + c - 2 * t) + t - c; long rightDiff = mh * (h + c - 2 * t) + t - c; leftDiff = (long) Math.abs(leftDiff) * (long) (2 * mh - 1); rightDiff = (long) Math.abs(rightDiff) * (long) (2 * ml - 1); if (leftDiff <= rightDiff) { cups = 2 * ml - 1; } else { cups = 2 * mh - 1; } } answer.append(cups + "\n"); } System.out.println(answer); } }
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 math from decimal import * tt = int(input()) for t in range(tt): x2,x1,x0 = input().split(' ') a1 = int(x1) a2 = int(x2) a0 = int(x0) # if(a0 >= a2): # print(1) # continue if(a0 <= (a1+a2)/2): print(2) continue ans1 = math.floor((a0-a2)/(a1+a2-2*a0)) ans2 = math.ceil((a0-a2)/(a1+a2-2*a0)) y1 = ((a1+a2)*ans1+a2)/Decimal((2*ans1+1)) y2 = ((a1+a2)*ans2+a2)/Decimal((2*ans2+1)) distance_ans1 = abs(y1 - a0) distance_ans2 = abs(y2 - a0) if (distance_ans2 < distance_ans1): x = ans2 else: x = ans1 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
import math from sys import stdin,stdout import sys import fractions mod=1000000007 F=fractions.Fraction t=int(stdin.readline()) while t>0: h,c,tt=list(map(int,stdin.readline().split())) mini=sys.maxsize ans=0 if(2*tt>h+c): low=1 high=1000000 mid=low+(high-low)//2 x=0 while(low<=high): mid=low+(high-low)//2 if(mid<(h-tt)/((2*tt)-(h+c))): x=mid low=mid+1 else: high=mid-1 if(mini>abs(tt-F(h*(x+1)+c*x,(2*x+1)))): mini=abs(tt-F(h*(x+1)+c*x,(2*x+1))) ans=2*x+1 low=1 high=1000000 mid=low+(high-low)//2 x=0 while(low<=high): mid=low+(high-low)//2 if(mid>=(h-tt)/((2*tt)-(h+c))): x=mid high=mid-1 else: low=mid+1 if(mini>abs(tt-F(h*(x+1)+c*x,(2*x+1)))): mini=abs(tt-F(h*(x+1)+c*x,(2*x+1))) ans=2*x+1 else: ans=2 stdout.write(f"{ans}\n") t-=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
for _ in range(int(input())): a,b,c=map(int,input().split()) mid=(a+b)/2 if c==a: print(1) elif c<=mid: print(2) elif c>=(mid+(a-b)/6) and c<a: d=(mid+(a-b)/6) if abs(d-c)<abs(a-c): print(3) else: print(1) else: c-=mid d=[] x=(a-b)//c if x%2==0: xx=x//2 if xx%2==0: h=max(6,x-2) i=max(6,h-4) j=max(6,h+4) else: h=max(6,x) i=max(6,h-4) j=max(6,h+4) e=[abs(((a-b)/h)-c),h] f=[abs(((a-b)/i)-c),i] g=[abs(((a-b)/j)-c),j] d.append(e) d.append(f) d.append(g) d.sort() print(int(d[0][1]//2)) else: xx=(x+1)//2 if xx%2==0: h=max(6,x-1) i=max(6,h+4) else: h=max(6,x-3) i=max(6,h+4) e=[abs(((a-b)/h)-c),h] f=[abs(((a-b)/i)-c),i] d.append(e) d.append(f) d.sort() print(int(d[0][1]//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
t=int(raw_input()) for _ in xrange(t): h,c,t=map(int,raw_input().split()) if 2*t<=h+c: print 2 elif t>=h: print 1 else: k1=(h-t)/(2*t-h-c) f1=[(k1+1)*h+k1*c,2*k1+1] f2=[(k1+2)*h+(k1+1)*c,2*k1+3] s=[f1[0]*f2[1]+f1[1]*f2[0],f1[1]*f2[1]] if s[0]<=2*t*s[1]: print 2*k1+1 else: print 2*k1+3
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
import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush from math import * from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect from time import perf_counter from fractions import Fraction # sys.setrecursionlimit(int(pow(10, 2))) # sys.stdin = open("input.txt", "r") # sys.stdout = open("output.txt", "w") mod = int(pow(10, 9) + 7) mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end) def l(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] # @lru_cache(None) for i in range(int(input())): h,c,t=map(int,input().split()) if (h+c)/2>=t: print(2) else: a = (h-t)//(2*t-h- c) b = a+1 print(2*a + 1 if 2*t*(2*a+1)*(2*b+1) >= (2*b+1)*((a+1)*h+a*c)+(2*a+1)*((b+1)*h+b*c) else 2 * b + 1)
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 = 9e18; const long long MOD = 1e9 + 7; const long long mod = 998244353; void fileio() {} int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; fileio(); long long tc; cin >> tc; while (tc--) { long long h, c, t; cin >> h >> c >> t; if (t <= (h + c) / 2) { cout << 2 << endl; continue; } long long k1 = (t - c) / (2 * t - h - c); long long k2 = k1 + 1; double t1, t2; double d1, d2; t1 = k1 * h + (k1 - 1) * c; t1 /= (2 * k1 - 1); t2 = k2 * h + (k2 - 1) * c; t2 /= (2 * k2 - 1); d1 = abs(t1 - t); d2 = abs(t2 - t); if (d1 <= d2) cout << 2 * k1 - 1 << endl; else cout << 2 * k2 - 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
#!/usr/bin/env python3 import sys import math from bisect import bisect_right as br from bisect import bisect_left as bl from collections import defaultdict from itertools import accumulate from collections import Counter from collections import deque from operator import itemgetter from itertools import permutations mod = 10**9 + 7 inf = float('inf') def I(): return int(sys.stdin.readline()) def LI(): return list(map(int,sys.stdin.readline().split())) T = I() def f(x): if ((x + 1) * h + (x - 1) * c) - 2 * x * t >= 0: return ((x + 1) * h + (x - 1) * c) - 2 * x * t return 2 * x * t - ((x + 1) * h + (x - 1) * c) for _ in range(T): h, c, t = LI() x = (h + c) / 2 if t <= x: print(2) continue y = (h - c) / (2*t - h - c) z = int(y) if z % 2: tmp = f(z) tmp2 = f(z+2) if (z + 2) * tmp <= z * tmp2: if tmp / z == abs(x - t): print(min(z, 2)) elif tmp / z < abs(x - t): print(z) else: print(2) else: if tmp2 / (z + 2)== abs(x - t): print(min(z+2, 2)) elif tmp2 / (z + 2) < abs(x - t): print(z+2) else: print(2) else: tmp = f(z-1) tmp2 = f(z+1) if (z + 1) * tmp <= (z - 1) * tmp2: if tmp / (z - 1) == abs(x - t): print(min(z-1, 2)) elif tmp / (z - 1) < abs(x - t): print(z-1) else: print(2) else: if tmp2 / (z + 1) == abs(x - t): print(min(z+1, 2)) elif tmp2 / (z + 1) < abs(x - t): print(z+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
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.PrintWriter; import java.util.Locale; import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { try (BufferedInputStream in = new BufferedInputStream(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) { Scanner sc = new Scanner(in).useLocale(Locale.US); int T = sc.nextInt(); for (int t = 0; t < T; t++) { int h = sc.nextInt(); int c = sc.nextInt(); int tar = sc.nextInt(); int a = 0; for (int step = 1_000_000; step >= 1; step /= 2) { while (true) { if (a + step > 1_000_000) break; long[] x = solve(a + step, h, c); if (x[0] <= tar * x[1]) break; a += step; } } if (tar <= (h + c) / 2.0) { out.println(2); } else { long[] x1 = solve(a, h, c); long[] x2 = solve(a + 1, h, c); boolean d = Math.abs(x1[0] - tar * x1[1]) * x2[1] <= Math.abs(x2[0] - tar * x2[1]) * x1[1]; out.println(d ? 2 * a + 1 : 2 * a + 3); } } } } private static long[] solve(long cnt, long h, long c) { long a = h + cnt * (h + c); long b = 2 * cnt + 1; return new long[]{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
import sys input=sys.stdin.buffer.readline 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) x=nHot den1=2*x-1;den2=2*x+1 num1=abs(x*h+(x-1)*c-t*den1);num2=abs((x+1)*h+x*c-t*den2) if num1*den2<=num2*den1: print(2*x-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
ans = [] for _ in range(int(input())): h, c, t = map(int, input().split()) if h + c - 2 * t >= 0: ans.append(2) continue a1 = int((t - c) / (2 * t - h - c)) b1 = a1 - 1 a2 = int((t - c) / (2 * t - h - c)) + 1 b2 = a2 - 1 dt1 = abs(t * (a1 + b1) - (a1 * h + b1 * c)) * (a2 + b2) dt2 = abs(t * (a2 + b2) - (a2 * h + b2 * c)) * (a1 + b1) if dt1 <= dt2: ans.append(a1 + b1) else: ans.append(a2 + b2) print('\n'.join(map(str, 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
# cook your dish here from sys import stdin, stdout import math from itertools import permutations, combinations from collections import defaultdict from bisect import bisect_left from bisect import bisect_right def L(): return list(map(int, stdin.readline().split())) def In(): return map(int, stdin.readline().split()) def I(): return int(stdin.readline()) def seive(): l=100002 c=[1,1]+[0]*l for i in range(2,l): if 1-c[i]: for j in range(i*i,l,i):c[j]=1 def binarySearch (arr, l, r, x): if r >= l: mid = l + (r - l) // 2 if arr[mid] == x: return mid elif arr[mid] > x: return binarySearch(arr, l, mid-1, x) else: return binarySearch(arr, mid + 1, r, x) else: return -1 P = 1000000007 def main(): for _ in range(I()): h, c, t = In() if (h+c)/2>=t: print(2) else: a = (h-t)//(2*t-h- c) 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) if __name__ == '__main__': 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
from sys import stdin from decimal import * inp = lambda: stdin.readline().strip() t = int(inp()) for _ in range(t): h, c, needed = [int(x) for x in inp().split()] s = 10 ** 6 + 1 l = 0 x = 0 prevWell = -1 if needed == h: print(1) elif needed <= (h + c) // 2: print(2) else: while True: x = (s + l) // 2 well = (h * x + c * (x - 1)) / (2 * x - 1) if well == needed: print(x+x-1) break if well == prevWell: well = Decimal(h * x + c * (x - 1)) / Decimal(2 * x - 1) prevWell = Decimal(h * (x + 1) + c * (x)) / Decimal(x + 1 + x) otherWell = Decimal(h * (x - 1) + c * (x-2)) / Decimal(x + x - 3) if abs(prevWell - needed) < abs(well - needed): if abs(prevWell - needed) < abs(otherWell - needed): print(x + 1 + x) else: print(x+x-3) else: if abs(well - needed) < abs(otherWell - needed): print(x + x - 1) else: print(x+x-3) break if well < needed: s = x elif well > needed: l = x prevWell = well
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 import sys input = sys.stdin.readline for _ in range(int(input())): h, c, t = map(int, input().split()) e = 1000001 x = 1 if t > (c+h)/2: a = t - (c+h)/2 x = math.ceil(((h-c)/(2*a) + 1)/2) q = x - 1 if(abs(t - (c+h)/2 - (h-c)/(4*q-2)) <= abs(t - (c+h)/2 - (h-c)/(4*x-2))): print(2*q - 1) else: print(2*x - 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
T, = map(int, input().split()) def f(h, c, n): return (h*(n+1)+c*n)/(2*n+1) def B(h, c, n): return 2*n+1 def C(h, c, n): return (h*(n+1)+c*n) def H(h, c, t, n): return 2*t*B(h,c,n)*B(h,c,n+1)-C(h,c,n+1)*B(h,c,n)-C(h,c,n)*B(h,c,n+1) >=0 for _ in range(T): h, c, t, = map(int, input().split()) mx = 10**18 if 2*t <= h+c: print(2) else: n = (t-h)//(h+c-2*t) if H(h,c,t,n): print(2*n+1) else: print(2*(n+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
import math from decimal import * for _ in range(int(input())): h,c,t = map(int,input().split()) if t <= (h+c)/2: print(2) else: n = math.ceil((h-c)/((2*t)-(h+c))) # print("before:",n) if n&1: print(n) else: half = n//2 getcontext().prec = 1000 a1 = Decimal((half*h)+((half-1)*c))/Decimal(n-1) a2 = Decimal(((half+1)*h)+(half*c))/Decimal(n+1) a1 = abs(a1-t) a2 = abs(a2-t) # print(a1,a2) if a1 <= a2: print(n-1) else: print(n+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 math from decimal import * t = int(sys.stdin.readline().strip()) for i in range(t): h, c, t = list(map(int,sys.stdin.readline().strip().split(' '))) mid = Decimal(c + (h - c)/2) if h == t: print(1) elif t <= mid: print(2) else: b = int((h - mid)/(t-mid)) # print(b) if b % 2 == 0: low = b - 1 high = b + 1 else: low = b high = b + 2 # print(low, high) res = None # u = Decimal((h - mid)/low) # v = Decimal((h - mid)/high) # print(u, v) u = abs((mid + (h - mid)/low) - t) v = abs((mid + (h - mid)/high) - t) # print(Decimal(u), Decimal(v)) if u <= v: res = low else: res = high print(res)
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) jj=int(abs((c-h)//(2*dif))) if(jj%2==0): jj+=1 if(dif-(h-c)/(2*jj)>=abs(dif-(h-c)/(2*(jj-2)))): print(jj-2) else: print(jj)
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 import bisect import os from collections import Counter import bisect from collections import defaultdict import math import random import heapq as hq from math import sqrt import sys from functools import reduce, cmp_to_key from collections import deque import threading from itertools import combinations from io import BytesIO, IOBase from itertools import accumulate # sys.setrecursionlimit(200000) # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def tinput(): return input().split() def rinput(): return map(int, tinput()) def rlinput(): return list(rinput()) mod = int(1e9)+7 def factors(n): return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))) # ---------------------------------------------------- # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') t = 1 # t = iinput() for _ in range(iinput()): h, c, t = rinput() mindiff = abs(t-h) mincups = 1 if abs(t-(h+c)/2.0) < mindiff: mindiff = abs(t-(h+c)/2.0) mincups = 2 # for i in range(1,int(1e6)+1): # avg = float(h*(i+1)+c*i)/float(2*i+1) # # if i == 0: # # print(avg, t, i) # if abs(t-avg) < mindiff: # mindiff = abs(t-avg) # mincups = 2*i+1 # elif abs(t-avg) == mindiff: # break # elif abs(t-avg)>t: # break if 2*t-h-c != 0: i = float(h-t)/float(2*t-h-c) i = abs(i) i1 = math.ceil(i) i2 = math.floor(i) newx = t newx -= Fraction(h*i2+h+c*i2,2*i2+1) newx = abs(newx) if newx < mindiff: mincups = 2*i2+1 mindiff = newx elif newx==mindiff: mincups = min(mincups,2*i2+1) newx = t newx -= Fraction(h*i1+h+c*i1,2*i1+1) newx = abs(newx) if newx < mindiff: mincups = 2*i1+1 mindiff = newx elif newx==mindiff: mincups = min(mincups,2*i1+1) print(mincups) # print(i,i) # print(mindiff) # print(mincups)
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 fractions import Fraction def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 10 MOD = 10 ** 9 + 7 EPS = 10 ** -10 def bisearch_max(mn, mx, func): ok = mn ng = mx while ok+1 < ng: mid = (ok+ng) // 2 if func(mid): ok = mid else: ng = mid return ok # m*2+1回カップを入れた時、温度がt以上かどうか def check(m): # 式変形で除算を避ける res = (h*(m+1) + c*m) return res >= t*(m*2+1) for _ in range(INT()): h, c, t = MAP() ev = (h+c) / 2 res = bisearch_max(0, 10**10, check) if res == 10**10-1: print(2) else: mn = INF ans = -1 for m in range(res, res+2): # 分数クラスで小数を避ける res = Fraction(h*(m+1)+c*m, m*2+1) diff = abs(res - t) if diff < mn: mn = diff ans = m*2 + 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
import sys from decimal import Decimal def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 10 MOD = 10 ** 9 + 7 EPS = 10 ** -10 def bisearch_max(mn, mx, func): ok = mn ng = mx while ok+1 < ng: mid = (ok+ng) // 2 if func(mid): ok = mid else: ng = mid return ok # m*2+1回カップを入れた時、温度がt以上かどうか def check(m): res = (h*(m+1) + c*m) / (m*2+1) return res >= t for _ in range(INT()): h, c, t = map(Decimal, input().split()) ev = (h+c) / 2 res = bisearch_max(0, 10**6, check) if res == 10**6-1: print(2) else: mn = INF ans = -1 for m in range(res, res+2): res = (h*(m+1) + c*m) / (m*2+1) diff = abs(res - t) if diff < mn: mn = diff ans = m*2 + 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
for i in range(int(input())): h,c,t=map(int,input().split()) if (h+c)/2>=t: print(2) else: a = (h-t)//(2*t-h- c) b = a+1 print(2*a + 1 if 2*t*(2*a+1)*(2*b+1) >= (2*b+1)*((a+1)*h+a*c)+(2*a+1)*((b+1)*h+b*c) else 2 * b + 1)
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 cs; cin >> cs; while (cs--) { double h, c, t; cin >> h >> c >> t; if (t >= h) cout << 1 << endl; else if (t <= (c + h) / 2) cout << 2 << endl; else { long long l = 1, r = 1e9, val; while (l < r) { long long mid = (l + r) / 2; val = 2 * mid + 1; double avg = (double)((val + 1) / 2) * h + (double)(val / 2) * c; avg /= val; if (avg < t) r = mid - 1; else l = mid + 1; } double mn = 1e9; int ans = val; for (int i = max(1LL, val - 100); i < val + 100; i += 2) { double avg = (double)((i + 1) / 2) * h + (double)(i / 2) * c; avg /= i; avg = fabs(avg - t); if (avg < mn) { mn = avg; 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
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target("avx2") long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } using namespace std; const int mod = 998244353; const int mxn = 1e6 + 7; const int inf = 1e9; int _, n, m, t, k, u, v, w, ans, cnt, ok, lim, len, tmp, last, nx; struct node { int u, v, nx, w; } e[mxn]; string str; long double calc(long double x) { return abs((x * n + (x - 1) * m) / (x * 2.0 - 1) - k); } void solve() { int l = 1, r = mxn; while (l < r) { int ml = l + (r - l) / 3; int mr = r - (r - l) / 3; long double ls = calc(ml), rs = calc(mr); if (ls > rs) l = ml + 1; else r = mr - 1; } cout << l * 2 - 1 << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; for (cin >> t; t; t--) { cin >> n >> m >> k; if (n <= k) cout << 1 << endl; else if ((n + m) / 2 >= k) cout << 2 << endl; else 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
from math import floor from sys import stdin input = stdin.readline def compare_answer(hot, cold, target, moves, ans): min_temperature = (ans // 2) * (hot + cold) + (ans % 2) * hot min_error = abs(min_temperature - ans * target) * moves temperature = (moves // 2) * (hot + cold) + (moves % 2) * hot error = abs(temperature - moves * target) * ans if error < min_error: ans = moves #print(f'moves {moves} error {error} ans {ans} min_error {min_error}' ) return ans def main(): test = int(input()) for _ in range(test): hot, cold, target = map(int, input().split()) avg = (hot + cold) / 2 if target >= hot: ans = 1 elif avg >= target: ans = 2 else: moves = floor((hot - avg) / (target - avg)) if moves % 2 == 0: moves -= 1 ans = 2 ans = compare_answer(hot, cold, target, moves, ans) ans = compare_answer(hot, cold, target, moves + 2, ans) print(ans) if __name__ == "__main__": 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
from sys import stdin input=lambda : stdin.readline().strip() char = [chr(i) for i in range(97,123)] CHAR = [chr(i) for i in range(65,91)] mp = lambda:list(map(int,input().split())) INT = lambda:int(input()) rn = lambda:range(INT()) mod = 10000000007 from math import ceil,sqrt,factorial,gcd for _ in rn(): h,c,t = mp() x = 1 if 2*t-h-c <= 0: print(2) else: x = max(1,(t-c)//(2*t-h-c)) y = x+1 val1 = (x*h+(x-1)*c) val2 = (y*h+(y-1)*c) m1 = abs(t*(2*x-1)-val1)/(2*x-1) m2 = abs(t*(2*y-1)-val2)/(2*y-1) if m1 <= m2 : print(2*x-1) else: print(2*y-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
# cook your dish here import math def bs(H, C, res): lo = 0 hi = 10**12 ans = 0 while(lo<=hi): mid = (lo + hi)//2 if (H*mid + ((mid-1)*C)) >= res*((2*mid) - 1): lo = mid + 1 ans = mid else: hi = mid - 1 return ans def main(): for q in range(int(input())): H,C,T = map(int,input().split()) avg = (H+C)/2 if T <= avg: print('2') else: ans = bs(H,C,T) ab1 = abs(T*((2*ans)-1) - H*ans - C*(ans-1)) ab2 = abs(T*((2*ans)+1) - H*(ans+1) - C*(ans)) ab1 /= (2*ans - 1) ab2 /= (2*ans + 1) if ab1<=ab2: print(((2*ans)-1)) else: print(((2*ans)+1)) if __name__ == "__main__": 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
t = int(input()) for test in range(t): h, c, t = map(int,input().split(" ")) t-=c h-=c if t<=h/2: print(2) else: res=int(h/(2*t-h)/2) if(abs(t-(res+1)*h/(2*res+1))>abs(t-(res+2)*h/(2*res+3))): print(2*res+3) else: print(2*res+1) """ So, what happens is: 1 1/2 2/3 = 1/2+1/6 -> 3 1/2 3/5 = 1/2+1/10 -> 5 1/2 4/7 = 1/2+1/14 ->7 1/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
input=__import__('sys').stdin.readline from decimal import Decimal import decimal decimal.getcontext().prec = 20 def cal1(no,h,c): ans = Decimal((h+c)*no + h)/Decimal(2*no+1) return ans for _ in range(int(input())): h,c,t = map(int,input().split()) a1=(h+c)/2 l=1 r=1000000 hh=0 if t<=(h+c)/2: print(2) continue while l<=r: mid = l +(r-l)//2 tmp= ((h+c)*mid + h)/(2*mid +1) if t<tmp: l=mid+1 else: r=mid-1 hh+=1 if hh>300: break a3=1000000000000 ind3=-1 # print(2*l+1) for i in range(l,max(-1,l-4),-1): tmp = cal1(i,h,c) if abs(t-tmp)<=abs(t-a3): a3=tmp ind3=i for i in range(l,l+4,1): tmp = cal1(i,h,c) if abs(t-tmp)<abs(t-a3): a3=tmp ind3=i print(2*ind3+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 getClosest(h, c, st, en, t): if abs(val(h, c, st, t)/(2*st-1)) <= abs(val(h, c, en, t)/(2*en-1)): return st else: return en def val(h, c, x, t): return x * h + (x - 1) * c - (2 * x - 1) * t def findClosest(h, c, st, en, t): # Corner cases if val(h, c, st, t) <= 0: return st if val(h, c, en, t) >= 0: return en i = st j = en mid = 0 while i < j: mid = (i + j) // 2 if val(h, c, mid, t) == 0: return mid if val(h, c, mid, t) < 0: if mid > st and val(h, c, mid - 1, t) > 0: return getClosest(h, c, mid - 1, mid, t) j = mid else: if mid < en and val(h, c, mid + 1, t) < 0: return getClosest(h, c, mid, mid + 1, t) i = mid + 1 return mid for _ in range(int(input())): h, c, t = map(int, input().split()) # n = int(input) # a = list(map(int, input().split())) if 2 * t <= (h + c): print(2) elif t == h: print(1) else: x = 1 while x * h + (x - 1) * c > (2 * x - 1) * t: x = x << 1 st = x >> 1 st = st - 1 en = x+2 cx = findClosest(h, c, st, en, t) print(2*cx-1)
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 double pi = acos(-1.0); template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } template <typename T> inline void write(T x) { int i = 20; char buf[21]; buf[20] = '\n'; do { buf[--i] = x % 10 + '0'; x /= 10; } while (x); do { putchar(buf[i]); } while (buf[i++] != '\n'); } template <typename T> inline T readInt() { T n = 0, s = 1; char p = getchar(); if (p == '-') s = -1; while ((p < '0' || p > '9') && p != EOF && p != '-') p = getchar(); if (p == '-') s = -1, p = getchar(); while (p >= '0' && p <= '9') { n = (n << 3) + (n << 1) + (p - '0'); p = getchar(); } return n * s; } long long int uniquePaths_math(long long int A, long long int B) { return A * B + 1; } int main() { int tc; scanf("%d", &tc); while (tc--) { long int h, c, t, ans; cin >> h >> c >> t; if (h == t) { ans = 1; } else if (t <= ((h + c) / 2)) { ans = 2; } else { double x = (double)(t - c) / (double)(2 * t - h - c); double x1 = ceil(x), x2 = floor(x), cnt, diff, tx1, tx2; tx1 = (double)(h * x1 + (x1 - 1) * c) / (2 * x1 - 1); tx2 = (double)(h * x2 + (x2 - 1) * c) / (2 * x2 - 1); diff = fabs(tx2 - t); cnt = 2 * x2 - 1; if (diff > fabs(tx1 - t)) { cnt = 2 * x1 - 1; } ans = (int)cnt; } 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
#include <bits/stdc++.h> using namespace std; double h, c, t; double get(int x) { return fabs(t - (1.0 * x * h + 1.0 * (x - 1) * c) / (1.0 * (x + x - 1))); } int main() { int T; cin >> T; while (T--) { cin >> h >> c >> t; if (h <= t) puts("1"); else { double avg = (h + c) / 2.0; if (avg >= t) puts("2"); else { int l = 1, r = (1 << 29), mid = r, rmid = r; while (l + 1 < r) { mid = (l + r) / 2; rmid = (mid + r) / 2; if (get(mid) > get(rmid)) l = mid; else r = rmid; } if (get(l) <= get(r)) printf("%d\n", l + l - 1); else printf("%d\n", r + r - 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
T=int(input()) for t in range(T): h,c,t=map(int,input().split()) h*=2;c*=2;t*=2 m=(h+c)//2 if t<=m: print(2) else: hc,tc=h-m,t-m tms=hc//tc apr=[2434841,1] for i in range(tms-5,tms+5): if i%2 and abs(tc-hc/i)<apr[0]: apr=[abs(tc-hc/i),i] print(apr[1])
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 h, c, t; double ans(double); int binarysearch(); double ghadr(double); bool check(); int main() { int T; cin >> T; while (T--) { cin >> h >> c >> t; if (t <= double(c + h) / 2) { cout << 2 << endl; continue; } if (h == t) { cout << 1 << endl; continue; } if (check()) continue; int res = binarysearch(); if (ghadr(t - ans(res)) <= ghadr(t - ans(res + 1))) cout << 2 * res - 1 << endl; else cout << 2 * res + 1 << endl; } return 0; } double ghadr(double a) { return max(a, -1 * a); } double ans(double a) { double res = c + a / (2 * a - 1) * (h - c); return res; } int binarysearch() { int left = 2, right = 1e9; while (left <= right) { int mid = left + (right - left) / 2; if (ans(mid) >= t && ans(mid + 1) <= t) { return mid; } if (ans(mid) >= t) left = mid + 1; else right = mid - 1; } return 0; } bool check() { if (t >= ans(2)) { if (ghadr(t - ans(2)) < h - t) cout << 3 << endl; else cout << 1 << endl; return true; } return false; }
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.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf1359c_2 { public static void main(String[] args) throws IOException { int q = ri(); while(q --> 0) { long h = rnl(), c = nl(), t = nl(); if(t <= (h + c) / 2) { prln(2); } else { long k = (t - h) / (h + c - 2 * t); long a = abs((2 * k + 3) * (2 * k + 1) * t - (2 * k + 3) * (k * (h + c) + h)); long b = abs((2 * k + 3) * (2 * k + 1) * t - (2 * k + 1) * ((k + 1) * (h + c) + h)); // double a = abs(t - (double)(k * (h + c) + h) / (2 * k + 1)); // double b = abs(t - (double)((k + 1) * (h + c) + h) / (2 * k + 3)); prln(a <= b ? 2 * k + 1 : 2 * k + 3); } } close(); } static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random rand = new Random(); // references // IBIG = 1e9 + 7 // IRAND ~= 3e8 // IMAX ~= 2e10 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IRAND = 327859546; static final int IMAX = 2147483647; static final int IMIN = -2147483648; static final long LMAX = 9223372036854775807L; static final long LMIN = -9223372036854775808L; // util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int floori(double d) {return (int)d;} static int ceili(double d) {return (int)ceil(d);} static long floorl(double d) {return (long)d;} static long ceill(double d) {return (long)ceil(d);} static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static <T> void shuffle(T[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); T swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static void rsort(double[] a) {shuffle(a); sort(a);} static int randInt(int min, int max) {return rand.nextInt(max - min + 1) + min;} // input static void r() throws IOException {input = new StringTokenizer(__in.readLine());} static int ri() throws IOException {return Integer.parseInt(__in.readLine());} static long rl() throws IOException {return Long.parseLong(__in.readLine());} static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;} static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;} static char[] rcha() throws IOException {return __in.readLine().toCharArray();} static String rline() throws IOException {return __in.readLine();} static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());} static int ni() {return Integer.parseInt(input.nextToken());} static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());} static long nl() {return Long.parseLong(input.nextToken());} // output static void pr(int i) {__out.print(i);} static void prln(int i) {__out.println(i);} static void pr(long l) {__out.print(l);} static void prln(long l) {__out.println(l);} static void pr(double d) {__out.print(d);} static void prln(double d) {__out.println(d);} static void pr(char c) {__out.print(c);} static void prln(char c) {__out.println(c);} static void pr(char[] s) {__out.print(new String(s));} static void prln(char[] s) {__out.println(new String(s));} static void pr(String s) {__out.print(s);} static void prln(String s) {__out.println(s);} static void pr(Object o) {__out.print(o);} static void prln(Object o) {__out.println(o);} static void prln() {__out.println();} static void pryes() {__out.println("yes");} static void pry() {__out.println("Yes");} static void prY() {__out.println("YES");} static void prno() {__out.println("no");} static void prn() {__out.println("No");} static void prN() {__out.println("NO");} static void pryesno(boolean b) {__out.println(b ? "yes" : "no");}; static void pryn(boolean b) {__out.println(b ? "Yes" : "No");} static void prYN(boolean b) {__out.println(b ? "YES" : "NO");} static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next());} static void h() {__out.println("hlfd");} static void flush() {__out.flush();} static void close() {__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 solve(h, c, t): if t == h: return 1 if 2 * t <= h + c: return 2 cands = [] cands.append((h + c, 2, 2)) bx = h - t by = 2 * t - h - c b = bx // by for d in range(2): bb = b + d aa = bb + 1 cands.append(( aa * h + bb * c, aa + bb, aa + bb )) # for _ in cands: # print(_) ans, ansx, ansy = float('inf'), float('inf'), 1 for ox, oy, o in cands: if ansx * oy > ansy * abs(ox - oy * t): ans, ansx, ansy = o, abs(ox - oy * t), oy return ans tests = int(input()) for _ in range(tests): h, c, t = map(int, input().split()) print(solve(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 sys from math import ceil, floor from functools import cmp_to_key input = sys.stdin.readline def main(): tc = int(input()) for _ in range(tc): h, c, t = map(int, input().split()) if h + c == 2 * t: print(2) else: x = max(0, (t - h) // (h + c - 2 * t)) cands = [ ((h + c) - 2 * t, 2), (x * (h + c) + h - (2 * x + 1) * t, 2 * x + 1), ((x + 1) * (h + c) + h - (2 * x + 3) * t, 2 * x + 3) ] cands = map(lambda t: (abs(t[0]), t[1]), cands) cmp = lambda a, b: a[0] * b[1] - a[1] * b[0] print(min(cands, key = cmp_to_key(cmp))[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
from math import ceil for _ in range(int(input())): h,c,t=map(int,input().split()) if t<=(h+c)/2:print(2) else: 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
import math tt = int(input()) for test in range(tt): h,c,t = map(int,input().split()) if t<=(h+c)/2: print(2) else: k = (h-t)//(2*t-h-c) # if k hot +1 hot and k cold cups 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: # if (k+1) hot +1 hot and (k+1) col cups print(2*k+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
import math test = int(input()) for _ in range(test): h,c,t = map(int, input().split()) if t==h: print(1) elif t <= (h+c)//2: print(2) else: x = (t-h)/((h+c)-2*t) ceil = math.ceil(x) floor = math.floor(x) if abs(((h+c)*ceil+h)-t*(2*ceil+1))*(2*floor+1) < abs(((h+c)*floor+h)-t*(2*floor+1))*(2*ceil+1): print(2*ceil+1) else: print(2*floor+1)
PYTHON3