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
from math import floor # inp = open( 'file.txt' ).readline inp = input getans = lambda a , b , x , y : ((( a*x ) + (b*y)) , ( x+y )) for _ in range( int( inp() ) ): h , c , t = map( int , inp().strip().split(" ") ) if h == t: print(1) elif (h+c)/2 >= t: print(2) else: # print(h , c , t , ( c - h ) , h+c - (2*t) ) x = floor(( c - t )/( h+c - (2*t) )) if x == 0: x = 1 y = x-1 # print(x , y , "in") ans1 , d1 = getans( h,c,x,y ) ans2 , d2 = getans( h,c,x+1,y+1 ) # if x-1 > 1: # ans3 , d3 = getans( h,c,x-1,y-1 ) # else: # ans3 , d3 = 99999 , 99999 ab1 = abs( ans1 - (t*d1) )*d2 ab2 = abs( ans2 - (t*d2) )*d1 # ab3 = abs( ans3 - (t*d3) )*d3 # print(ab1 , ab2 , x) if ab1 <= ab2: print( x+y ) else: print( x+y+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 math as ma t=int(input()) for _ in range(t): h,c,t=list(map(int,input().split())) if (h+c)//2==t: print(2) else: a=[[abs(h-t),1]] a.append([abs((h+c)/2-t),2]) if (h+c)/2<t: b=t-(h+c)/2 e=(h-c)/(2*b) d=int(e) f=ma.ceil(e) if d==f: f+=1 if d>0 and d%2!=0: a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d+=2 a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d+=2 a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d+=2 a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d-=6 a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d-=2 if d>0: a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d-=2 if d>0: a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) if f>0 and f%2!=0: d=f a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d+=2 a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d+=2 a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d+=2 a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d-=6 a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d-=2 if d>0: a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) d-=2 if d>0: a.append([abs(t-(h+c)/2-(h-c)/(2*d)),d]) print(int(min(a)[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() { long long n; cin >> n; double h, c, t; while (n--) { long long ans = 0; cin >> h >> c >> t; if (t <= ((h + c) / 2.0)) cout << "2\n"; else { long long k = ((h - t) / ((2.0 * t) - h - c)); double ck1 = abs(((double)k * (h + c) + h) - t * (2.0 * (double)k + 1.0)) * (2.0 * (double)k + 3.0); double ck2 = abs((((double)k + 1) * (h + c) + h) - t * (2.0 * (double)k + 3.0)) * (2.0 * (double)k + 1.0); if (ck1 <= ck2) { cout << 2 * k + 1 << "\n"; } else { cout << 2 * (k + 1) + 1 << 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
# Contest No.: Edu 88 # Problem No.: C # Solver: JEMINI # Date: 20200528 import sys import heapq def gcd(a, b): if a < b: a, b = b, a if b == 0: return a return gcd(b, a % b) def main(): a = int(input()) for _ in range(a): h, c, t = map(int, sys.stdin.readline().split()) if t >= h: print(1) continue if t <= (h + c) / 2: print(2) continue n = 0 while (n * (c + h) + h) / (2 * n + 1) > t: temp = 0 cnt = 1 while ((n + 2 * cnt) * (c + h) + h) / (2 * (n + 2 * cnt) + 1) > t: cnt *= 2 temp += 1 n += cnt #print(n, abs((n * (c + h) + h) / (2 * n + 1) - t), abs(((n - 1) * (c + h) + h) / (2 * n - 1) - t)) tempH = int(abs((n * (c + h) + h) - t * (2 * n + 1))) tempL = 2 * n + 1 bigVal = (tempH // gcd(tempH, tempL)) / (tempL // gcd(tempH, tempL)) tempH = int(abs(((n - 1) * (c + h) + h) - t * (2 * n - 1))) tempL = 2 * n - 1 lowVal = (tempH // gcd(tempH, tempL)) / (tempL // gcd(tempH, tempL)) if bigVal >= lowVal: print(2 * n - 1) else: print(2 * n + 1) return 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigDecimal; import java.util.StringTokenizer; public class MixingWater { static long INF=1000000000L; public static void main(String[] args) throws IOException { int testNum = Reader.nextInt(); StringBuffer stringBuffer = new StringBuffer(); for (int rr = 0; rr < testNum; rr++) { int h = Reader.nextInt(); int c = Reader.nextInt(); int t = Reader.nextInt(); BigDecimal INF = new BigDecimal("10000000000"); BigDecimal bigDecimal = new BigDecimal(0); long ans = 0; long k = (long) ((t - h) * 1.0 / (h + c - 2 * t)) + 1; double diff = 2 * t * (4 * k * k - 1) - (2 * k - 1) * (h + k * h + k * c) - (2 * k + 1) * (k * h + k * c - c); if (t <= (h + c) / 2) { ans = 2; } else if (t >= h) { ans = 1; } else if (diff < 0) { ans = 2 * k + 1; } else { ans = 2 * k - 1; } stringBuffer.append(ans + "\n"); } System.out.println(stringBuffer.toString()); } static class Reader { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer tokenizer = new StringTokenizer(""); static String nextLine() throws IOException {// 读取下一行字符串 return reader.readLine(); } static String next() throws IOException {// 读取下一个字符串 while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException {// 读取下一个int型数值 return Integer.parseInt(next()); } static long nextLong() throws IOException {// 读取下一个int型数值 return Long.parseLong(next()); } static double nextDouble() throws IOException {// 读取下一个double型数值 return Double.parseDouble(next()); } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import os import sys from io import BytesIO, IOBase # 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") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ from math import factorial from collections import Counter, defaultdict, deque from heapq import heapify, heappop, heappush def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) mod = 998244353 INF = float('inf') # ------------------------------ def main(): for _ in range(N()): h, c, t = RL() avg = (h+c)/2 if t<=avg: print(2) else: num = (t-h)//(c+h-2*t) # print(num) r1 = (num+1)*h + num*c r2 = (num+2)*h + (num+1)*c if abs(r1-t*(2*num+1))/(2*num+1)<=(abs(r2-t*(2*num+3)))/(2*num+3): print(2*num+1) else: print(2*num+3) 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
import math 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 T; int h, c, t; int main() { scanf("%d", &T); while (T--) { scanf("%d%d%d", &h, &c, &t); if (t * 2 <= (h + c)) printf("%d\n", 2); else { long long lef = (h - t) / (2 * t - (h + c)); double a1 = (1.0 * h * (lef + 1) + c * lef) / (2 * lef + 1); lef++; double a2 = (1.0 * h * (lef + 1) + c * lef) / (2 * lef + 1); if (abs(a1 - t) <= abs(a2 - t)) printf("%lld\n", 2 * (lef - 1) + 1); else printf("%lld\n", 2 * lef + 1); } } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
def f(h,c,t): if t<= (c+h)/2: return 2 if t>=h: return 1 x = (t-c)//(2*t-h-c) if (4*x*x+2*x-1)*h+(4*x*x-2*x-1)*c <= 2*(4*x*x-1)*t: return 2*x-1 else: return 2*x+1 t = int(input()) for i in range(t): [h,c,t] = input().split(' ') print(f(int(h),int(c),int(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 t_on_move(move): return (move * h + (move - 1) * c) / (2 * move - 1) for _ in range(int(input())): h, c, t = list(map(int, input().split())) even_val = (h + c) // 2 l = 0 r = 2000000000 while r - l > 1: m = (l + r) // 2 m_val = t_on_move(m) if m_val > t: l = m else: r = m delta = abs(t - (h + c) / 2) ans = 2 if l > 0: if (r * h + (r - 1) * c) * (2 * l - 1) - t * (2 * r - 1) * (2 * l - 1) > - (l * h + (l - 1) * c) * (2 * r - 1) + t * (2 * r - 1) * (2 * l - 1): if (delta > abs(t - t_on_move(r))): ans = 2 * r - 1 else: if (delta > abs(t - t_on_move(l))): ans = 2 * l - 1 elif delta > abs(t - t_on_move(r)): ans = 2 * r - 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 math import floor t = int(input()) def near(i, h, c): return abs(((i+1.0)*h + i*c)/(2*i+1.0)) for _ in range(t): h, c, t = (int(x) for x in input().split()) h = h - t c = c - t if h == 0 or h == c: print(1) elif h + c >= 0: print(2) else: cand = -(h*1.0)/(h+c) i1 = floor(cand) i2 = i1+1 if near(i1, h, c) <= near(i2, h, c): print(2*i1+1) else: print(2*i2+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 collections import deque,defaultdict printn = lambda x: print(x,end='') inn = lambda : int(input()) inl = lambda: list(map(int, input().split())) inm = lambda: map(int, input().split()) ins = lambda : input().strip() DBG = True # and False BIG = 10**18 R = 10**9 + 7 def ddprint(x): if DBG: print(x) def foo(h,c,n): return (n*h+(n-1)*c)/(2*n-1) def bar(h,c,t,n): return (n*h+(n-1)*c)==t*(2*n-1) ttt = inn() for tt in range(ttt): h,c,t = inm() if 5*h+c<=6*t: print(1) elif 2*t<=(h+c): print(2) else: mn = 1 mx = 10**16 found = False while mx-mn>1: mid = (mn+mx)//2 if bar(h,c,t,mid): found = True break elif foo(h,c,mid)<t: mx = mid else: mn = mid if found: #ddprint(f"found") print(2*mid-1) continue #tn = foo(h,c,mn) #tx = foo(h,c,mx) #if abs(tn-t)>abs(tx-t): x = 2*(2*mn-1)*(2*mx-1)*t y = (2*mx-1)*(mn*h+(mn-1)*c) + (2*mn-1)*(mx*h+(mx-1)*c) #ddprint(f"mn {mn} mx {mx} x {x} y {y}") if x<y: print(2*mx-1) else: print(2*mn-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 int N = 100010, M = 1010; int T; int h, c, t; int main() { cin >> T; while (T--) { cin >> h >> c >> t; if ((double)t <= (double)(h + c) / 2.0) puts("2"); else { long long k = (long long)(h - t) / (2LL * t - h - c); long long v1 = abs(((long long)(k + 1LL) * h + k * c) - (long long)(2LL * k + 1LL) * t) * (long long)(2LL * k + 3LL); long long v2 = abs(((long long)(k + 2LL) * h + (k + 1LL) * c) - (long long)(2LL * k + 3LL) * t) * (long long)(2LL * k + 1LL); if (v1 <= v2) cout << 2LL * k + 1LL << '\n'; else cout << 2 * k + 3LL << '\n'; } } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; long long T, h, c, t; signed main() { cin >> T; while (T--) { cin >> h >> c >> t; if (t + t <= h + c) { cout << 2 << endl; continue; } if (h <= t) { cout << 1 << endl; continue; } double ans = (h - t) / (t + t - h - c); cout << ((t + t >= (ans * ((double)c + h) + h) / (2 * ans + 1) + ((ans + 1) * ((double)c + h) + h) / (2 * ans + 3)) ? 2ll * (long long)ans + 1ll : 2ll * (long long)ans + 3ll) << 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
def N(): return int(input()) def NM():return map(int,input().split()) def L():return list(NM()) def LN(n):return [N() for i in range(n)] def LL(n):return [L() for i in range(n)] T=N() def f(): h,c,t=NM() h-=c t-=c if h==t: print(1) return elif t*2<=h: print(2) return lo=0 hi=10**6*2 while hi>=lo: mi=(hi+lo)//2 if sa(mi,t,h)>sa(mi+1,t,h): lo=mi+1 else: hi=mi-1 if sa(lo,t,h)<=sa(lo+1,t,h): print(lo*2+1) else: print((lo+1)*2+1) return def sa(k,t,h): return abs((h*(k+1)-t*(2*k+1))/((2*k+1)*h)) for i in range(T): f()
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 * t = int(input()); for test in range(t): h,c,t = map(int,input().split()); eavg = (h+c)/2; if(t<=eavg): print(2); continue; avg = (h-t)/(2*t-h-c); a1 = max(0,math.ceil(avg)); a2 = max(0,math.floor(avg)); t1 = Decimal((a1+1)*h + a1*c)/(2*a1 + 1); t2 = Decimal((a2+1)*h + a2*c)/(2*a2 + 1); ans = 0; if(abs(t-t2)<=abs(t-t1)): ans = a2; else: ans = a1; print(int(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 math t=int(input()) for w in range(t): h,c,te=(int(i) for i in input().split()) d2=(h+c)/2 if(h<te): print(1) elif(d2>=te): print(2) else: d=pow(10,18) x=(te-c)/((2*te)-h-c) k1=max(1,int(x-10)) k2=int(x+10) for i in range(k1,k2): if abs(te*((2*i)-1)-((h*i+c*(i-1))))/((2*i)-1)<d: d=abs(te*((2*i)-1)-((h*i+c*(i-1))))/((2*i)-1) n=(2*i)-1 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
for ti 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
from math import floor def f(r,x,y,z): temp=((r+1)*x + r*y)/(2*r+1) return abs(temp-z) for _ in range(int(input())): h,c,t=map(int,input().split()) av=(h+c)/2 if av>=t: print(2) continue k=floor(((h-t)/(2*t-(h+c))) + 0.5) temp=abs((((k+1)*h + k*c)/(2*k + 1))-t) temp1=abs((((k+2)*h + (k+1)*c)/(2*k + 3))-t) if temp<=temp1: print(2*k+1) else: 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
#AUTHOR pajenegod import sys, fractions range = xrange inp = [int(x) for x in sys.stdin.read().split()] ii = 0 def rint(): global ii i = ii ii += 1 return inp[i] def rints(n): global ii i = ii ii += n return inp[i:ii] def rintss(n, k): global ii i = ii ii += n * k return [inp[i + j: ii: k] for j in range(k)] F = fractions.Fraction t = rint() for _ in range(t): h,c,t = rints(3) if t == h: print 1 continue if 2 * t <= h + c: print 2 continue closest = abs(t - F(h+c,2)) mugs = 2 a = 0 b = 10**9 while a < b: mid = a + b + 1>> 1 rat = F(h * (mid + 1) + c * mid, 2 * mid + 1) if rat >= t: a = mid else: b = mid - 1 for n in a,a+1: rat = F(h * (n + 1) + c * n, 2 * n + 1) goal = abs(t - rat) if goal < closest: closest = goal mugs = 2 * n + 1 print mugs
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
#include <bits/stdc++.h> using namespace std; int main() { int _; cin >> _; while (_--) { long long h, c, t; cin >> h >> c >> t; if (h + c == 2 * t) { cout << 2 << endl; continue; } else { long long x = 1LL * (c - t) / (h + c - 2 * t); double t1 = 1.0 * (x * h + (x - 1) * c) / (2 * x - 1); double t2 = 1.0 * ((x + 1) * h + x * c) / (2 * x + 1); int y = (abs(t1 - t) > abs(t2 - t) ? (2 * x + 1) : (2 * x - 1)); cout << (y > 0 ? y : 2) << 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
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long inf = 0x3f3f3f3f3f3f3f3f; const int N = 2e5 + 10; int T; long long h, c, t; double cal(long long x) { return abs(1.0 * (h * x + c * (x - 1)) / (2 * x - 1) - 1.0 * t); } int main() { scanf("%d", &T); while (T--) { scanf("%lld%lld%lld", &h, &c, &t); if (t == h) { puts("1"); continue; } if (2 * t <= h + c) { puts("2"); continue; } long long x1 = (t - c) / (2 * t - h - c); long long x2 = x1 + 1; double t1 = cal(x1), t2 = cal(x2); if (t1 - t2 > 0) printf("%lld\n", 2 * x2 - 1); else printf("%lld\n", 2 * x1 - 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
from fractions import Fraction from math import ceil, floor for _ in range(int(input())): h, c, t = map(int, input().split()) if t * 2 <= h + c: print(2) else: # (t - (h + c) / 2) ~~ ((h - c) / 2) / ans c1 = t - Fraction(h + c, 2) c2 = Fraction(h - c, 2) goal = c2 / c1 goal1 = (goal + 1) / 2 ans1 = floor(goal1) * 2 - 1 ans2 = ceil(goal1) * 2 - 1 if c2 / ans1 + c2 / ans2 <= c1 * 2: print(ans1) else: print(ans2)
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 python3 from collections import defaultdict,deque from heapq import heappush, heappop from bisect import bisect_left, bisect_right import sys, random, itertools, math sys.setrecursionlimit(10**5) input = sys.stdin.readline sqrt = math.sqrt def LI(): return list(map(int, input().split())) def LF(): return list(map(float, input().split())) def LI_(): return list(map(lambda x: int(x)-1, input().split())) def II(): return int(input()) def IF(): return float(input()) def S(): return input().rstrip() def LS(): return S().split() def IR(n): return [II() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def FR(n): return [IF() for _ in range(n)] def LFR(n): return [LI() for _ in range(n)] def LIR_(n): return [LI_() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] mod = 1000000007 inf = float('INF') #solve def solve(): def f(m): return c * (m - 1) + h * m ans = [] for i in range(II()): h, c, t = LI() if (h + c) / 2 >= t: ans.append(2) continue l = 10 ** 6 r = 1 while l - r > 1: m = (r + l) // 2 if f(m) < t * (2 * m - 1): l = m else: r = m fl = abs(f(l) - t * (2 * l - 1)) * (2 * r - 1) fr = abs(f(r) - t * (2 * r - 1)) * (2 * l - 1) if fl < fr: ans.append(l * 2 - 1) else: ans.append(r * 2 - 1) for ai in ans: print(ai) return #main if __name__ == '__main__': 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
# -*- coding: utf-8 -*- from math import ceil from fractions import Fraction t=int(input()) for iza in range(t): X=input() X1=X.split() h=int(X1[0]) c=int(X1[1]) t=int(X1[2]) h=h-c t=t-c c=0 if t<=h/2: print(2) else: if t==h: print(1) else: k=ceil((t)/(2*t-h)) dif1=abs(t*(2*k-1)-h*(k)) dif2=abs(t*(2*k-3)-h*(k-1)) if dif1*(2*k-3) < dif2*(2*k-1): print(int(2*k-1)) else: print(int(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 decimal decimal.getcontext().prec = 20 def get_r(r , n1,n2,i): d = abs(r - decimal.Decimal((h * (i // 2 + 1) + c * (i // 2))) / decimal.Decimal(i)) return d t = int(input()) for _ in range(t): h, c , r = map(int, input().rstrip().split(" ")) if h == r: print(1) elif r <= (h+c)/2: print(2) else: d = (h-r)/(2*r - h - c) if d%1: d = ((int(d)+1)*2 - 1)//1 t1 = get_r(r , h, c, d-2) t2 = get_r(r , h, c, d) t3 = get_r(r , h, c, d+2) if t1 == min(t1,t2,t3): print(int(d-2)) elif t2 == min(t1,t2,t3): print(int(d)) elif t3 == min(t1,t2,t3): print(int(d+2)) else: print((int((2*d)+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 decimal def f(h,c,t): if (h+c)/2==t: return 2 n=((h-c)/2)/(t-h/2-c/2) if n<0: return 2 if n%2==1 and n==int(n): return int(n) n=int(n) #print(n) if n%2==0: n1=n-1 n2=n+1 if n==0: n1=n+1 else: n1=n n2=n+2 #print(n1,n2) ll=[abs(t-(h/2+c/2)),abs(t-g(n1,h,c)),abs(t-g(n2,h,c))] #print(ll) if min(ll)==ll[0]: return 2 if min(ll)==ll[1]: return n1 if min(ll)==ll[2]: return n2 def g(n,h,c): #return ((((n+1)/2)*h+((n-1)/2)*c)/n) return decimal.Decimal(((decimal.Decimal((n+1)/2))*h+(decimal.Decimal((n-1)/2))*c)/n) for tc in range(int(input())): al=list(map(int,input().split())) print(f(al[0],al[1],al[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 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) ans = [] for _ in range(tc): h, c, t = map(int, input().split()) if h + c == 2 * t: ans.append(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) ans.append(min(cands, key = cmp)[1]) print('\n'.join(map(str, ans))) main()
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys from fractions import gcd from math import ceil,sqrt,sin,pi,tan from collections import defaultdict read=sys.stdin.buffer.readline import heapq mi=lambda:map(int,read().split()) li=lambda:list(mi()) cin=lambda:int(read()) for _ in range(cin()): h,c,t=mi() if t==h: print(1) continue if t<=(c+h)/2: print(2) continue k=abs(t-h) k=k//abs(h+c-2*t) t1,t2=(k*(h+c)+h),((k+1)*(h+c)+h) if abs(t*(2*k+1)-t1)*(2*k+3)<=abs(t*(2*k+3)-t2)*(2*k+1): print(2*k+1) else: 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
#include <bits/stdc++.h> using namespace std; long long h, c, t; double check(int cnt) { return ((double)cnt * (double)h + ((double)cnt - (double)1) * (double)c) / (2.0 * cnt - 1.0); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tt; cin >> tt; while (tt--) { cin >> h >> c >> t; if (t == h) cout << "1\n"; else if (t <= (c + h) / 2) cout << "2\n"; else { int l = 1, r = 1e8, ans; while (l <= r) { int mid = (l + r) / 2; if (check(mid) >= ((double)t)) { ans = mid; l = mid + 1; } else r = mid - 1; } if (abs(check(ans) - t) <= abs(check(ans + 1) - t)) { cout << 2 * ans - 1 << "\n"; } else cout << 2 * ans + 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
#include <bits/stdc++.h> using namespace std; long long h, c; long double x = 1.0; long double get(long long moves) { long double x = 1.0; return c + ((moves) / (2 * moves - 1.0)) * (h * x - c * x); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, t, m, y, z, temp; cin >> t; while (t--) { cin >> h >> c >> temp; if (2 * temp <= (h + c)) { cout << 2 << '\n'; continue; } long long lo = 1, hi = 1e15, mid; while ((hi - lo) > 1) { mid = (hi + lo) / 2; if (get(mid) > temp * 1.0) { lo = mid; } else { hi = mid; } } if (abs(get(lo) - x * temp) <= abs(get(hi) - x * temp)) { cout << 2 * lo - 1 << '\n'; } else { cout << 2 * hi - 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
#include <bits/stdc++.h> using namespace std; const double pi = 3.14159265358979323846; long long gcd(long long a, long long b) { long long temp; if (a < b) { temp = b; b = a; a = temp; } if (a % b == 0) { return b; } return gcd(b, a % b); } long long modu(long long a, long long b) { if (a < 0) { a = -1 * a; return (b - a % b) % b; } else { return a % b; } } long long lcm(long long a, long long b, long long c, long long d) { long long temp; if (a < b) { temp = b; b = a; a = temp; } if (a % b == 0) { return (c * d) / b; } return lcm(b, a % b, c, d); } long long powers(long long a, long long b) { long long ans = 1; while (b > 0) { if (b % 2 == 1) { ans = ans * a; } b = b / 2; a = a * a; } return ans; } int main() { long long i, j, k, z, t, n, f, ans, ma, mi, a, b, c, d, y, mid, m, g; cin >> t; while (t--) { long double h, c, te, x; cin >> h >> c >> te; long double fuf; fuf = 10000000000; mi = 1; ma = 1000000; mid = (mi + ma) / 2; ans = 0; i = 0; while (mi <= ma && i < 100) { i++; z = mid; x = mid; if (z % 2 == 1) { if (abs((((x / 2 + 0.5) * h + (x / 2 - 0.5) * c) / x) - te) < fuf) { ans = x; fuf = abs((((x / 2 + 0.5) * h + (x / 2 - 0.5) * c) / (x)) - te); } if (abs(((x / 2 + 0.5) * h + (x / 2 - 0.5) * c) / x) < te) { ma = mid; } else if (abs(((x / 2 + 0.5) * h + (x / 2 - 0.5) * c) / x) > te) { mi = mid; } else { break; } mid = (mi + ma) / 2; } else { mid++; } } if (1 > ans - 1500) { y = 1; } else { y = ans - 1500; } for (i = y + 3000; i > y && i >= 1; i--) { x = i; if (i % 2 == 1) { double yele = (int)i / 2; if (abs((((yele + 1) * h + (yele)*c) / (2 * yele + 1)) - te) <= fuf) { ans = i; fuf = abs((((yele + 1) * h + (yele)*c) / (2 * yele + 1)) - te); } } } long double fuf1, fuf2; fuf1 = abs(h - te); fuf2 = abs((h + c) / 2 - te); if (abs((h + c) / 2 - te) < fuf && fuf2 < fuf1) { cout << 2 << endl; } else if (fuf1 <= fuf2 && fuf1 <= fuf) { cout << 1 << endl; } else { cout << ans << endl; } } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math def solve(): T = int(input()) while (T > 0): hct = input().split() h = int(hct[0]) c = int(hct[1]) t = int(hct[2]) if (t <= (h+c)/2): print(2) else: k1 = math.ceil((t-c)/(2*t-h-c)) k2 = math.floor((t-c)/(2*t - h - c)) if (k2 == 0): print(2*k1 - 1) else: le = 2*k1*k2*(2*t-h-c) + 2*k2*(c-t) - k1*(2*t-h-c)+(t-c) rg = 2*k1*k2*(h+c-2*t) + 2*k1*(t-c)-k2*(h+c-2*t)+(c-t) if (le < rg): print(2*k1-1) else: print(2*k2-1) T -= 1 solve()
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys q=int(input()) for i in range(q): h,c,t=[int(j) for j in sys.stdin.readline().split()] if t==h: print(1) elif t<=(h+c)/2: print(2) else: first=(h+c)/t first1=(h+c)/2 second=(h-t)//(2*t-h-c) if second==second+1: if second==0: print(1) else: print(2) else: second1=((h+c)*(second)+h)#/((second)*2+1) second2=((h+c)*(second+1)+h)#/((second+1)*2+1) min1=min(abs(t-(h+c)/2),abs(t*((second)*2+1)-second1)/((second)*2+1),abs(t*((second+1)*2+1)-second2)/((second+1)*2+1),abs(t-h)) if min1==abs(t-h): print(1) elif min1==abs(t-(h+c)/2): print(2) elif min1==abs(t*((second)*2+1)-second1)/((second)*2+1): print(int(second)*2+1) elif min1==abs(t*((second+1)*2+1)-second2)/((second+1)*2+1): print(int(second+1)*2+1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
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
def solve(): h, c, t = map(int, input().split()) if h == t: result = 1 elif t <= (h + c) / 2: result = 2 else: k = (t - c - 1) // (2 * t - h - c) result = 2 * k + 1 result -= 2 if (4 * k * k - 1) * (2 * t - h - c) >= 2 * (h - c) * k else 0 print(result) def main(): for _ in range(int(input())): solve() 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 solve() { double h, c, t; cin >> h >> c >> t; if (t <= (c + h) / 2) return 2; int k = (h - t) / ((2 * t) - h - c); double tk1 = ((k * (h + c)) + h) / ((2 * k) + 1); double tk2 = (((k + 1) * (h + c)) + h) / ((2 * k) + 3); if (abs(t - tk1) <= abs(t - tk2)) return (k * 2) + 1; return ((k + 1) * 2) + 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) cout << solve() << "\n"; return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); static long f(long x, long h , long c) { return x * (h + c) + h; } static long solve(long num1, long num2, long h, long c, long temp) { long x1 = (num1 + 1)/2, y1 = num1 /2; long x2 = (num2 + 1)/2, y2 = num2 /2; long a = Math.abs((x1 * h + y1 * c) - num1 * temp); long b = Math.abs((x2 * h + y2 * c) - num2 * temp); if(a * num2 <= b * num1) { return num1; } return num2; } public static void main(String[] args) throws Exception { int t = sc.nextInt(); while(t-- > 0) { long h = sc.nextInt(), c = sc.nextInt(), temp = sc.nextInt(); long start = 0, end = (long) 1e9, val = 0; while(start <= end) { long mid = (start + end) >> 1; if(f(mid, h , c) >= temp * (2 * mid + 1)) { val = mid; start = mid + 1; }else { end = mid - 1; } } val = 2 * val + 1; val = solve(val, val + 2, h, c, temp); val = solve(2, val, h, c, temp); out.println(val); } out.close(); } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public Long nextLong() throws IOException { return Long.parseLong(next()); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from decimal import * def value(n,h,c,t): getcontext().prec = 100 a = Decimal(n*h + (n-1)*c)/Decimal(2*n - 1) return abs(a-Decimal(t)) t = int(input()) for i in range(t): h,c,t = list(map(float,input().split())) if(t == h): print(1) elif(t - (h+c)/2 <= 0): print(2) else: option1 = abs(t - (h+c)/2 ) n = (t - c)/(2*t-h-c) n2 = int(n) n3 = n2 + 1 option2 = value(n2,h,c,t) option3 = value(n3,h,c,t) lst = [option1,option2,option3] ans = min(lst) if(ans == option1): print(2) elif(ans == option2): print(2*n2 - 1) elif(ans == option3): print(2*n3 - 1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
T = int(input()) for iteration in range(T): [h, c, t] = list(map(int, input().split())) if t <= (h+c)/2: print(2) else: k = 1/2 * (h-c)/(2*t-h-c) k = k + 1/(4*(int(k)+1)) if k - int(k) <= 10**(-8): k = int(k) - 1 else: k = int(k) print(2*k + 1)
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.*; import java.io.*; import java.math.*; public class Main { private static FastReader fastReader = new FastReader(); public static void main(String[] args) { Task solver = new Task(); solver.solve(); } static class Task { private static StringBuilder result = new StringBuilder(); public void solve() { int nt = fastReader.nextInt(); while (nt-- > 0) { double h = fastReader.nextLong(); double c = fastReader.nextLong(); double t = fastReader.nextLong(); if(h == t){ result.append("1\n"); continue; } if(2*t <= (h+c)){ result.append("2\n"); continue; } double minAvg = Double.MAX_VALUE; long ans = 1; long low = 1; long high = (long) 1e6; while(low <= high){ long mid = low + (high-low)/2; long x = mid; long y = mid-1; double currAvg = Math.abs((x*h + y*c) - (x+y)*t); double fCurrAvg = currAvg / (x+y); if(fCurrAvg < minAvg){ minAvg = fCurrAvg; ans = x+y; } else if(fCurrAvg == minAvg){ ans = Math.min(ans, x+y); } if((x*h + y*c) > (x+y)*t) low = mid + 1; else high = mid - 1; } result.append(ans+"\n"); } fastReader.print(result.toString()); } } static class FastReader { public BufferedReader reader; public StringTokenizer tokenizer; private static PrintWriter pw; public FastReader() { reader = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String readLine() { try { return reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return null; } public void print(String str) { pw.print(str); pw.flush(); } } }
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 * T = int(input()) for T in range(1, T+1): h, c, t = input().split() h = int(h) c = int(c) t = int(t) if (t<=((h+c)/2)): ans = 2 #print(ans) elif (t>= h): ans = 1 #print(ans) else: k = (t-c)//(2*t-(h+c)) if ((t-c)%(2*t-(h+c))==0): ans = (2*k)-1 else: if (((((k*h)+((k-1)*c))/1)-t*(2*k-1))*(2*k+1)<= (2*k-1)*(t*(2*k+1)-(((((k+1)*h)+((k)*c))/1)))): ans = 2*k-1 else: ans = 2*k+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
T=int(input()) C=[] for i in range (0,T): h,c,t = [int(x) for x in input().split(' ')] if h+c==2*t: C.append(2) elif 2*t<h+c: if abs((h+c)/2-t)<abs(h-t): C.append (2) else: C.append(1) else: m= (h-t)//(2*t-h-c) if abs(((m+2)*h+(m+1)*c)*(2*m+1)-t*(2*m+1)*(2*m+3))>=abs(((m+1)*h+m*c)*(2*m+3)-t*(2*m+3)*(2*m+1)): C.append(2*m+1) else: C.append(2*m+3) for i in C: print(i)
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); const long long dx[] = {0, 0, 1, -1}; const long long dy[] = {1, -1, 0, 0}; const long long N = (long long)1e6 + 17; const long long M = (long long)2e3 + 69; const long long inf = (long long)1e14 + 3; const long long mod = (long long)1e9 + 7; long long t = 1, a[N]; void solve() { double h, c, n; cin >> h >> c >> n; if (n <= (h + c) / 2) { cout << "2\n"; return; } long long l = 1, r = inf; while (l < r) { long long m = (l + r) / 2; if ((h * m + c * (m - 1)) / (m * 2 - 1) > n) l = m + 1; else r = m; } l--; if (abs(n - (h * l + c * (l - 1)) / (l * 2 - 1)) <= abs(n - (h * r + c * (r - 1)) / (r * 2 - 1))) cout << l * 2 - 1 << "\n"; else cout << r * 2 - 1 << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); 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 math def getValue(num, data): [hot, cold, t] = data more = math.ceil(num) less = math.floor(num) f = (hot * more + cold * (more - 1)) / (2 * more - 1) s = (hot * less + cold * (less - 1)) / (2 * less - 1) if ((more - num) * 10 == 5): return more if (abs(f - t) < abs(s - t)): return more else: return less def countCups(info): [h, c, t] = info cups = 0 a = c - t b = h + c - (2 * t) if (b == 0): cups = 2 else: x = a / b if (x <= 0): cups = 2 else: x = getValue(x, info) cups = (2 * x) - 1 return cups for _ in range(int(input())): data = [*map(int, input().split(' '))] print(countCups(data))
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; bool f(int n, int h, int c, int t) { return (2 * n + 1ll) * abs(1ll * n * (h + c - 2 * t) + t - c) <= (2 * n - 1ll) * (1ll * n * (h + c - 2 * t) + h - t); } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long t = 1; cin >> t; while (t--) { int h, c, t; cin >> h >> c >> t; if (2 * t <= h + c) cout << "2" << "\n"; else { int a = h - t; int b = 2 * t - c - h; int k = 2 * (a / b) + 1; long long val1 = abs(k / 2 * 1ll * c + (k + 1) / 2 * 1ll * h - t * 1ll * k); long long val2 = abs((k + 2) / 2 * 1ll * c + (k + 3) / 2 * 1ll * h - t * 1ll * (k + 2)); if (val1 * (k + 2) <= val2 * k) cout << k << "\n"; else cout << k + 2 << "\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
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { 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=(t-c)/(2*t-h-c); // double td1=(double)((pd+1)*h+pd*c)/(2*pd+1); double abs2=Math.abs(t*(2*pd-1)-(pd*h+(pd-1)*c))/(2*pd-1.0); // ans=2*pd+1; pd++; double abs3=Math.abs(t*(2*pd-1)-(pd*h+(pd-1)*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
#!/usr/bin/env python from __future__ import division, print_function import os import sys import math from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def main(): T = int(input()) for _ in range(T): h, c, t = map(int, input().split()) if t <= (h + c) / 2: print(2) else: div = ((2 * t) - h - c) - 1 sol1 = (t - c + div) // ((2 * t) - h - c) sol2 = sol1 - 1 got1 = sol1 * h + (sol1 - 1) * c den1 = (2 * sol1) - 1 got2 = sol2 * h + (sol2 - 1) * c den2 = (2 * sol2) - 1 diff1 = abs((t * den1 * den2) - (got1 * den2)) diff2 = abs((t * den1 * den2) - (got2 * den1)) print(2 * (sol1 if diff1 < diff2 else sol2) - 1) #print(2 * (sol1 if abs(t - got1) < abs(t - got2) else sol2) - 1) # 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") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main()
PYTHON
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from fractions import Fraction as f import sys t = int(input()) for _ in range(t): h, c, t = [int(x) for x in input().split()] if h == t: print(1) continue x = (h + c) / 2 if x == t: print(2) continue ans = abs(f(h + c, 2) - t) ans2 = 2 if abs(h - t) <= ans: ans2 = 1 y = (t - h) / ((h + c) - 2 * t) y = int(y) for j in range(max(0, y - 5), y + 5 + 1): k = f(j * (h + c) + h, (2 * j + 1)) if abs(k - t) < ans: ans = (abs(k - t)) ans2 = 2 * j + 1 print(ans2)
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 i in range(t): h1,c1,t1=input().split(" ") h=int(h1) c=int(c1) t=int(t1) if (t<=(h+c)/2): print(2) else: x=(t-c) // (2*t-h-c) m=((h+c)*x-c)/(2*x-1)-t n=t-((h+c)*x+h)/(2*x+1) if (m<n): print(2*x-1) elif(m==n and m==int(m)): print(2*x-1) elif(m==n and m!=int(m)): 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.io.*; import java.math.*; import java.util.*; public class Sol2{ public static void main(String[] args) throws IOException{ FastIO sc = new FastIO(System.in); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { double h = sc.nextInt(); double c = sc.nextInt(); double b = sc.nextInt(); double avg = (h+c)/2; if(avg+.00000001>=b) { System.out.println(2); } else { double ans = (int)((h-b)/(b-avg))+1; if((int)ans%2==1) { double first = Math.abs((avg*((ans-1)/(ans)) + h/(ans)-b)); double second = Math.abs((avg)*(ans+1)/(ans+2) + h/(ans+2)-b); //System.out.println(first + " " + second); if(second+.000000001<first) { System.out.println((int)ans+2); }else { System.out.println((int)ans); } }else { double first = Math.abs((avg*(ans/(ans+1)) + h/(ans+1)-b)); double second = Math.abs((avg*(ans-2)/(ans-1)) + h/(ans-1)-b); //System.out.println(first + " " + second); if(first<second+.000000001) System.out.println((int)ans+1); else System.out.println((int)ans-1); } } } out.close(); } static class FastIO { // Is your Fast I/O being bad? InputStream dis; byte[] buffer = new byte[1 << 17]; int pointer = 0; public FastIO(String fileName) throws IOException { dis = new FileInputStream(fileName); } public FastIO(InputStream is) throws IOException { dis = is; } int nextInt() throws IOException { int ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } long nextLong() throws IOException { long ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } byte nextByte() throws IOException { if (pointer == buffer.length) { dis.read(buffer, 0, buffer.length); pointer = 0; } return buffer[pointer++]; } String next() throws IOException { StringBuffer ret = new StringBuffer(); byte b; do { b = nextByte(); } while (b <= ' '); while (b > ' ') { ret.appendCodePoint(b); b = nextByte(); } return ret.toString(); } } }
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.readline INF = 10**13 Q = int(input()) Query = [list(map(int, input().split())) for _ in range(Q)] for h, c, t in Query: if 2*t <= h+c: ans = 2 else: l = 0 r = INF while r - l > 1: m = (l+r)//2 if (h+c)*m+h > (2*m+1)*t: l = m else: r = m if 2*t*(2*r+1)*(2*l+1) - ((h+c)*r+h)*(2*l+1) >= ((h+c)*l+h)*(2*r+1): ans = 2*l+1 else: ans = 2*r+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 math def getval(y, h, c, t): return abs(((((y+1)*h + y*c) - (2*y+1)*t))/(2*y+1)) for _ in range(int(input())): h, c, t = map(int, input().split()) avg = (h+c)/2 if (avg >= t): print(2) continue b = (t-h)/(h+c-2*t) b1 = math.ceil(b) if (getval(b1-1, h, c, t) <= getval(b1, h, c, t)): print(2*b1-1) else: print(2*b1+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.*; import java.io.*; public class C88 { static int c, h, temp; public static void main(String [] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t > 0) { h = sc.nextInt(); c = sc.nextInt(); temp = sc.nextInt(); double x = ((double)temp - c) / (2 * temp - h - c); if (temp <= ((double)h + c) / 2) { out.println(2); } else { if (temp == h) { out.println(1); } else { int y = (int) x; long ret = comp(y); if (ret <= 0) { out.println(2 * y - 1); } else { out.println(2 * y + 1); } } } t--; } out.close(); } static long comp(int y) { long first = (h * y + c * (y - 1)) * (2 * y + 1) + (h * (y + 1) + c * y) * (2 * y - 1) - 2 * temp * (2 * y + 1) * (2 * y - 1); return first; } //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math t = int(input()) for _ in range(t): h,c,t = list(map(int, input().split(' '))) mid = (h+c)/2 if t <= mid: print(2) elif t >= h: print(1) else: x = (h-t)/(2*t-h-c) x1 = int(math.floor(x)) x2 = int(math.ceil(x)) t1 = (h-c)*(x1+1)*(2*x2+1)+(c-t)*(2*x1+1)*(2*x2+1) t2 = (h-c)*(x2+1)*(2*x1+1)+(c-t)*(2*x1+1)*(2*x2+1) if abs(t1) > abs(t2): print(int(2*x2+1)) else: print(int(2*x1+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 maxint from math import floor Q = input() for q in range(Q) : H, C, T = map(int, raw_input().split()) base = (H+C) / 2.0 if base >= T : print 2 else : k = int(floor((H-T) / float(2*T-H-C))) k0 = abs(k*(H+C)+H - T*(2*k+1)) * (2*k+3) k1 = abs((k+1)*(H+C)+H - T*(2*k+3)) * (2*k+1) if k0 <= k1 : print 2*k+1 else : print 2*k+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 java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class code { public static void main(String[] args)throws IOException { FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int t1 = sc.nextInt(); //System.out.println(1); while (t1-- > 0) { long h = sc.nextLong(); long c = sc.nextLong(); long t = sc.nextLong(); if (h == t) System.out.println(1); else if (((h + c) / 2) >= t) { System.out.println(2); } else { long xx = (h - t) / (2 * t - h - c); double abs1 = Math.abs(t * 1.0 * (2 * xx + 1) - (((xx + 1) * h) + (xx * c))) / (2 * xx + 1.0); xx++; double abs2 = Math.abs(t * 1.0 * (2 * xx + 1) - (((xx + 1) * h) + (xx * c))) / (2 * xx + 1.0); if (abs2 >= abs1) { xx--; } System.out.println(2 * xx + 1); } } } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.TreeMap; import java.util.TreeSet; public class A { static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(int arr[], int l, int r) { if (l < r) { int m = (l+r)/2; sort(arr, l, m); sort(arr , m+1, r); merge(arr, l, m, r); } } static void revOrder(int[] arr) { int[] a = new int[arr.length]; for(int i=0;i<arr.length;i++) { a[i] = arr[arr.length-i-1]; } for(int i=0;i<arr.length;i++) arr[i] = a[i]; } static int next(long target, long[] arr) { int start = 0, end = arr.length - 1; int ans = -1; while (start < end) { int mid = start + (end-start) / 2; if (arr[mid] <=target) { start = mid + 1; ans = mid; } else { end = mid - 1; } } return ans; } static void pf(int n, ArrayList<Integer> al) { for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { if (n/i == i) { al.add(i); } else{ al.add(i); al.add(n/i); } } } } static long power(long x, long y, long p) { long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static int gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; int k; for (k = 0; ((a | b) & 1) == 0; ++k) { a >>= 1; b >>= 1; } while ((a & 1) == 0) a >>= 1; do { while ((b & 1) == 0) b >>= 1; if (a > b) { int temp = a; a = b; b = temp; } b = (b - a); } while (b != 0); return a << k; } static long inv(long a, long m){ return power(a, m-2, m); } static DecimalFormat df = new DecimalFormat("0.0000000000"); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { try { BufferInput in = new BufferInput(); int t = in.nextInt(); while(t-->0) { long H = in.nextLong(); long C = in.nextLong(); long T = in.nextLong(); if(2*T <= H+C) { out.println("2"); } else { long low = 1L; long high = 1000000000L; while(low != high) { long mid = (low+high+1)/2; if(T*(2*mid-1) <= mid*H+(mid-1)*C) low = mid; else high = mid-1; } //just one long res = low; double num1 = Math.abs(T*(2*low-1)-(res*H+(res-1)*C)); double dem1 = 2*low-1; double num2 = Math.abs(T*(2*low+1)-((res+1)*H+C*res)); double dem2 = 2*low+1; if(num1*dem2 <= num2*dem1) res = 2*res-1; else res = 2*res+1; out.println(res); } } out.close(); }catch(Exception e) {} } static class Pair implements Comparable<Pair> { int x;int y; Pair(int i,int o) { x=i;y=o; } public int compareTo(Pair n) { if(this.x!=n.x) return this.x-n.x; return this.y-n.y; } } static class BufferInput { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public BufferInput() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public BufferInput(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public String nextString() throws IOException{ byte c = read(); while(Character.isWhitespace(c)){ c = read(); } StringBuilder builder = new StringBuilder(); builder.append((char)c); c = read(); while(!Character.isWhitespace(c)){ builder.append((char)c); c = read(); } return builder.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public int[] nextIntArray(int n) throws IOException { int arr[] = new int[n]; for(int i = 0; i < n; i++){ arr[i] = nextInt(); } return arr; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long[] nextLongArray(int n) throws IOException { long arr[] = new long[n]; for(int i = 0; i < n; i++){ arr[i] = nextLong(); } return arr; } public char nextChar() throws IOException{ byte c = read(); while(Character.isWhitespace(c)){ c = read(); } return (char) c; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } public double[] nextDoubleArray(int n) throws IOException { double arr[] = new double[n]; for(int i = 0; i < n; i++){ arr[i] = nextDouble(); } return arr; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
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
# -*- coding: utf-8 -*- import math import collections import bisect import heapq import time import random import itertools import sys from typing import List """ created by shhuan at 2020/6/10 22:37 """ def solve(H, C, T): if T >= H: return 1 if T <= (H+C)//2: return 2 k = int((T-H)/(H+C-2*T)) 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): return k*2+1 else: return k*2+3 N = int(input()) ans = [] for i in range(N): H, C, T = map(int, input().split()) ans.append(solve(H, C, T)) 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
from sys import stdin from collections import deque # https://codeforces.com/contest/1354/status/D mod = 10**9 + 7 import sys import random # sys.setrecursionlimit(10**6) from queue import PriorityQueue # def rl(): # return [int(w) for w in stdin.readline().split()] from bisect import bisect_right from bisect import bisect_left from collections import defaultdict from math import sqrt,factorial,gcd,log2,inf,ceil # map(int,input().split()) # # l = list(map(int,input().split())) # from itertools import permutations import heapq # input = lambda: sys.stdin.readline().rstrip() input = lambda : sys.stdin.readline().rstrip() from sys import stdin, stdout from heapq import heapify, heappush, heappop from itertools import permutations from math import factorial as f # def ncr(x, y): # return f(x) // (f(y) * f(x - y)) def ncr(n, r, p): num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p import sys # input = sys.stdin.readline # LCA # def bfs(na): # # queue = [na] # boo[na] = True # level[na] = 0 # # while queue!=[]: # # z = queue.pop(0) # # for i in hash[z]: # # if not boo[i]: # # queue.append(i) # level[i] = level[z] + 1 # boo[i] = True # dp[i][0] = z # # # # def prec(n): # # for i in range(1,20): # # for j in range(1,n+1): # if dp[j][i-1]!=-1: # dp[j][i] = dp[dp[j][i-1]][i-1] # # # def lca(u,v): # if level[v] < level[u]: # u,v = v,u # # diff = level[v] - level[u] # # # for i in range(20): # if ((diff>>i)&1): # v = dp[v][i] # # # if u == v: # return u # # # for i in range(19,-1,-1): # # print(i) # if dp[u][i] != dp[v][i]: # # u = dp[u][i] # v = dp[v][i] # # # return dp[u][0] # # dp = [] # # # n = int(input()) # # for i in range(n + 10): # # ka = [-1]*(20) # dp.append(ka) # class FenwickTree: # def __init__(self, x): # """transform list into BIT""" # self.bit = x # for i in range(len(x)): # j = i | (i + 1) # if j < len(x): # x[j] += x[i] # # def update(self, idx, x): # """updates bit[idx] += x""" # while idx < len(self.bit): # self.bit[idx] += x # idx |= idx + 1 # # def query(self, end): # """calc sum(bit[:end])""" # x = 0 # while end: # x += self.bit[end - 1] # end &= end - 1 # return x # # def find_kth_smallest(self, k): # """Find largest idx such that sum(bit[:idx]) <= k""" # idx = -1 # for d in reversed(range(len(self.bit).bit_length())): # right_idx = idx + (1 << d) # if right_idx < len(self.bit) and k >= self.bit[right_idx]: # idx = right_idx # k -= self.bit[idx] # return idx + 1 # import sys # def rs(): return sys.stdin.readline().strip() # def ri(): return int(sys.stdin.readline()) # def ria(): return list(map(int, sys.stdin.readline().split())) # def prn(n): sys.stdout.write(str(n)) # def pia(a): sys.stdout.write(' '.join([str(s) for s in a])) # # # import gc, os # # ii = 0 # _inp = b'' # # # def getchar(): # global ii, _inp # if ii >= len(_inp): # _inp = os.read(0, 100000) # gc.collect() # ii = 0 # if not _inp: # return b' '[0] # ii += 1 # return _inp[ii - 1] # # # def input(): # c = getchar() # if c == b'-'[0]: # x = 0 # sign = 1 # else: # x = c - b'0'[0] # sign = 0 # c = getchar() # while c >= b'0'[0]: # x = 10 * x + c - b'0'[0] # c = getchar() # if c == b'\r'[0]: # getchar() # return -x if sign else x # fenwick Tree # n,q = map(int,input().split()) # # # l1 = list(map(int,input().split())) # # l2 = list(map(int,input().split())) # # bit = [0]*(10**6 + 1) # # def update(i,add,bit): # # while i>0 and i<len(bit): # # bit[i]+=add # i = i + (i&(-i)) # # # def sum(i,bit): # ans = 0 # while i>0: # # ans+=bit[i] # i = i - (i & ( -i)) # # # return ans # # def find_smallest(k,bit): # # l = 0 # h = len(bit) # while l<h: # # mid = (l+h)//2 # if k <= sum(mid,bit): # h = mid # else: # l = mid + 1 # # # return l # # # def insert(x,bit): # update(x,1,bit) # # def delete(x,bit): # update(x,-1,bit) # fa = set() # # for i in l1: # insert(i,bit) # # # for i in l2: # if i>0: # insert(i,bit) # # else: # z = find_smallest(-i,bit) # # delete(z,bit) # # # # print(bit) # if len(set(bit)) == 1: # print(0) # else: # for i in range(1,n+1): # z = find_smallest(i,bit) # if z!=0: # print(z) # break # # service time problem # def solve2(s,a,b,hash,z,cnt): # temp = cnt.copy() # x,y = hash[a],hash[b] # i = 0 # j = len(s)-1 # # while z: # # if s[j] - y>=x-s[i]: # if temp[s[j]]-1 == 0: # j-=1 # temp[s[j]]-=1 # z-=1 # # # else: # if temp[s[i]]-1 == 0: # i+=1 # # temp[s[i]]-=1 # z-=1 # # return s[i:j+1] # # # # # # def solve1(l,s,posn,z,hash): # # ans = [] # for i in l: # a,b = i # ka = solve2(s,a,b,posn,z,hash) # ans.append(ka) # # return ans # # def consistent(input, window, min_entries, max_entries, tolerance): # # l = input # n = len(l) # l.sort() # s = list(set(l)) # s.sort() # # if min_entries<=n<=max_entries: # # if s[-1] - s[0]<window: # return True # hash = defaultdict(int) # posn = defaultdict(int) # for i in l: # hash[i]+=1 # # z = (tolerance*(n))//100 # poss_window = set() # # # for i in range(len(s)): # posn[i] = l[i] # for j in range(i+1,len(s)): # if s[j]-s[i] == window: # poss_window.add((s[i],s[j])) # # if poss_window!=set(): # print(poss_window) # ans = solve1(poss_window,s,posn,z,hash) # print(ans) # # # else: # pass # # else: # return False # # # # # l = list(map(int,input().split())) # # min_ent,max_ent = map(int,input().split()) # w = int(input()) # tol = int(input()) # consistent(l, w, min_ent, max_ent, tol) # t = int(input()) # # for _ in range(t): # # n,m,k = map(int,input().split()) # # z = n//k # if z>=m: # print(m) # else: # m-=z # # print(m/k) # print(z-ceil(m/(k-1))) # # t = int(input()) # # for _ in range(t): # # l = [] # # n,m,x,y = map(int,input().split()) # # for i in range(n): # # la = list(input()) # l.append(la) # ans = 0 # for i in range(n): # j = 0 # while j<m: # if l[i][j] == '.': # if j+1<m: # if l[i][j+1] == '.': # if y<x: # j+=2 # ans+=y # else: # j+=1 # ans+=x # else: # j+=1 # ans+=x # else: # j+=1 # ans+=x # else: # j+=1 # # # print(j) # # print(ans) from decimal import * t = int(input()) for _ in range(t): h,c,t = map(int,input().split()) ans = 2 z = (c+h)/2 ans1 = abs(t-z) fin = ans1 try: z1 = (h-c)/(2*t - (h+c)) except: print(2) continue k1,k2 = int(z1),ceil(z1) hash = defaultdict(list) hash[fin].append(2) if k1%2==0 and k1>=0: k = (k1+1) t1 = (h*((k+1)//2) + c*((k-1)//2))/(k) temp = fin fin = min(fin,abs(t-t1)) if fin == abs(t-t1) and fin!=temp: hash[fin].append(k) if k1-1>0: k = (k1-1) t1 = (h*((k+1)//2) + c*((k-1)//2))/(k) temp = fin fin = min(fin,abs(t-t1)) if fin == abs(t-t1) and fin!=temp: hash[fin].append(k) if k1%2 != 0 and k1>0: k = k1 t1 = (h*((k+1)//2) + c*((k-1)//2))/(k) temp = fin fin = min(fin,abs(t-t1)) if fin == abs(t-t1) and fin!=temp: hash[fin].append(k) k1 = k2 if k1%2==0 and k1>=0: k = (k1+1) t1 = (h*((k+1)//2) + c*((k-1)//2))/(k) temp = fin fin = min(fin,abs(t-t1)) if fin == abs(t-t1) and fin!=temp: hash[fin].append(k) if k1-1>0: k = (k1-1) t1 = (h*((k+1)//2) + c*((k-1)//2))/(k) temp = fin fin = min(fin,abs(t-t1)) if fin == abs(t-t1) and fin!=temp: hash[fin].append(k) if k1%2 != 0 and k1>0: k = k1 t1 = (h*((k+1)//2) + c*((k-1)//2))/(k) temp = fin fin = min(fin,abs(t-t1)) if fin == abs(t-t1) and fin!=temp: hash[fin].append(k) print(min(hash[fin]))
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 = 28 n = int(input()) for i in range(n): h,c,t = list(map(int, input().split())) if (2*h+c)/3 < t <= h : if abs(t - (2*h+c)/3) < abs(t-h): print(3) else: print(1) elif (h+c)/2 >= t: print(2) else: k = (h-t)//(2*t-(h+c)) num = num1 = abs( t - Decimal(h*(k+1) + c*k )/Decimal(2*k + 1) ) k1 = k+1 num1 = abs( t - Decimal(h*(k1+1) + c*k1 )/Decimal(2*k1 + 1) ) # print(num,num1 , num2) if num <= num1: print(2*k+1) else: print(2 * k1 + 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 getValue(num, data): [hot, cold, t] = data more = math.ceil(num) less = math.floor(num) f = (hot * more + cold * (more - 1)) / (2 * more - 1) s = (hot * less + cold * (less - 1)) / (2 * less - 1) if (abs(f - t) < abs(s - t) or (more - num) * 10 == 5): return more else: return less def countCups(info): [h, c, t] = info cups = 0 a = c - t b = h + c - (2 * t) if (b == 0): cups = 2 else: x = a / b if (x <= 0): cups = 2 else: x = getValue(x, info) cups = (2 * x) - 1 return cups for _ in range(int(input())): data = [*map(int, input().split(' '))] print(countCups(data))
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.ceil((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) * h + (a - 1) * c - t * (2 * (a - 1) + 1); double denom1 = 2 * (a - 1) + 1; double num2 = t * (2 * (a) + 1) - ((a + 1) * h + (a) * c); // negate to make positive double denom2 = 2 * (a) + 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) + 1 : 2 * (a) + 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
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: valh=(c-t)//(h+c-2*t) valc=valh-1 ans=abs((h*valh+c*valc)-t*(2*valh-1)) ans1=(2*valh-1) valh1=valh+1 valc1=valh ans2=abs((h*valh1+c*valc1)-t*(2*valh+1)) ans3=(2*valh+1) if(ans2*ans1<ans*ans3): print(2*valh+1) else: print(2*valh-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> long long int mod = 1e9 + 7; using namespace std; const long long int INF = 1e18; const long long int MAX = 2e5 + 10; long long int power(long long int a, long long int b) { if (b == 0) return 1; long long int num = 1; long long int m = mod + 2; while (b != 1) { if (b % 2 == 0) { a *= a; a %= m; b /= 2; } else { num *= a; num %= m; b--; } } return (a * num) % m; } void solve() { long long int c, h, t; cin >> h >> c >> t; if (t == h) { cout << 1 << "\n"; return; } if (t <= ((c + h) / 2.0)) { cout << 2 << "\n"; return; } long long int val = (h - t - 1) / (2 * t - c - h) + 1; long long int val1 = (h - t) / (2 * t - c - h); double temp = (((h + c) * val + h) * 1.0) / (2 * val + 1); double temp1 = (((h + c) * val1 + h) * 1.0) / (2 * val1 + 1); if (fabs(temp - t) < fabs(temp1 - t)) cout << 2 * val + 1 << "\n"; else cout << 2 * val1 + 1 << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; t = 1; cin >> t; while (t--) { solve(); } return 0; ; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math def getValue(num, data): [hot, cold, t] = data more = math.ceil(num) less = math.floor(num) f = (hot*more + cold*(more-1)) / (2 * more - 1) s = (hot*less + cold*(less-1)) / (2 * less - 1) if (abs(f - t) == abs(s - t)): if ((more - num) * 10 == 5): return more return less if (abs(f - t) < abs(s - t)): return more else: return less def countCups(info): [h, c, t] = info cups = 0 a = c - t b = h + c - (2 * t) if (b == 0): cups = 2 else: x = a / b if (x <= 0): cups = 2 else: x = getValue(x, info) cups = 2 * x - 1 return cups for _ in range(int(input())): data = list(map(int, input().split(' '))) print(countCups(data))
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; inline long long ceil(long long a, long long b) { return (a + b - 1) / b; } long double h, c, tb; long double calc(long double x) { long double res = (x + 1) * h + x * c; res /= (2 * x + 1); res = abs(res - tb); return res; } void func() { cin >> h >> c >> tb; if (tb == h) { cout << "1\n"; return; } long double pp = (h + c) / 2; if (tb <= pp) { cout << "2\n"; return; } else { long long tt = (h - tb) / (2 * tb - h - c); long double chk1 = abs((h + c) / 2 - tb); long long x = max(tt, (long long)0); long long y = max(x - 1, (long long)0); long long z = max(x + 1, (long long)0); long double xx = calc((long double)x); long double yy = calc((long double)y); long double zz = calc((long double)z); if (min(min(xx, yy), zz) >= chk1) { cout << "2\n"; return; } if (yy <= min(xx, zz)) { cout << 2 * y + 1 << "\n"; return; } if (xx <= zz) { cout << 2 * x + 1 << "\n"; return; } cout << 2 * z + 1 << "\n"; } } int main() { { ios ::sync_with_stdio(false); cin.tie(0); cout.tie(0); } long long ntc = 1; cin >> ntc; for (long long i = 1; i < ntc + 1; i++) { func(); } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#!/usr/bin/python3.8 from fractions import Fraction from math import floor, ceil def f(h, c, x): return Fraction(h * (x + 1), x * 2 + 1) + Fraction(c * x, x * 2 + 1) def solve(): h, c, t = [int(x) for x in input().split()] a = h + c d1 = Fraction(h + c, 2) if h + c >= t * 2: print(2) return if t >= h: print(1) return x = Fraction(t - h, h + c - t * 2) if x < 0: x = 0 d2 = f(h, c, floor(x)) d3 = f(h, c, ceil(x)) dif1 = abs(d1 - t) dif2 = abs(d2 - t) dif3 = abs(d3 - t) dm = min(dif1, dif2, dif3) if dm == dif1: print(2) elif dm == dif2: print(2 * floor(x) + 1) else: print(2 * ceil(x) + 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
cases = int(input()) for _ in range(cases): h, c, t = [int(x) for x in input().split()] if t <= (h+c)/2: print(2) continue n = (t-h)/(h+c-2*t) x, y = int(n), int(n)+1 xx = ((x+1)*h+x*c) yy = ((y+1)*h+y*c) if xx*(2*y+1)+yy*(2*x+1) <= 2*t*(2*y+1)*(2*x+1): 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
""" // Author : snape_here - Susanta Mukherjee """ from __future__ import division, print_function import os,sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def ii(): return int(input()) def si(): return input() def mi(): return map(int,input().split()) def li(): return list(mi()) def read(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') def gcd(x, y): while y: x, y = y, x % y return x mod=1000000007 import math import bisect from decimal import * getcontext().prec = 50 abc="abcdefghijklmnopqrstuvwxyz" def fn(n): return (n*(n+1))//2 pi=3.141592653589793238 def main(): for _ in range(ii()): h,c,t=mi() if t>=h: print(1) continue x=(h+c)/2 if t<=x: print(2) continue z=t-x a=h+c p=h-t #print(p,z) y=int(round(p/z)) ans=y+1 if ans%2==0: d=ans-1 e=ans+1 #print(d,e) r=h+2*x*(d//2) s=h+2*x*(e//2) r=Decimal(r)/Decimal(d) s=Decimal(s)/Decimal(e) #print(r,s) s1=abs(t-r) s2=abs(t-s) if s1<=s2: ans=d else: ans=e print(ans) # 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") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": #read() main()
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 input = sys.stdin.buffer.readline def print(*val): sys.stdout.write(str(*val) + '\n') 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
// package 第1359场; /* 盗图小能手 ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ */ import java.io.IOException; import java.util.Scanner; public class C { /** * 比赛时写下的,思路是对的,但写错了公式!! * 而且最大的问题在于!一定要避免浮点运算,浮点会有值卡住过不了!! * * @param h * @param c * @param t */ private static void fun(long h, long c, long t) { if (h == t) { System.out.println(1); return; } if (h + c >= 2 * t) { System.out.println(2); return; } // 冷水x杯,热水x+1杯 long x = (h - t) / (2 * t - h - c); // 附近两个值,谁更接近就是谁 long a1 = ((x + 1) * h + x * c); long a2 = ((x + 2) * h + (x + 1) * c); double abs1 = Math.abs(a1 - t * (2 * x + 1)); double abs2 = Math.abs(a2 - t * (2 * x + 3)); // TODO 这里这个细节相当难注意到,要仔细看公式!!!看官方提交给出的不等式就目标了。其实就是分母统一一下才能比较 // abs1/(2 * x + 1)>abs2/(2 * x + 3) // 交叉写一下 if (abs1 * (2 * x + 3) > abs2 * (2 * x + 1)) { System.out.println(2 * x + 3); } else { System.out.println(2 * x + 1); } } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int numberOfTestcases = sc.nextInt(); for (int tt = 0; tt < numberOfTestcases; tt++) { long h = sc.nextLong(); long c = sc.nextLong(); long t = sc.nextLong(); fun(h, c, t); } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; const long long int mod = 1000000007; long long int T, t, n, m, h, c, ini, fin; double can, aux, can2; int main() { cin >> T; while (T--) { cin >> h >> c >> t; aux = t; if (t >= h) cout << 1 << "\n"; else { if (t <= ((h + c) / 2)) cout << 2 << "\n"; else { ini = 2; fin = 10000000; while (ini != fin) { m = ini + fin; m /= 2; can = (h * m) + (c * (m - 1)); can /= double(m + m - 1); if (can <= aux) fin = m; else ini = m + 1; } can = (h * fin) + (c * (fin - 1)); can /= double(fin + fin - 1); can2 = (h * (fin - 1)) + (c * (fin - 2)); can2 /= double(fin + fin - 3); if (abs(can - aux) < abs(can2 - aux)) cout << fin + fin - 1 << "\n"; else cout << fin + fin - 3 << "\n"; } } } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; int main() { long long T = 1; cin >> T; while (T--) { long long h, c, t; cin >> h >> c >> t; if (t == h) { cout << 1 << "\n"; continue; } if (2 * t <= (h + c)) { cout << 2 << "\n"; continue; } long long x = (t - c) / (2 * t - h - c); long long y = x + 1; long double t1 = (h * x + c * (x - 1)) / (1.0 * (2 * x - 1)); long double t2 = (h * y + c * (y - 1)) / (1.0 * (2 * y - 1)); long double diff1 = abs(t1 - t); long double diff2 = abs(t2 - t); if (diff1 <= diff2) { cout << 2 * x - 1; } else { cout << 2 * y - 1; } cout << "\n"; } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; class Time { chrono::high_resolution_clock::time_point start; public: Time() { start = chrono::high_resolution_clock::now(); } ~Time() { cout << "\nTime: " << chrono::duration_cast<chrono::duration<double>>( chrono::high_resolution_clock::now() - start) .count() << "s\n"; } }; void fIO() { freopen("output.txt", "w", stdout); freopen("input.txt", "r", stdin); } void testCase() { long long int h, c, t; cin >> h >> c >> t; if (t <= (h + c) / 2) { cout << "2\n"; return; } long long int k = (h - t) / (2 * t - h - c); if (abs(k * c + (k + 1) * h - t * (2 * k + 1)) * (2 * k + 3) <= abs((k + 1) * c + (k + 2) * h - t * (2 * k + 3)) * (2 * k + 1)) { cout << 2 * k + 1 << '\n'; } else { cout << 2 * k + 3 << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; cin >> t; for (; t; --t) { testCase(); } 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
test=int(input()) while test: h,c,t=map(int,input().split()) choto=abs((h+c)-t) if (h+c-2*t)*(t-c)>=0: print(2) else: x=int((c-t)/(h+c-2*t)) m=abs((x*(h+c-2*t)+t-c)/(2*x-1)) n=abs(((x+1)*(h+c-2*t)+t-c)/(2*(x+1)-1)) if m<=n: print(2*x-1) else: print(2*x+1) test-=1
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
def f(h,c,t,x): n=x*2+1 return abs((h*(x+1)+c*x)-t*n) for _ in range(int(input())): h,c,t=map(int,input().split()) if h+c>=t*2:print(2);continue if h<=t:print(1);continue ans1=(h-t)//(2*t-h-c) ans2=ans1+1 if f(h,c,t,ans1)*(2*ans2+1)<=f(h,c,t,ans2)*(2*ans1+1):print(ans1*2+1) else:print(ans2*2+1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h,c,t = map(int,input().split()) if t<=(h+c)/2: print(2) else: x = (h-t)//(2*t-h-c) v1 = ((x+1)*h + x*c) v2 = ((x+2)*h + (x+1)*c) # print(v1,v2) # print(abs(v1-(2*x+1)*t)/(2*x+1),abs(t*(2*x+3)-v2)/(2*x+3)) if abs(v1-(2*x+1)*t)/(2*x+1)<=abs(t*(2*x+3)-v2)/(2*x+3): print(2*x+1) else: print(2*x+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
from math import * s=int(input()) for _ in range(s): h,c,t=map(int,input().split()) k=(h+c)/2 if k>=t: ans=2 else: d=(h-t) x=2*t -h-c l=d//x a=(l+1)*h +l*c m=abs(a-t*(2*l +1)) m=m*(2*l +3) b=(l+2)*h +(l+1)*c n=abs(b-t*(2*l +3)) n=n*(2*l +1) if m>n: ans=2*l+3 else: ans=2*l+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 java.util.*; public class CodeForces1359C{ public static void main(String[] args) { Scanner input = new Scanner(System.in); int q = input.nextInt(); for(int i = 0;i<q;i++){ int h = input.nextInt(); int c = input.nextInt(); int t = input.nextInt(); if(2*t <= h+c){ System.out.println(2); continue; } int k = (int)Math.floor((double)(h-t)/(2*t-h-c)); if(Math.abs((k*(h+c)+h)-t*(2*k+1)) * (2*k+3) <=Math.abs(((k+1)*(h+c)+h)-t*(2*k+3)) * (2*k+1)){ System.out.println(2*k+1); } else{ System.out.println(2*(k+1)+1); } } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class Prac{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static class Key { private final int x; private final int y; public Key(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Key)) return false; Key key = (Key) o; return x == key.x && y == key.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } } static class Pair{ int x,y; public Pair(int x,int y){ this.y=y; this.x=x; } } static PrintWriter w = new PrintWriter(System.out); static long mod1=998244353L,mod=1000000007; static long h,c,t1; static double cal(long y){ return ((Math.abs(t1*(2*y+1)-(y*(h+c)+h)))*1.0)/((2*y+1)*1.0); } public static void main(String [] args){ InputReader sc=new InputReader(System.in); int t=sc.ni(); while(t-->0){ h=sc.ni(); c=sc.ni(); t1=sc.ni(); long ans=1; double min=h; double x=((h+c)*1.0)/2; if(h==t1){ w.println(1); continue; } long val=2*t1-h-c; if(val<=0){ w.println(2); continue; } long y=(h-t1)/(2*t1-h-c); double t11=cal(y); double t12=cal(y+1); if(t12>=t11)w.println(2*y+1); else w.println(2*(y+1)+1); } w.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(ll i) { return (ll((i + 1) / 2) * h + ll(i / 2) * c) / (double)i; } 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 len = 1ll << 38; ll steps; while (len >= 1) { ll mid = low + len / 2; double cavg = f(mid); if (cavg >= t) { steps = mid; low = mid; } len /= 2; } ll ans = 2; double avg = (h + c) / 2.0; for (ll i = max<int>(1, steps - 100); i <= steps + 100; ++i) { double cavg = f(i); if (fabs(t - cavg) < fabs(t - avg)) { ans = i; avg = cavg; } } cout << ans << 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 t = int(input()) for _ in range(t): h, c, t = [int(x) for x in input().split()] s = h + c if 2 * t <= h + c: print(2) elif t == h: print(1) else: denom = (2 * t) - h - c alpha1 = (h - t) // denom alpha2 = alpha1 - 1 alpha3 = alpha1 + 1 val1 = Decimal(alpha1 * s + h) / Decimal(2 * alpha1 + 1) val2 = Decimal(alpha2 * s + h) / Decimal(2 * alpha2 + 1) val3 = Decimal(alpha3 * s + h) / Decimal(2 * alpha3 + 1) diff1 = abs(Decimal(t) - val1) diff2 = abs(Decimal(t) - val2) diff3 = abs(Decimal(t) - val3) if alpha2 >= 0 and diff1 > diff2: alpha1 = alpha2 diff1 = diff2 if diff1 > diff3: alpha1 = alpha3 print((2 * alpha1) + 1) # 499,998.000002
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 for _ in range(int(input())): h, c, t = map(int, input().split()) if t == h: print(1) continue if 2*t <= (h + c): print(2) continue x = max(1,((t-c) //(2 * t - h - c))) y=x+1 # print(x,y) try1=((x*h)+(x-1)*c) try2=((y*h)+(y-1)*c) diff1=abs(try1-t*(2*x-1))/(2*x-1) diff2=abs(try2-t*(2*y-1))/(2*y-1) if diff1<=diff2: 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
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) ans = [] for _ in range(tc): h, c, t = map(int, input().split()) if h + c == 2 * t: ans.append(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) ans.append(min(cands, key = cmp)[1]) print('\n'.join(map(str, ans))) 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
/* I FOLLOW THE WAY OF THE RIGHT HONOURABLE JIM KWIK, GOD BLESS YOU SIR 1. I AM A 'NEWBIE' IN RATING 2. I WILL FAIL MANY TIMES, BUT I WILL NOT FAIL TO LEARN FROM FAILURE 3. I WILL NEVER SURRENDER MY GOAL, NO MATTER HOW MUCH I FAIL 4. I WILL PROVE MY SKILLS WHEN THE TIME COMES 5. I WILL ALWAYS KEEP TRYING */ import java.util.*; import java.io.*; public class A { // Decimals are kinda shady, credit to the editorial for making me notice // also, i forgot to move the denominators on each side to the other side, that's why i got WA on test 4 static boolean tests = true; static long calc(int h, int c, int k){ return (long)h*(k+1)+(long)c*k; } static void solve(){ int h = fs.nextInt(); int c = fs.nextInt(); int t = fs.nextInt(); if (1.0*(h+c)/2 >= t){ out.println(2); return; } int k = (h-t)/(2*t-h-c); if ((long)(2*k+1)*Math.abs(calc(h, c, k+1)-(long)t*(2*k+3)) < (long)(2*k+3)*Math.abs(calc(h, c, k)-(long)t*(2*k+1))) ++k; out.println(2*k+1); } static FastScanner fs; static PrintWriter out; static int int_max = (int)1e9, mod = int_max+7; public static void main(String[] args) { fs = new FastScanner(); out = new PrintWriter(System.out); int t = tests ? fs.nextInt() : 1; while (t-- > 0) solve(); out.close(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readIntArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readLongArray(int n){ long a[] = new long[n]; for (int i = 0; i < n; ++i){ a[i] = nextLong(); } return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(System.in); int t = sc.nextInt(); for(int ti = 0; ti < t; ti++){ long h = sc.nextInt(); long c = sc.nextInt(); long m = sc.nextInt(); if(h == m){ System.out.println(1); }else if((h+c) >= m*2){ System.out.println(2); }else{ long nowmin = h-m; long left = 0; long right = 1000000; long ans = 1; while(right - left > 1){ long mid = (left+right)/2; long tmp = mid*h+(mid-1)*c-(mid*2-1)*m; if(Math.abs(tmp)*ans < nowmin*(mid*2-1) || Math.abs(tmp)*ans == nowmin*(mid*2-1) && ans > mid){ nowmin = Math.abs(tmp); ans = mid*2-1; } if(tmp < 0){ right = mid; }else{ left = mid; } } System.out.println(ans); } } } } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public double[] nextDoubleArray(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } }
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 sys import stdin input = stdin.readline q = int(input()) for _ in range(q): h,c,t = map(int,input().split()) if 2*t <= h+c: print(2) else: t*=2 h*=2 c*=2 w = h//2-c//2 pod = (h+c)//2 #w/(2k+1) jak najblizej t-pod goal = t-pod if goal == 0: print(1) else: x = w//goal wynik = 2 mini = 23649823847238 for szkl in range(x-3,x+4): if szkl%2 == 1 and szkl >= 1 and abs(goal-w/szkl) < mini: mini = abs(goal-w/szkl) wynik = szkl print(wynik)
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 F t = int(input()) for _ in range(t): h,c,t = map(int,input().split()) if 2 * t - h - c == 0: k = 0 else: k = (h - t) // (2 * t - h - c) t = F(t) def f(mid): return F(mid * (h + c) + h, 2 * mid + 1) res, tmp = 10 ** 100, F(10 ** 100) for i in range(k - 10, k + 10): if i < 0: continue dif = abs(t - f(i)) if dif < tmp: tmp = dif res = 2 * i + 1 elif dif == tmp: res = min(res, 2 * i + 1) for i in range(10): dif = abs(t - f(i)) if dif < tmp: tmp = dif res = 2 * i + 1 elif dif == tmp: res = min(res, 2 * i + 1) f = abs(t - F(h + c, 2)) if f <= tmp: res = min(res, 2) 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
from math import inf t=int(input()) for _ in range(t): h,c,t=map(int,input().split()) min1=abs(t-h) min2=abs(t-(h+c)/2) min3=inf if t<(h+c)/2: print(2) elif 2*t==(h+c): print(2) elif t==h: print(1) else: a1=int((t-c)/(2*t-h-c)) a2=a1+1 if abs((h*a2+c*(a2-1)-t*(2*a2-1))*(2*a1-1) )>=abs(h*a1+c*(a1-1)-t*(2*a1-1))*abs(2*a2-1): a0=a1 min3=abs((h*a1+c*(a1-1)-t*(2*a1-1))/(2*a1-1)) else: a0=a2 min3=abs((h*a2+c*(a2-1)-t*(2*a2-1))/(2*a2-1)) if min1==min(min1,min2,min3): print(1) elif min2==min(min1,min2,min3): print(2) else: print(2*a0-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 inp(): return(int(input())) import math def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) import bisect #python3 codeforces_1359C.py T = inp() def odds(num): high = math.ceil(num) low = math.ceil(num) if low % 2 == 0: low -=1 if high % 2== 0: high +=1 return low,high pass def solve(H,C,t): even_avg = (H + C) / 2 #Time to calculate odds val1 = (H - C) / 2 val2 = t - even_avg if val2 == 0: return 2 odd_position = val1/val2 if odd_position < 1: if H < even_avg: return 1 else: return 2 low,high = odds(odd_position) difference = abs(t - even_avg) #print(low, high) if low > 400000: return high number = 2 for i in [low, high]: item1 = ((i + 1) // 2) * H item2 = (i // 2) * C avg = float((item1 + item2) / i) diff = abs(t-avg) if diff < difference: difference = diff number = i pass return number for i in range(T): H,C,t = inlt() print(solve(H,C,t)) #999977 17 499998
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h, c, t = map(int, input().split()) if h <= t: print(1) continue if h + c >= t*2: print(2) continue lo = 1 hi = 10**10 while lo < hi: mid = (lo + hi + 1) // 2 totTemp = mid * h + (mid-1) * c if totTemp // (mid + mid-1) >= t: lo = mid else: hi = mid - 1 totTemp = lo * h + (lo-1) * c if totTemp // (lo + lo-1) < t: print(1/0) a = lo b = lo-1 totTemp = a*h + b*c totTarget = (a+b) * t totTemp2 = totTemp + c + h totTarget2 = totTarget + t*2 #if totTemp - totTarget / (a+b) < totTarget2 - totTemp / (a+b+2): #print(a+b) #print((totTemp - totTarget) / (a+b), (totTarget2 - totTemp) / (a+b+2)) if (totTemp - totTarget) * (a+b+2) <= (totTarget2 - totTemp2) * (a+b): print(a+b) else: print(a+b+2)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for T in range(int(input())): h ,c , x =list(map(int,input().split())) if h == x : print(1) else : av = (h+c)/2 if av >=x : print(2) else : n = (h-x)//(2*x -h -c) p = n +1 temp = ((h+c)*n + h)*(2*p +1) temp1 = ((h+c)*p + h)*(2*n +1) if 2*x*(2*p +1)*(2*n +1) >= temp + temp1 : print(2*n+1) else : print(2*p +1)
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 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") ############################################ t=int(input()) from math import ceil from fractions import Fraction for i in range(t): h,c,t=map(int,input().split()) av1=Fraction(h+c,2) if av1==t: print(2) elif av1>t: print(2) else: x=int((h-t)/abs(t-av1))+1 y=x+1 l=[] if x%2!=0: l.append([abs((av1*(x-1)+h)*Fraction(1,x)-Fraction(t,1)),x]) l.append([abs((av1*(x+1)+h)*Fraction(1,x+2)-Fraction(t,1)),x+2]) if y%2!=0: l.append([abs((av1*(y-1)+h)*Fraction(1,y)-Fraction(t,1)),y]) l.append([abs((av1*(y+1)+h)*Fraction(1,y+2)-Fraction(t,1)),y+2]) l.append([abs(Fraction(t,1)-av1),2]) l.sort() print(l[0][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; void solve() { long long int i, k, j, h, t, c; cin >> h >> c >> t; if (t == h) { cout << 1 << '\n'; return; } if (t == (h + c) / 2) { cout << 2 << '\n'; return; } if (t < (h + c) / 2) { cout << 2 << '\n'; return; } double n; n = ((t - c) * 1.0) / (2 * t - h - c); i = floor(n); j = ceil(n); if (i == j) { cout << 2 * i - 1 << '\n'; return; } double x = (h + c) / (2.0); x = abs(x - t); double s1 = (i * h + i * c - c) / ((2 * i - 1) * 1.0); s1 = abs(t - s1); double s2 = (j * h + j * c - c) / ((2 * j - 1) * 1.0); s2 = abs(t - s2); if (min(x, min(s1, s2)) == s1) { cout << 2 * i - 1 << '\n'; return; } else if (min(x, min(s1, s2)) == s2) cout << 2 * j - 1 << '\n'; else { cout << 2 << '\n'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 1; cin >> t; while (t--) solve(); return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h,c,t=map(int,input().split()) if (h+c)//2>=t:print(2) elif h==c:print(1) else: i=(t-c)//(2*t-h-c) p=abs((i*(h+c-2*t)+t-c)/(2*i-1)) q=abs(((i+1)*(h+c-2*t)+t-c)/(2*(i+1)-1)) print(2*i-1) if p<=q else print(2*i+1)
PYTHON3
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); EInverseGenealogy solver = new EInverseGenealogy(); solver.solve(1, in, out); out.close(); } static class EInverseGenealogy { private static final String NO = "NO"; private static final String YES = "YES"; private static final int[][] small = { // x = 0 null, // x = 1 {-1}, // x = 2 {-1, 0, 1, 2, 0}, // x = 3 {-1, 0, 0}, // x = 4 {-1, 0, 0, 1, 1}, // x = 5 {-1, 0, 0, 1, 1, 2}, // x = 6 {-1, 0, 0, 1, 1, 3, 3, 4}, // x = 7 {-1, 0, 0, 1, 1, 2, 2}}; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(), k = in.nextInt(); if (n % 2 == 0) { out.println(NO); return; } if (n == 1) { if (k == 0) { out.println(YES); out.println(0); } else { out.println(NO); } return; } int balanced = n - k; int leaves = n / 2 + 1; int x = balanced - leaves; // number of balanced internal nodes n -= leaves; // number of internal nodes if (x <= 0 || x > n) { out.println(NO); return; } Util.ASSERT(x > 0 && n > 0); int[] answer = solve(x, n); if (answer == null) { out.println(NO); return; } int[] degree = new int[answer.length]; for (int i = 0; i < answer.length; i++) { if (answer[i] >= 0) degree[answer[i]]++; } out.println(YES); for (int i = 0; i < answer.length; i++) { answer[i]++; } out.print(Util.join(answer) + " "); for (int i = 0; i < answer.length; i++) { for (int j = degree[i]; j < 2; j++) { out.print((i + 1) + " "); } } } private int[] solve(int x, int n) { int[] ans = f(x); if (ans == null || ans.length > n) return null; return pad(ans, n); } private int[] f(int x) { if (x < small.length) { return small[x]; } if (Integer.bitCount(x + 1) == 1) { return powerOfTwoMinus1(x); } int powerOfTwoMinus1 = getPowerOfTwoMinus1(x); int remaining = x - powerOfTwoMinus1; if (Integer.bitCount(remaining + 1) == 1 && remaining != powerOfTwoMinus1) { // remaining is also 2^k-1, so we do a merge with an imbalanced root return merge(f(powerOfTwoMinus1), f(remaining)); } // merge with balanced root return merge(f(powerOfTwoMinus1), pad(f(remaining - 1), remaining)); } private int[] pad(int[] ans, int n) { if (ans.length >= n) return ans; int[] big = new int[n]; System.arraycopy(ans, 0, big, big.length - ans.length, ans.length); for (int i = 0; i < big.length - ans.length + 1; i++) { big[i] = i - 1; } for (int i = big.length - ans.length + 1; i < big.length; i++) { big[i] += big.length - ans.length; } return big; } private int[] merge(int[] left, int[] right) { if (left == null || right == null) return null; int[] answer = new int[1 + left.length + right.length]; answer[0] = -1; System.arraycopy(left, 0, answer, 1, left.length); answer[1] = 0; System.arraycopy(right, 0, answer, 1 + left.length, right.length); answer[1 + left.length] = 0; for (int i = 1 + 1; i < 1 + left.length; i++) { answer[i] += 1; } for (int i = 1 + left.length + 1; i < answer.length; i++) { answer[i] += 1 + left.length; } return answer; } private int getPowerOfTwoMinus1(int x) { int powerOfTwoMinus1 = 3; while (true) { if (x <= powerOfTwoMinus1 * 3) { return powerOfTwoMinus1; } powerOfTwoMinus1 = 2 * (powerOfTwoMinus1 + 1) - 1; } } private int[] powerOfTwoMinus1(int x) { int[] ans = new int[x]; ans[0] = -1; for (int i = 1; i < x; i++) { ans[i] = (i - 1) / 2; } return ans; } } static class Util { public static String join(int... i) { StringBuilder sb = new StringBuilder(); for (int o : i) { sb.append(o); sb.append(" "); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } public static void ASSERT(boolean assertion) { if (!assertion) throw new AssertionError(); } private Util() { } } static class InputReader { public final BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, m, k; bool works(int n, int k) { if (!(n & 1) || k < 0) return 0; if (k < 2) return k ^ !(n & (n + 1)); if (n == 9 && k == 2) return 0; return 2 * k + 3 <= n; } void sol(int n, int k, int p) { int x = ++m; cout << p << (x > ::n ? '\n' : ' '); for (int i = 1; i < n; i = 2 * i + 1) { int j = n - i - 1, l = k - (max(i, j) >= 2 * min(i, j)); if (works(i, 0) && works(j, l)) { sol(i, 0, x), sol(j, l, x); return; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; if (!works(n, k)) { cout << "NO" << '\n'; } else { cout << "YES" << '\n'; sol(n, k, 0); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, a[100500], cnt = 1; bool A[5][5]; bool can(int i, int j) { if (j < 0 || j > i) return 0; if (i <= 4) return A[i][j]; if (((i + 1) & -(i + 1)) == (i + 1)) return j != 1 && j != i; return j != 0 && j != i; } void work(int st, int n, int k) { if (n == 0) return; int j; for (j = 0; j < n; j = j * 2 + 1) ; for (j /= 2;; j /= 2) { int ub = (j > 2 * (n - 1 - j) || 2 * j < (n - 1 - j)); for (int t = 0; t <= j && t <= k; ++t) if (can(j, t) && can(n - 1 - j, k - ub - t)) { a[++cnt] = st; work(cnt, j, t); a[++cnt] = st; work(cnt, n - 1 - j, k - ub - t); return; } else if (t >= 50) break; if (j == 0) return; } } int main() { A[0][0] = A[1][0] = A[2][1] = A[3][0] = A[3][2] = A[4][1] = A[4][3] = 1; int n, k; cin >> n >> k; if (n % 2 == 0 || !can(n = n / 2, k)) puts("NO"); else { puts("YES"); work(1, n, k); for (int i = 1; i <= 2 * n + 1; ++i) cout << a[i] << ' '; } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const long long LINF = 1e18; const long long MOD = 1e9 + 7; template <typename T> T mul(T a, T b) { return (1LL * a * b) % MOD; } template <typename T> T add(T a, T b) { return (a + b) % MOD; } template <typename T> T sub(T a, T b) { return ((a - b) % MOD + MOD) % MOD; } template <typename T, typename... V> T mul(T t, V... v) { return mul(t, mul(v...)); } template <typename T, typename... V> T add(T t, V... v) { return add(t, add(v...)); } template <typename T, typename... V> T sub(T t, V... v) { return sub(t, sub(v...)); } int P[100003]; bool build(int x, int n, int k); bool build(int x, int x1, int n1, int k1, int x2, int n2, int k2) { P[x1] = x; P[x2] = x; build(x1, n1, k1); build(x2, n2, k2); return 1; } bool CASE1(int x, int n, int k) { if (n == 3) { build(x, x + 1, 1, 0, x + 2, 1, 0); } if (n == 5) { build(x, x + 1, 1, 0, x + 2, 3, 0); } if (n == 9) { if (k == 2) return 0; build(x, x + 1, 1, 0, x + 2, 7, k - 1); } if (n == 11) { if (k & 1) build(x, x + 1, 3, 0, x + 4, 7, k - 1); else build(x, x + 1, 1, 0, x + 2, 9, k - 1); } return 1; } bool pbt(int n) { return ((n + 1) & (-(n + 1))) == (n + 1); } bool build(int x, int n, int k) { if (n == 1) return 1; if (n % 2 == 0) return 0; if (k > n / 2 - 1) return 0; if (!pbt(n) && k == 0) return 0; if (pbt(n) && k == 1) return 0; if (n == 9 && k == 2) return 0; if (pbt(n)) { if (k) build(x, x + 1, 1, 0, x + 2, n - 2, k - 1); else build(x, x + 1, n / 2, 0, x + 1 + n / 2, n / 2, 0); } else if (n <= 11) { CASE1(x, n, k); } else if (pbt(n - 2)) { if (k == 1 || k > 2) build(x, x + 1, 1, 0, x + 2, n - 2, k - 1); else build(x, x + 1, 3, 0, x + 4, n - 4, 1); } else { if (k > 1) build(x, x + 1, 1, 0, x + 2, n - 2, k - 1); else { for (int i = 2; i <= n; i += i) { int pbt1 = i - 1; int nod = n - pbt1 - 1; if (pbt(nod)) { build(x, x + 1, pbt1, 0, x + 1 + pbt1, nod, 0); break; } if (max(pbt1, nod) < 2 * min(pbt1, nod)) { build(x, x + 1, pbt1, 0, x + 1 + pbt1, nod, 1); break; } } } } return 1; } int main() { int n, k; scanf("%d %d", &n, &k); if (!build(1, n, k)) { printf("NO\n"); } else { printf("YES\n"); for (int i = 1; i <= n; i++) printf("%d%c", P[i], (i == n) ? '\n' : ' '); } }
CPP