Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.*; import java.io.*; public class Main { static final int M = 1000000007; static FastReader in = new FastReader(); // static PrintWriter out = new PrintWriter(System.out); // static Scanner in = new Scanner(System.in); // File file = new File("input.txt"); // Scanner in = new Scanner(file); // PrintWriter out = new PrintWriter(new FileWriter("output.txt")); // Arrays.sort(arr, Comparator.comparingDouble(o -> o[0])); public static void main(String[] args) { int t = in.nextInt(); while(t-- > 0) solve(); } static void solve() { int h = in.nextInt(), c = in.nextInt(), t = in.nextInt(), k; if(h==t) System.out.println(1); else if(h+c>=2*t) System.out.println(2); else { k=(h-t)/(2*t-h-c); long l=Math.abs((k*(h+c)+h)-t*(2*k+1)); long ld=2*k+1; long rd=2*k+3; long r=Math.abs(((k+1)*(h+c)+h)-t*(2*k+3)); if(l*rd<=r*ld) { System.out.println(2*k+1); } else { System.out.println(2*k+3); } } } static void sieve(int n, boolean[] prime) { for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } static int gcd(int a, int b) { if(b==0) return a; return gcd(b, a%b); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from collections import deque import sys def input(): return sys.stdin.readline().rstrip() for _ in range(int(input())): h, c, t = list(map(int, input().split())) if (2 * t == c + h): print(2) continue base = abs(h - c) // abs(2*t - c - h) ans = 0 d = 2e18 for i in range(base - 1000, base + 1000): if i < 1: continue if i % 2 == 1 and abs((2 * t - c - h) * i + c - h) * ans < d * i: d = abs((2 * t - c - h) * i + c - h) ans = i if (ans * abs(2 * t - c - h) <= abs(2 * t * ans - (c + h) * (ans - 1) - 2 * h)): ans = 2 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, ceil from fractions import Fraction as F T = int(input()) for _ in range(T): H, C, O = map(int, input().split()) if H == C or O >= H: print(1) elif O <= (H+C)/2: print(2) else: x = F((O-C),(H-C)) N = F((x-1),(1-2*x)) low = floor(N) hi = ceil(N) lowV = F((low+1),(2*low+1)) hiV = F((hi+1),(2*hi+1)) lowD = lowV-x hiD = x-hiV if lowD <= hiD: print(low*2+1) else: print(hi*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
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; const long long INF = 1e17 + 7; const long long mod = 1e9 + 7; int main() { long long t; cin >> t; while (t--) { double h, c, t; cin >> h >> c >> t; double k = (h + c) / 2; if (t <= k) { cout << 2 << "\n"; } else if (t == h) { cout << 1 << '\n'; } else { long long a = (h - t) / (t - k); if (a % 2 != 0) a = a + 1; double x[4]; double x1 = (k * a + h) / (a + 1); x[1] = abs(t - x1); double x2 = (k * (a + 2) + h) / (a + 3); x[2] = abs(t - x2); double x0 = (k * (a - 2) + h) / (a - 1); x[0] = abs(t - x0); double x3 = (k * (a + 4) + h) / (a + 5); x[3] = abs(t - x3); if ((h - t) <= x[1] && (h - t) <= x[2] && (h - t) <= x[3] && (h - t) <= x[0]) { cout << 1 << "\n"; } else if (x[0] <= x[1] && x[0] <= x[2] && x[0] <= x[3] && a > 2) { cout << a - 1 << "\n"; } else if (x[1] <= x[0] && x[1] <= x[2] && x[1] <= x[3] && a > 0) { cout << a + 1 << "\n"; } else if (x[2] <= x[0] && x[2] <= x[1] && x[2] <= x[3]) { cout << a + 3 << "\n"; } else { cout << a + 5 << "\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; double h, c, t; inline double avg(long long m) { return (m * h + (m - 1) * c) / (2 * m - 1); } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long tc; cin >> tc; while (tc--) { cin >> h >> c >> t; if (t == h) { cout << "1\n"; continue; } if (t <= (h + c) / 2) { cout << "2\n"; continue; } long long l = 1, r = 1e9, m; while (l <= r) { m = (l + r) / 2; double temp = avg(m); if (temp > t) l = m + 1; else r = m - 1; } double close = 1E10; long long ans = l; for (long long j = max(0LL, l - 5); j <= l + 5; j++) { if (abs(avg(j) - t) < close) { close = abs(avg(j) - t); ans = j; } } 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; bool comp(const pair<int, int> &a, const pair<int, int> &b) { if (a.first == b.first) return a.second < b.second; return a.first < b.first; } bool comps(const pair<int, int> &a, const pair<int, int> &b) { if (a.second == b.second) return a.first < b.first; return a.second < b.second; } bool prime[1000005]; void primeseries(long long int n) { memset(prime, true, sizeof(prime)); for (long long int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (long long int i = p * p; i <= n; i += p) prime[i] = false; } } } struct custom_hash { size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); x ^= FIXED_RANDOM; return x ^ (x >> 16); } }; bool compmx(int a, int b) { return (a < b); } void fastio() { ios::sync_with_stdio(0); cin.tie(0); } void inout() {} long long int su(long long int n) { long long int cnt = 0; while (n) { cnt += n % 10; n /= 10; } return cnt; } long long int mod = 1E9 + 7; long long int fin(long long int a[], long long int n) { long long int sum = 0; vector<long long int> odd; for (long long int i = 0; i < n / 2; i++) { odd.push_back(a[2 * i + 1] - a[2 * i]); sum += a[2 * i]; } if (n % 2) sum += a[n - 1]; long long int mx = 0, push_back = INT_MIN; for (long long int i = 0; i < (odd).size(); i++) { if (mx < 0) { mx = 0; } mx += odd[i]; push_back = max(push_back, mx); } if (push_back > 0) { sum += push_back; } return sum; } vector<pair<long long int, long long int>> ans; void nest(long long int n, long long int x, long long int y) { if (n == 0) { ans.push_back({x, y + 1}); ans.push_back({x + 1, y}); ans.push_back({x + 1, y + 1}); } else { ans.push_back({x, y + 1}); ans.push_back({x + 1, y}); ans.push_back({x + 1, y + 1}); nest(n - 1, x + 1, y + 1); } } char a[55][55]; bool vis[55][55]; long long int n, m; void dfs(long long int x, long long int y) { if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y]) return; vis[x][y] = true; if (a[x][y] == 'G' || a[x][y] == '.') { dfs(x - 1, y); dfs(x + 1, y); dfs(x, y - 1); dfs(x, y + 1); } } bool chk(string a, string b, long long int m) { long long int cnt = 0; for (long long int i = 0; i < m; i++) { if (a[i] != b[i]) cnt++; } if (cnt <= 1) return true; else return false; } long double fi(long double a, long double b, long double x) { return ((x + 1) * a + x * b) / (2 * x + 1); } int main() { long long int t; cin >> t; while (t--) { long double a, b, t; cin >> a >> b >> t; long double m = (1.0) * (a + b) / 2; if (t <= m) cout << 2; else { long long int x = (a - t + 2 * t - (a + b) - 1) / (2 * t - (a + b)); long long int ans; if (abs(t - fi(a, b, x)) <= abs(t - fi(a, b, x + 1))) { ans = 2 * x + 1; if (abs(t - fi(a, b, x - 1)) <= abs(t - fi(a, b, x))) ans = 2 * (x - 1) + 1; } else { ans = 2 * (x + 1) + 1; if (abs(t - fi(a, b, x - 1)) <= abs(t - fi(a, b, x + 1))) ans = 2 * (x - 1) + 1; } cout << ans; } 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
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out) ; int T = sc.nextInt() ; while (T-->0) { long h = sc.nextInt() , c = sc.nextInt() , t = sc.nextInt() ; long lastUpper = h + c , lastLower = 2, ans = 2 , first = 0 , second = 0 , start = 0 , end = (int)1e9 ; while (start <= end) { long mid = start + end >> 1 ; if(h * (mid + 1) + c * mid >= t * (2 * mid + 1)) { start = mid + 1 ; first = mid ; second = mid + 1 ; } else end = mid - 1 ; } long upper = (first + 1) * h + c * first ; long lower = 2 * first + 1 ; if (Math.abs(upper - lower * t) * lastLower <Math.abs(lastUpper - lastLower * t) * lower) { lastLower = lower ; lastUpper = upper; ans = lower; } upper = (second + 1) * h + c * second ; lower = 2 * second + 1 ; if (Math.abs(upper - lower * t) * lastLower <Math.abs(lastUpper - lastLower * t) * lower) ans = lower; out.println(ans); } out.flush(); } static class Scanner { BufferedReader br ; StringTokenizer st ; Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)) ; } String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()) ; return st.nextToken() ; } int nextInt() throws Exception { return Integer.parseInt(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 if os.path.exists('/mnt/c/Users/Square/square/codeforces'): _f = iter(open('C.txt').readlines()) def input(): return next(_f) # input = lambda: sys.stdin.readline().strip() else: input = lambda: sys.stdin.readline().strip() fprint = lambda *args: print(*args, flush=True) import functools for _ in range(int(input())): h, c, t = map(int, input().split()) m = (h + c) / 2 if t <= m: print(2) continue elif t >= h: print(1) continue x = int((t - c) / (2 * t - h - c)) y = x + 1 if abs((x * h + (x - 1) * c) - t * (2 * x - 1)) * (2 * y - 1) <= abs((y * h + (y - 1) * c) - 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
from fractions import Fraction as frac import sys, os input = sys.stdin.buffer.read().split(b'\n')[::-1].pop from heapq import heappush, heappop def i(): return input() def ii(): return int(input()) def iis(): return map(int, input().split()) def liis(): return list(map(int, input().split())) def pint(x): os.write(1, str(x).ecode('ascii')), os.write(1, b'\n') def print_array(a): os.write(1, b' '.join(str(x).encode('ascii') for x in a)), os.write(1, b'\n') Test = int(input()) while True: Test -= 1 if Test < 0: break [h, c, t] = [int(x) for x in input().split()] x = h+c if h+c >= 2*t: print(2) continue def eval(k): return abs(frac(k*x + h, 2*k+1) - t); k = (h-t)//(2*t - x) k = max(k-3, 0) ans = k for i in range(0, 7): if eval(k+i) < eval(ans): ans = k+i print(2*ans + 1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h, c, t = map(int, input().split()) n = 0 ct = 0 if h == t: print(1) elif t <= (h+c)//2: print(2) else: t -= (h+c)/2 t1 = ((h-c)/2)/t x1 = int(((t1-1)//2)*2+1) x2 = int(((t1+1)//2)*2+1) if (h-c)/(x1*2)-t <= t - (h-c)/(x2*2): print(x1) else: print(x2) ''' med = (h + c)/2 last = (h+c+h) n = 3 for i in range(20): print(t-((last)/(n)), t - (last + h + c)/(n+2)) if abs(t-(last/n)) > abs(t - (last + h + c)/(n+2)): if abs(t-((last+h+h+c+c)/(n+4))) > abs(t - (last + h + c)/(n+2)): print(n+2) break else: n += 2 last += h + c if abs(t-(last/n)) < abs(t - (last + h + c)/(n+2)): if abs(t-((last+h+h+c+c)/(n+4))) > abs(t - (last + h + c)/(n+2)): print(n) break else: n += 2 last += h + c else: n += 2 last += h + c '''
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
# Template 1.0 from decimal import * import sys, re, math from collections import deque, defaultdict, Counter, OrderedDict from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd, floor from heapq import heappush, heappop, heapify, nlargest, nsmallest def STR(): return list(input()) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) def list2d(a, b, c): return [[c] * b for i in range(a)] def sortListWithIndex(listOfTuples, idx): return (sorted(listOfTuples, key=lambda x: x[idx])) def sortDictWithVal(passedDic): temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0])) toret = {} for tup in temp: toret[tup[0]] = tup[1] return toret def sortDictWithKey(passedDic): return dict(OrderedDict(sorted(passedDic.items()))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 def calc(k): return Decimal((h+c)*k+h)/Decimal(2*k+1) T = INT() while(T!=0): h,c,t = MAP() mn = (h+c)/2 ans = 2 diff = abs(mn-t) # # for k in range(0, 100): # print(((h+c)*k+h)/(2*k+1)) # l = 0 # r = 10**6+1 # # while(l<r): # mid = (l+r)//2 # temp = calc(mid) # if(temp>t): # if(abs(temp-t)<abs(mn-t)): # mn = temp # ans = 2*mid+1 # l = mid + 1 # else: # if(abs(temp-t)<abs(mn-t)): # mn = temp # ans = 2*mid+1 # r = mid # print(ans) if(h+c!=2*t): k = (t-h)/(h+c-2*t) if(k<0): ans = 2 elif(k==0): ans = 1 else: ab = calc(floor(k)) abc = calc(ceil(k)) if(abs(t-ab)<=abs(t-abc)): if(abs(t-ab)<abs(t-h)): ans = 2*floor(k)+1 else: ans = 1 else: if(abs(t-abc)<abs(t-h)): ans = 2*ceil(k) + 1 else: ans = 1 print(ans) elif(h!=t): ans = 2 print(ans) else: print(1) T-=1
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class C_Edu_Round_88 { public static long MOD = 1000000007; static int[][] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int T = in.nextInt(); boolean ok = false; for (int z = 0; z < T; z++) { int h = in.nextInt(); int c = in.nextInt(); int t = in.nextInt(); if (h == t) { out.println(1); } else if (c == t) { out.println(2); } else { long x = cal(h, c, t); if (x == -1) { out.println(2); } else { out.println((x + x - 1)); } } } out.close(); } static long cal(long a, long b, long c) { long u = c - b; long v = a - c; if (u <= v) { return -1; } long x = u / (u - v); Rate X = new Rate(x * a + b * (x - 1), (2 * x - 1)); Rate Y = new Rate((x + 1) * a + b * x, (2 * x + 1)); Rate C = new Rate(c, 1); if (C.compareTo(X) < 0) { X = X.minus(C); } else { X = C.minus(X); } if (C.compareTo(Y) < 0) { Y = Y.minus(C); } else { Y = C.minus(Y); } Rate O = new Rate(a - c, 1); if (O.compareTo(X) <= 0 && O.compareTo(Y) <= 0) { return 1; } if (X.compareTo(Y) <= 0) { return x; } return x + 1; } static class Rate implements Comparable<Rate> { long x, y; public Rate(long x, long y) { long g = gcd(x, y); this.x = x / g; this.y = y / g; } public Rate minus(Rate rate) { long X = x * rate.y - rate.x * y; long Y = y * rate.y; long g = gcd(X, Y); return new Rate(X / g, Y / g); } @Override public int compareTo(Rate rate) { long re = x * rate.y - rate.x * y; return re < 0 ? -1 : re > 0 ? 1 : 0; } } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return Integer.compare(x, o.x); } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return val * val; } else { return val * (val * a); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
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, exit import fractions input = stdin.readline def i(): return input() def ii(): return int(input()) def iis(): return map(int, input().split()) def liis(): return list(map(int, input().split())) def print_array(a): print(" ".join(map(str, a))) def eq(x): return F((h*x + c*(x-1)),(x+x-1)) def bin_ser(lb, ub, lv, uv, target): elm_mid = lb while abs(lb-ub) > 1: elm_mid = (lb+ub)//2 mid = eq(elm_mid) if mid < target: ub = elm_mid uv = mid elif mid > target: lb = elm_mid lv = mid else: break return elm_mid F = fractions.Fraction t = ii() for _ in range(t): h, c, t = iis() if t <= (h+c)/2: print(2) elif t >= h: print(1) else: i = 1 while True: i *= 2 calc = eq(i) if calc < t: mid = (bin_ser(1, i, h, calc, t)) #print("mid", mid) ops = [[mid-1, eq(mid-1)], [mid, eq(mid)], [mid+1, eq(mid+1)]] mini = float("inf") resp = float("inf") #for i in ops: # print(i[1], end=" - ") #print() for i, j in ops: if abs(t-j) < mini: mini = abs(t-j) resp = i print(resp*2-1) break ''' total = 0 count = 0 hot = True for i in range(50): if hot: total += h else: total += c count += 1 print(f"#{i}: {total/count}") hot = not hot print() '''
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h,c,t = map(int,input().split()) if(h==t):print(1) else: if(((h+c)/2) >= t):print(2) else: dif=(t-((h+c)/2));j=int(abs((c-h)//(2*dif))) if(j%2==0):j+=1 print(j-2) if(dif-(h-c)/(2*j)>=abs(dif-(h-c)/(2*(j-2)))) else print(j)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys from fractions import Fraction as f 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 y = (t - h) / ((h + c) - 2 * t) if y < 0: print(2) continue y = int(y) ans = sys.maxsize ans2 = 0 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 ans = min(ans, abs(h - t), abs(x - t)) if abs(h - t) == ans: print(1) elif abs(x - t) == ans: print(2) 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
import java.util.Scanner; public class MixingWater { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int i=0; i<n; i++) { int hot=sc.nextInt(); int cold=sc.nextInt(); int goal=sc.nextInt(); if(goal<=(hot+cold)/2) { System.out.println(2); } else if(goal>=hot) { System.out.println(1); } else { int t=(goal-hot)/(hot+cold-2*goal); t=Math.max(0, t-5); double closest=hot-goal; while((double)(hot+(hot+cold)*t)>goal*(2*t+1)) { closest=(double)(hot+(hot+cold)*t)-goal*(2*t+1); t++; } if(Math.abs((double)(hot+(hot+cold)*t)-goal*(2*t+1))*(2*t-1)<closest*(2*t+1)) { System.out.println(2*t+1); } else { System.out.println(2*t-1); } } } sc.close(); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math t = int(input()) for i in range(0, t): h, c, d = map(int, input().split(' ')) if d <= (h + c) / 2: print(2) else: ex = (h-d) // (2*d-h-c) if abs((ex * (h+c) + h) - d * (2*ex+1)) * (2*ex+3) <= abs((ex+1) * (h+c) + h - d * (2*ex+3)) * (2*ex+1): print(2*ex+1) else: print(2*ex+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 java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.ConnectException; import java.rmi.dgc.Lease; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; import java.util.Set; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; import static java.util.Comparator.*; public class Main { public static void main(String[] args) { Main main = new Main(); main.solve(); main.out.close(); } // ====================================================================== public void solve() { int Q = ni(); long h, c, t, wk; long saL, saR; int l, r, m, cnt, ll, rr; for (int i = 0; i < Q; i++) { h = ni(); c = ni(); t = ni(); if(h == t) out.println(1); else if((h + c) / 2 >= t) out.println(2); else { l = 0; r = 1000000000; while(l+1 < r) { m = (l + r) / 2; cnt = 2 * m + 1; wk = h + (h + c) * m; if(wk > t * cnt) l = m; else r = m; } ll = 2 * l + 1; wk = h + (h + c) * l; saL = Math.abs(wk - t * ll); rr = 2 * r + 1; wk = h + (h + c) * r; saR = Math.abs(wk - t * rr); out.println(saL * rr <= saR * ll ? ll : rr); } } } // ------------------------------------------ // ラむブラγƒͺ // ------------------------------------------ // Print private PrintWriter out = new PrintWriter(System.out); // Scanner private FastScanner scan = new FastScanner(); int ni() { return scan.nextInt(); } int[] ni(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } int[][] ni(int y, int x) { int[][] a = new int[y][x]; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { a[i][j] = ni(); } } return a; } long nl() { return scan.nextLong(); } long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } long[][] nl(int y, int x) { long[][] a = new long[y][x]; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { a[i][j] = nl(); } } return a; } String ns() { return scan.next(); } String[] ns(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = ns(); } return a; } String[][] ns(int y, int x) { String[][] a = new String[y][x]; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { a[i][j] = ns(); } } return a; } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.*; import java.io.*; public class C { public static void main(String[] args) throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(f.readLine()); while(t-->0){ StringTokenizer st = new StringTokenizer(f.readLine()); long h = Long.parseLong(st.nextToken()); long c = Long.parseLong(st.nextToken()); long temp = Long.parseLong(st.nextToken()); long avg = h+c; long val1 = h; long ans = 0; long minDiff = 0; if(Math.abs(avg-2*temp) >= 2*Math.abs(val1-temp)){ ans = 1; minDiff = Math.abs(val1-temp); }else{ ans = 2; minDiff = Math.abs(avg-2*temp); } long lb = 1; long rb = 1000000000; while(lb < rb-5){ long mid = (lb+rb)/2; if((h*mid + c*(mid-1)) <= temp*(2*mid-1)){ rb = mid; }else{ lb = mid; } } for(long i = lb; i <= rb; i++){ long val = Math.abs((h*i + c*(i-1)) - temp*(2*i-1)); if(val*ans < minDiff*(2*i-1)){ minDiff = val; ans = 2*i-1; } } out.println((long)ans); } out.close(); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; void solve() { int h, c, t; cin >> h >> c >> t; if ((h + c) / 2 >= t) { cout << 2 << '\n'; return; } int a = h - t; int b = 2 * t - h - c; 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'; } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) solve(); }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; const long long Mod = 1e9 + 7; const int N = 1000; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int numCases = 1; cin >> numCases; for (int caseNo = 1; caseNo <= numCases; caseNo++) { long long h, c, t; cin >> h >> c >> t; if (h == t) { cout << 1 << endl; } else if (abs(h - t) >= abs(c - t)) { cout << 2 << endl; } else { int l = 0, r = Mod, ans = 0; while (l < r) { int mid = l + (r - l) / 2; if (mid % 2 == 0) mid++; long long th = ((mid + 1) / 2) * h; long long tc = (mid / 2) * c; if ((1.0 * (th + tc) / mid) > t) { l = mid + 1; ans = mid; } else { r = mid - 1; } } long long th1 = ((ans + 1) / 2) * h; long long tc1 = (ans / 2) * c; long long th2 = ((ans + 3) / 2) * h; long long tc2 = ((ans + 2) / 2) * c; if (abs(1.0 * (th1 + tc1) / ans - t) > abs(1.0 * (th2 + tc2) / (ans + 2) - t)) ans += 2; cout << ans << endl; } } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
# cook your dish here import math def bs(H, C, res): lo = 0 hi = 10**12 ans = 0 while(lo<=hi): mid = (lo + hi)//2 if (H*(mid+1) + ((mid)*C)) >= res*((2*mid) + 1): lo = mid + 1 ans = mid else: hi = mid - 1 return ans def main(): for q in range(int(input())): H,C,T = map(int,input().split()) avg = (H+C)/2 if T <= avg: print('2') else: ans = bs(H,C,T) ab1 = abs(T*((2*ans)+1) - H*(ans+1) - C*(ans)) ab2 = abs(T*((2*ans)+3) - H*(ans+2) - C*(ans+1)) ab1 /= (2*ans + 1) ab2 /= (2*ans+3) if ab1<=ab2: print(((2*ans)+1)) else: print(((2*ans)+3)) #973303 519683 866255 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 bisect import heapq import math import collections import sys import copy from functools import reduce import decimal from io import BytesIO, IOBase import os import itertools sys.setrecursionlimit(10 ** 9) decimal.getcontext().rounding = decimal.ROUND_HALF_UP graphDict = collections.defaultdict queue = collections.deque ################## Graphs ################### class Graphs: def __init__(self): self.graph = graphDict(set) def add_edge(self, u, v): self.graph[u].add(v) self.graph[v].add(u) def delEdge(self, u, v): for ii in range(len(self.graph[u])): if self.graph[u][ii] == v: self[u].remove(v) break for ii in range(len(self[v])): if self[v][ii] == u: self[v].pop(i) break def dfs_utility(self, nodes, visited_nodes): visited_nodes.add(nodes) for neighbour in self.graph[nodes]: if neighbour not in visited_nodes: self.dfs_utility(neighbour, visited_nodes) def dfs(self): count = 0 ans = [] Visited = set() for i in range(1, n + 1): if i not in Visited: count += 1 ans.append(i) self.dfs_utility(i, Visited) return count, ans def bfs(self, node): visited = set() if node not in visited: queue.append(node) visited.add(node) while queue: parent = queue.popleft() print(parent) for item in self.graph[parent]: if item not in visited: queue.append(item) visited.add(item) ################### Tree Implementaion ############## class Tree: def __init__(self, data): self.data = data self.left = None self.right = None def inorder(node, lis): if node: inorder(node.left, lis) lis.append(node.data) inorder(node.right, lis) return lis def leaf_node_sum(root): if root is None: return 0 if root.left is None and root.right is None: return root.data return leaf_node_sum(root.left) + leaf_node_sum(root.right) def hight(root): if root is None: return -1 if root.left is None and root.right is None: return 0 return max(hight(root.left), hight(root.right)) + 1 ################################################# def rounding(n): return int(decimal.Decimal(f'{n}').to_integral_value()) def factors(n): return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))) ################################ <fast I/O> ########################################### 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, **kwargs): 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) #############################################<I/O Region >############################################## def inp(): return sys.stdin.readline().strip() def map_inp(v_type): return map(v_type, inp().split()) def list_inp(v_type): return list(map_inp(v_type)) ######################################## Solution #################################### for _ in range(int(inp())): h, c, t = map_inp(int) if (h + c) / 2 == t: print(2) else: temp = (t - h) // (h + c - 2 * t) if temp < 0: print(2) else: ans = [] for i in range(temp, temp + 3): for j in range(temp, temp + 3): if i + j == 0: continue if i < j: continue else: ans.append([abs(t - (decimal.Decimal(i * h + j * c) / (i + j))), i, j]) ans.sort() print(ans[0][1] + ans[0][2])
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author EigenFunk */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); CMixingWater solver = new CMixingWater(); solver.solve(1, in, out); out.close(); } static class CMixingWater { public void solve(int testNumber, FastScanner in, PrintWriter out) { int rr = in.nextInt(); for (int i = 0; i < rr; i++) { long h = in.nextInt(); long c = in.nextInt(); long tg = in.nextInt(); if (tg == h) { out.println(1); } else if (2 * tg <= c + h) { out.println(2); } else { double m = (h + c) / 2d; double tm = tg - m; int counter = (int) Math.ceil((h - c) / (2d * tm)); counter = (counter % 2 != 0) ? counter : counter + 1; double toUpper = Math.abs((h - c) / ((double) (2 * (counter - 2))) - tm); double toLower = Math.abs((h - c) / ((double) (2 * counter)) - tm); if (toUpper <= toLower) { out.println(counter - 2); } else { out.println(counter); } } } } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public FastScanner(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(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
#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 (int i = steps; 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
for case in range(int(input())): a = input().split(' ') h = int(a[0]) c = int(a[1]) t = int(a[2]) avg = (h + c) / 2 if t < avg : ans = 2 elif t == avg: ans = 2 elif t == h: ans = 1 elif h > t > avg: diff = t - avg v = (h-avg) // diff if v % 2 == 0: v1 = (h-avg)/(v+1) v2 = (h-avg)/(v-1) d1 = abs(diff- v1) d2 = abs(diff - v2) if d1 >= d2: ans = v-1 else: ans = v + 1 else: v1 = (h-avg)/(v-2) v2 = (h-avg)/v v3 = (h-avg)/(v+2) d1 = abs(diff-v1) d2 = abs(diff-v2) d3 = abs(diff-v3) if d1 <= d2 and d1 <= d3: ans = v-2 elif d2 <= d1 and d2 <= d3: ans = v else: ans = v+2 else: ans = 1 print(int(ans))
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys import decimal as dc dc.getcontext().prec = 18 readline = sys.stdin.readline readlines = sys.stdin.readlines ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def calc(x, t, h, c): return abs(dc.Decimal(t) - dc.Decimal(x * h + (x-1) * c) / dc.Decimal(x * 2 - 1)) def solve(): h, c, t = nm() if t * 2 <= h + c: print(2) return x = (t - c) // (2 * t - h - c) cmax = abs(dc.Decimal(t) - dc.Decimal(h+c)/dc.Decimal(2)) cidx = -1 for i in range(x+3, max(x-4, 0), -1): if cmax >= calc(i, t, h, c): cmax = calc(i, t, h, c) cidx = i print(cidx*2 - 1 if cidx >= 0 else 2) return # solve() T = ni() for _ 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
#include <bits/stdc++.h> using namespace std; void solve() { double a, b, c; cin >> a >> b >> c; double x = (a + b) - c - c; double y = -c + b; x = abs(x); y = abs(y); double hihi = abs(c - ((a + b) / 2.0)); double haha = abs(c - (max(ceil(y / x), 1.0) * a + (max(ceil(y / x), 1.0) - 1) * b) / (max(ceil(y / x), 1.0) * 2.0 - 1.0)); double ngaymaisekhac = abs(c - (max(floor(y / x), 1.0) * a + (max(floor(y / x), 1.0) - 1) * b) / (max(floor(y / x), 1.0) * 2.0 - 1.0)); if ((hihi < ngaymaisekhac && hihi < haha) || hihi == 0) { printf("2"); return; } if (haha < ngaymaisekhac) { printf("%.0lf", max(ceil(y / x), 1.0) * 2 - 1); } else { printf("%.0lf", max(floor(y / x), 1.0) * 2 - 1); } } int main() { int q; cin >> q; while (q--) { solve(); cout << endl; } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from fractions import Fraction T = int(input()) def TMP(h, c, nbH): return c + Fraction(nbH, 2*nbH-1) * (h-c) for _ in range(T): h, c, t = [int(_) for _ in input().split()] if t <= (h+c)//2: print(2) continue L, R = 1, 10**9 a = 0 while L <= R: m = (L+R)//2 tmp = TMP(h, c, m) if tmp >= t: L = m+1 a = m else: R = m-1 b = a+1 if abs(TMP(h, c, a) - t) <= abs(TMP(h, c, b) - t): print(a*2-1) else: print(b*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
#include <bits/stdc++.h> using namespace std; const int inf = 0x7FFFFFFF; const long long mod = (0 ? 1000000007 : 998244353); const double eps = 1e-7; const long long llinf = 4223372036854775807; inline void out(long long a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; } void work() { double h, c, t; cin >> h >> c >> t; if (t <= (h + c) / 2) { cout << 2 << endl; return; } double dis = t - (h + c) / 2; long long now = 1; double chu = h - (h + c) / 2; long long b = 0; long long e = inf; while (e > b) { double mid = (e + b) / 2; if (dis > chu / (mid * 2 + 1)) { e = mid; } else b = mid + 1; } now = b * 2 + 1; if (now == 1) cout << now << endl; else if (abs(dis - chu / now) < abs(dis - chu / (now - 2))) { cout << now << endl; } else cout << now - 2 << endl; } signed main() { std::ios::sync_with_stdio(false); cin.tie(NULL); long long t = 1; cin >> t; while (t--) { work(); } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; void solve() { float h, c, t; cin >> h >> c >> t; if (t >= h) { cout << "1\n"; return; } else if (2 * t <= (h + c)) { cout << "2\n"; return; } else { int val = (h - t) / (2 * t - h - c); float t1 = ((val + 1) * h + val * c) / (2 * val + 1); val++; float t2 = ((val + 1) * h + val * c) / (2 * val + 1); if (abs(t - t1) <= abs(t - t2)) { val--; } cout << 2 * val + 1 << "\n"; } } int main() { int t; t = 1; scanf("%d", &t); while (t--) { solve(); } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); // BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int T=1; T=sc.nextInt(); // int t=Integer.parseInt(br.readLine()); int q=0; while(--T>=0){ q++; long h=sc.nextLong(); long c=sc.nextLong(); long t=sc.nextLong(); long l=1,u=10000000; long ln=Math.abs(t*2-(h+c)); long ld=2; long ans=2; while(l<=u){ long mid=(l+u)/2; // System.out.println(mid); long d=2*mid-1; long n=Math.abs(mid*(h+c)-c-d*t); long z=mid*(h+c)-c; long t1=ln*d; long t2=n*ld; // System.out.println(n+" "+d+" "+ln+" "+ld+" "+t1+" "+t2); if(t2<t1){ ans=d; ln=n; ld=d; } else if(t2==t1){ ans=Math.min(ans,d); } if(d*t<z){ l=mid+1; } else if(d*t==z){ ans=d; break; } else{ u=mid-1; } } System.out.println(ans); } } }
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.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.math.BigInteger; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author El Mehdi ASSALI */ 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); CMixingWater solver = new CMixingWater(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class CMixingWater { public void solve(int testNumber, InputReader in, PrintWriter out) { long h = in.nextInt(), c = in.nextInt(), t = in.nextInt(); if (2 * t == h + c) { out.println(2); } else { long n = Math.max(0, (h - t) / (2 * t - h - c)); Fraction target = new Fraction(t); Fraction temp1 = new Fraction(n * (h + c) + h, 2 * n + 1); Fraction temp2 = new Fraction((n + 1) * (h + c) + h, 2 * (n + 1) + 1); Fraction temp3 = new Fraction(h + c, 2); Fraction diff1 = temp1.sub(target).abs(); Fraction diff2 = temp2.sub(target).abs(); Fraction diff3 = temp3.sub(target).abs(); if (diff1.compareTo(diff2) <= 0) { if (diff1.compareTo(diff3) < 0) { out.println(2 * n + 1); } else { out.println(2); } } else { if (diff2.compareTo(diff3) < 0) { out.println(2 * (n + 1) + 1); } else { out.println(2); } } } } } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } static class Fraction implements Comparable<Fraction> { private final BigInteger x; private final BigInteger y; public Fraction(long x) { this(x, 1); } public Fraction(long x, long y) { this(new BigInteger(String.valueOf(x)), new BigInteger(String.valueOf(y))); } private Fraction(BigInteger x, BigInteger y) { this.x = x; this.y = y; } public Fraction sub(Fraction f) { return new Fraction(x.multiply(f.y).subtract(y.multiply(f.x)), y.multiply(f.y)); } public Fraction abs() { return new Fraction(x.abs(), y.abs()); } public int compareTo(Fraction f) { return x.multiply(f.y).compareTo(y.multiply(f.x)); } public String toString() { return x + " / " + y; } } }
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
t = int(input()) for i in range(t): h,c,t = map(int,input().split()) x = t - (h+c)/2 if x <= 0: print(2) else: n = (h-c)//(2*t - h - c) if n%2 == 0: if abs(x - (h-c)/(2*(n+1))) > abs(x - (h-c)/(2*(n+3))): print(n+3) else: print(n+1) else: if abs(x - (h-c)/(2*(n))) > abs(x - (h-c)/(2*(n+2))): print(n+2) else: 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
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long tst, h, c, t; cin >> tst; while (tst--) { cin >> h >> c >> t; if (t <= (h + c) / 2) cout << 2 << '\n'; else { long long t1 = (long long)ceil((h - t) / ((2 * t - h - c) * 1.0)); float d1 = abs((h * t1 + c * (t1 - 1)) / ((2 * t1 - 1) * 1.0) - t), d2 = abs((h * (t1 + 1) + c * t1) / ((2 * t1 + 1) * 1.0) - t); if (d1 <= d2) cout << 2 * t1 - 1 << '\n'; else cout << 2 * t1 + 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; int main() { int t; cin >> t; while (t--) { int h, c, t; cin >> h >> c >> t; long long res1 = 0, res2 = 0; if (h + c - 2 * t >= 0) cout << 2 << endl; else { int a = h - t; int b = 2 * t - c - h; int k = 2 * (a / b) + 1; res1 = abs(k / 2 * 1ll * c + (k + 1) / 2 * 1ll * h - t * 1ll * k); res2 = abs((k + 2) / 2 * 1ll * c + (k + 3) / 2 * 1ll * h - t * 1ll * (k + 2)); if (res1 * (k + 2) <= res2 * k) cout << k << endl; else cout << k + 2 << endl; } } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.*; import java.lang.*; import java.io.*; import java.awt.Point; // U KNOW THAT IF THIS DAY WILL BE URS THEN NO ONE CAN DEFEAT U HERE................ // ASCII = 48 + i ;// 2^28 = 268,435,456 > 2* 10^8 // log 10 base 2 = 3.3219 // odd:: (x^2+1)/2 , (x^2-1)/2 ; x>=3// even:: (x^2/4)+1 ,(x^2/4)-1 x >=4 // FOR ANY ODD NO N : N,N-1,N-2 //ALL ARE PAIRWISE COPRIME //THEIR COMBINED LCM IS PRODUCT OF ALL THESE NOS // two consecutive odds are always coprime to each other // two consecutive even have always gcd = 2 ; public class Main { // static int[] arr = new int[100002] ; // static int[] dp = new int[100002] ; static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out); } 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; } } //////////////////////////////////////////////////////////////////////////////////// public static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } ///////////////////////////////////////////////////////////////////////////////////////// public static int sumOfDigits(long n) { if( n< 0)return -1 ; int sum = 0; while( n > 0) { sum = sum + (int)( n %10) ; n /= 10 ; } return sum ; } ////////////////////////////////////////////////////////////////////////////////////////////////// public static long arraySum(int[] arr , int start , int end) { long ans = 0 ; for(int i = start ; i <= end ; i++)ans += arr[i] ; return ans ; } ///////////////////////////////////////////////////////////////////////////////// public static int mod(int x) { if(x <0)return -1*x ; else return x ; } public static long mod(long x) { if(x <0)return -1*x ; else return x ; } //////////////////////////////////////////////////////////////////////////////// public static void swapArray(int[] arr , int start , int end) { while(start < end) { int temp = arr[start] ; arr[start] = arr[end]; arr[end] = temp; start++ ;end-- ; } } ////////////////////////////////////////////////////////////////////////////////// public static int[][] rotate(int[][] input){ int n =input.length; int m = input[0].length ; int[][] output = new int [m][n]; for (int i=0; i<n; i++) for (int j=0;j<m; j++) output [j][n-1-i] = input[i][j]; return output; } /////////////////////////////////////////////////////////////////////////////////////////////////////// public static int countBits(long n) { int count = 0; while (n != 0) { count++; n = (n) >> (1L) ; } return count; } /////////////////////////////////////////// //////////////////////////////////////////////// public static boolean isPowerOfTwo(long n) { if(n==0) return false; if(((n ) & (n-1)) == 0 ) return true ; else return false ; } ///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// public static String reverse(String input) { StringBuilder str = new StringBuilder("") ; for(int i =input.length()-1 ; i >= 0 ; i-- ) { str.append(input.charAt(i)); } return str.toString() ; } /////////////////////////////////////////////////////////////////////////////////////////// public static boolean sameParity(long a ,long b ) { long x = a% 2L; long y = b%2L ; if(x==y)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static boolean isPossibleTriangle(int a ,int b , int c) { if( a + b > c && c+b > a && a +c > b)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////// static long xnor(long num1, long num2) { if (num1 < num2) { long temp = num1; num1 = num2; num2 = temp; } num1 = togglebit(num1); return num1 ^ num2; } static long togglebit(long n) { if (n == 0) return 1; long i = n; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return i ^ n; } /////////////////////////////////////////////////////////////////////////////////////////////// public static int xorOfFirstN(int n) { if( n % 4 ==0)return n ; else if( n % 4 == 1)return 1 ; else if( n % 4 == 2)return n+1 ; else return 0 ; } ////////////////////////////////////////////////////////////////////////////////////////////// public static int gcd(int a, int b ) { if(b==0)return a ; else return gcd(b,a%b) ; } public static long gcd(long a, long b ) { if(b==0)return a ; else return gcd(b,a%b) ; } //////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c , int d ) { int temp = lcm(a,b , c) ; int ans = lcm(temp ,d ) ; return ans ; } /////////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c ) { int temp = lcm(a,b) ; int ans = lcm(temp ,c) ; return ans ; } //////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a , int b ) { int gc = gcd(a,b); return (a*b)/gc ; } public static long lcm(long a , long b ) { long gc = gcd(a,b); return (a*b)/gc ; } /////////////////////////////////////////////////////////////////////////////////////////// static boolean isPrime(long n) { if(n==1) { return false ; } boolean ans = true ; for(long i = 2L; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } static boolean isPrime(int n) { if(n==1) { return false ; } boolean ans = true ; for(int i = 2; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } /////////////////////////////////////////////////////////////////////////// static int sieve = 1000000 ; static boolean[] prime = new boolean[sieve + 1] ; public static void sieveOfEratosthenes() { // FALSE == prime // TRUE == COMPOSITE // FALSE== 1 // time complexity = 0(NlogLogN)== o(N) // gives prime nos bw 1 to N for(int i = 4; i<= sieve ; i++) { prime[i] = true ; i++ ; } for(int p = 3; p*p <= sieve; p++) { if(prime[p] == false) { for(int i = p*p; i <= sieve; i += p) prime[i] = true; } p++ ; } } /////////////////////////////////////////////////////////////////////////////////// public static void sortD(int[] arr , int s , int e) { sort(arr ,s , e) ; int i =s ; int j = e ; while( i < j) { int temp = arr[i] ; arr[i] =arr[j] ; arr[j] = temp ; i++ ; j-- ; } return ; } ///////////////////////////////////////////////////////////////////////////////////////// public static long countSubarraysSumToK(long[] arr ,long sum ) { HashMap<Long,Long> map = new HashMap<>() ; int n = arr.length ; long prefixsum = 0 ; long count = 0L ; for(int i = 0; i < n ; i++) { prefixsum = prefixsum + arr[i] ; if(sum == prefixsum)count = count+1 ; if(map.containsKey(prefixsum -sum)) { count = count + map.get(prefixsum -sum) ; } if(map.containsKey(prefixsum )) { map.put(prefixsum , map.get(prefixsum) +1 ); } else{ map.put(prefixsum , 1L ); } } return count ; } /////////////////////////////////////////////////////////////////////////////////////////////// // KMP ALGORITHM : TIME COMPL:O(N+M) // FINDS THE OCCURENCES OF PATTERN AS A SUBSTRING IN STRING //RETURN THE ARRAYLIST OF INDEXES // IF SIZE OF LIST IS ZERO MEANS PATTERN IS NOT PRESENT IN STRING public static ArrayList<Integer> kmpAlgorithm(String str , String pat) { ArrayList<Integer> list =new ArrayList<>(); int n = str.length() ; int m = pat.length() ; String q = pat + "#" + str ; int[] lps =new int[n+m+1] ; longestPefixSuffix(lps, q,(n+m+1)) ; for(int i =m+1 ; i < (n+m+1) ; i++ ) { if(lps[i] == m) { list.add(i-2*m) ; } } return list ; } public static void longestPefixSuffix(int[] lps ,String str , int n) { lps[0] = 0 ; for(int i = 1 ; i<= n-1; i++) { int l = lps[i-1] ; while( l > 0 && str.charAt(i) != str.charAt(l)) { l = lps[l-1] ; } if(str.charAt(i) == str.charAt(l)) { l++ ; } lps[i] = l ; } } /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// // CALCULATE TOTIENT Fn FOR ALL VALUES FROM 1 TO n // TOTIENT(N) = count of nos less than n and grater than 1 whose gcd with n is 1 // or n and the no will be coprime in nature //time : O(n*(log(logn))) public static void eulerTotientFunction(int[] arr ,int n ) { for(int i = 1; i <= n ;i++)arr[i] =i ; for(int i= 2 ; i<= n ;i++) { if(arr[i] == i) { arr[i] =i-1 ; for(int j =2*i ; j<= n ; j+= i ) { arr[j] = (arr[j]*(i-1))/i ; } } } return ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; int j=1; for(;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } /////////////////////////////////////////////////////////////////////////////////////////// public static ArrayList<Integer> allFactors(int n) { ArrayList<Integer> list = new ArrayList<>() ; for(int i = 1; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } public static ArrayList<Long> allFactors(long n) { ArrayList<Long> list = new ArrayList<>() ; for(long i = 1L; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } //////////////////////////////////////////////////////////////////////////////////////////////////// static final int MAXN = 10000001; static int spf[] = new int[MAXN]; static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) spf[i] = i; for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } } // The above code works well for n upto the order of 10^7. // Beyond this we will face memory issues. // Time Complexity: The precomputation for smallest prime factor is done in O(n log log n) // using sieve. // Where as in the calculation step we are dividing the number every time by // the smallest prime number till it becomes 1. // So, let’s consider a worst case in which every time the SPF is 2 . // Therefore will have log n division steps. // Hence, We can say that our Time Complexity will be O(log n) in worst case. static Vector<Integer> getFactorization(int x) { Vector<Integer> ret = new Vector<>(); while (x != 1) { ret.add(spf[x]); x = x / spf[x]; } return ret; } ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; //Copy data to temp arrays 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]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array 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++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() public static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays 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]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array 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++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } ///////////////////////////////////////////////////////////////////////////////////////// public static long knapsack(int[] weight,long value[],int maxWeight){ int n= value.length ; //dp[i] stores the profit with KnapSack capacity "i" long []dp = new long[maxWeight+1]; //initially profit with 0 to W KnapSack capacity is 0 Arrays.fill(dp, 0); // iterate through all items for(int i=0; i < n; i++) //traverse dp array from right to left for(int j = maxWeight; j >= weight[i]; j--) dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]); /*above line finds out maximum of dp[j](excluding ith element value) and val[i] + dp[j-wt[i]] (including ith element value and the profit with "KnapSack capacity - ith element weight") */ return dp[maxWeight]; } /////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// // to return max sum of any subarray in given array public static long kadanesAlgorithm(long[] arr) { if(arr.length == 0)return 0 ; long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long kadanesAlgorithm(int[] arr) { if(arr.length == 0)return 0 ; long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } /////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// public static long binarySerachGreater(int[] arr , int start , int end , int val) { // fing total no of elements strictly grater than val in sorted array arr if(start > end)return 0 ; //Base case int mid = (start + end)/2 ; if(arr[mid] <=val) { return binarySerachGreater(arr,mid+1, end ,val) ; } else{ return binarySerachGreater(arr,start , mid -1,val) + end-mid+1 ; } } ////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //TO GENERATE ALL(DUPLICATE ALSO EXIST) PERMUTATIONS OF A STRING // JUST CALL generatePermutation( str, start, end) start :inclusive ,end : exclusive //Function for swapping the characters at position I with character at position j public static String swapString(String a, int i, int j) { char[] b =a.toCharArray(); char ch; ch = b[i]; b[i] = b[j]; b[j] = ch; return String.valueOf(b); } //Function for generating different permutations of the string public static void generatePermutation(String str, int start, int end) { //Prints the permutations if (start == end-1) System.out.println(str); else { for (int i = start; i < end; i++) { //Swapping the string by fixing a character str = swapString(str,start,i); //Recursively calling function generatePermutation() for rest of the characters generatePermutation(str,start+1,end); //Backtracking and swapping the characters again. str = swapString(str,start,i); } } } //////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// public static long factMod(long n, long mod) { if (n <= 1) return 1; long ans = 1; for (int i = 1; i <= n; i++) { ans = (ans * i) % mod; } return ans; } ///////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static long power(int x ,int n) { //time comp : o(logn) if(n==0)return 1 ; if(n==1)return x; long ans =1L ; while(n>0) { if(n % 2 ==1) { ans = ans *x ; } n /= 2 ; x = x*x ; } return ans ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static long powerMod(long x, long n, long mod) { //time comp : o(logn) if(n==0)return 1L ; if(n==1)return x; long ans = 1; while (n > 0) { if (n % 2 == 1) ans = (ans * x) % mod; x = (x * x) % mod; n /= 2; } return ans; } ////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// /* lowerBound - finds largest element equal or less than value paased upperBound - finds smallest element equal or more than value passed if not present return -1; */ public static long lowerBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static int lowerBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static long upperBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } public static int upperBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } ////////////////////////////////////////////////////////////////////////////////////////// public static void printArray(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printArrayln(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printLArray(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printLArrayln(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printtwodArray(int[][] ans) { for(int i = 0; i< ans.length ; i++) { for(int j = 0 ; j < ans[0].length ; j++)out.print(ans[i][j] +" "); out.println() ; } out.println() ; } static long modPow(long a, long x, long p) { //calculates a^x mod p in logarithmic time. long res = 1; while(x > 0) { if( x % 2 != 0) { res = (res * a) % p; } a = (a * a) % p; x /= 2; } return res; } static long modInverse(long a, long p) { //calculates the modular multiplicative of a mod m. //(assuming p is prime). return modPow(a, p-2, p); } static long modBinomial(long n, long k, long p) { // calculates C(n,k) mod p (assuming p is prime). long numerator = 1; // n * (n-1) * ... * (n-k+1) for (int i=0; i<k; i++) { numerator = (numerator * (n-i) ) % p; } long denominator = 1; // k! for (int i=1; i<=k; i++) { denominator = (denominator * i) % p; } // numerator / denominator mod p. return ( numerator* modInverse(denominator,p) ) % p; } static void update(int val , long[] bit ,int n) { for( ; val <= n ; val += (val &(-val)) ) { bit[val]++ ; } } static long query(int val , long[] bit , int n) { long ans = 0L; for( ; val >=1 ; val-=(val&(-val)) )ans += bit[val]; return ans ; } static int countSetBits(long n) { int count = 0; while (n > 0) { n = (n) & (n - 1L); count++; } return count; } ///////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// // static ArrayList<Integer>[] tree ; // static long[] child; // static int mod= 1000000007 ; // static int[][] pre = new int[3001][3001]; // static int[][] suf = new int[3001][3001] ; //program to calculate noof nodes in subtree for every vertex including itself // static void dfs(int sv) // { // child[sv] = 1L; // for(Integer x : tree[sv]) // { // if(child[x] == 0) // { // dfs(x) ; // child[sv] += child[x] ; // } // } // } static long factorial(long a) { if(a== 0L || a==1L)return 1L ; return a*factorial(a-1L) ; } ////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// public static void solve() { FastReader scn = new FastReader() ; //Scanner scn = new Scanner(System.in); //int[] store = {2 ,3, 5 , 7 ,11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 } ; // product of first 11 prime nos is greater than 10 ^ 12; //ArrayList<Integer> arr[] = new ArrayList[n] ; ArrayList<Integer> list = new ArrayList<>() ; ArrayList<Long> lista = new ArrayList<>() ; ArrayList<Long> listb = new ArrayList<>() ; // ArrayList<Integer> lista = new ArrayList<>() ; // ArrayList<Integer> listb = new ArrayList<>() ; //ArrayList<String> lists = new ArrayList<>() ; HashMap<Integer,Integer> map = new HashMap<>() ; //HashMap<Long,Long> map = new HashMap<>() ; HashMap<Integer,Integer> map1 = new HashMap<>() ; HashMap<Integer,Integer> map2 = new HashMap<>() ; //HashMap<String,Integer> maps = new HashMap<>() ; //HashMap<Integer,Boolean> mapb = new HashMap<>() ; //HashMap<Point,Integer> point = new HashMap<>() ; Set<Integer> set = new HashSet<>() ; Set<Integer> setx = new HashSet<>() ; Set<Integer> sety = new HashSet<>() ; StringBuilder sb =new StringBuilder("") ; //Collections.sort(list); //if(map.containsKey(arr[i]))map.put(arr[i] , map.get(arr[i]) +1 ) ; //else map.put(arr[i],1) ; // if(map.containsKey(temp))map.put(temp , map.get(temp) +1 ) ; // else map.put(temp,1) ; //int bit =Integer.bitCount(n); // gives total no of set bits in n; // Arrays.sort(arr, new Comparator<Pair>() { // @Override // public int compare(Pair a, Pair b) { // if (a.first != b.first) { // return a.first - b.first; // for increasing order of first // } // return a.second - b.second ; //if first is same then sort on second basis // } // }); int testcase = 1; testcase = scn.nextInt() ; for(int testcases =1 ; testcases <= testcase ;testcases++) { //if(map.containsKey(arr[i]))map.put(arr[i],map.get(arr[i])+1) ;else map.put(arr[i],1) ; //if(map.containsKey(temp))map.put(temp,map.get(temp)+1) ;else map.put(temp,1) ; // tree = new ArrayList[n] ; // child = new long[n] ; // for(int i = 0; i< n; i++) // { // tree[i] = new ArrayList<Integer>(); // } long h = scn.nextLong() ; long c = scn.nextLong() ; long t = scn.nextLong() ; if( h ==c)out.println(1) ; else if((h+c-(2*t) >= 0))out.println(2) ; else{ long x = ((h-t)/(2*t-h-c)) ; // out.println(x) ; long val1 = (h*(x+1) + c*(x))*((2*x)+3) -t*(2*x+1)*(2*x+3) ; long val2 = (h*(x+2) + c*(x+1))*((2*x)+1) -t*(2*x+1)*(2*x+3) ; // out.println(val1+" " +val2) ; if(val1 < 0 )val1 = -1*val1 ; if(val2 < 0 )val2 = -1*val2 ; if(val1 <= val2)out.println( (2*x)+1 ) ; else out.println( (2*x)+3 ) ; } set.clear() ; sb.delete(0 , sb.length()) ; list.clear() ;lista.clear() ;listb.clear() ; map.clear() ; map1.clear() ; map2.clear() ; setx.clear() ;sety.clear() ; } // test case end loop out.flush() ; } // solve fn ends public static void main (String[] args) throws java.lang.Exception { solve() ; } } class Pair { int first ; int second ; @Override public String toString() { String ans = "" ; ans += this.first ; ans += " "; ans += this.second ; return ans ; } }
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.*; public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while(test-->0) { long h = sc.nextLong(); long c = sc.nextLong(); long t = sc.nextLong(); //double min = h-t; if(t==h){ System.out.println(1); } else if((h+c) >= 2*t){ System.out.println(2); } else{ long x = (t-c)/(2*t-h-c); long y = x+1; // double exp1 = ((x * h) + (x - 1) * c) / (1.0 * (2 * x - 1)); // double exp2 = ((y * h) + (y - 1) * c) / (1.0 * (2 * y - 1)); double e1 = Math.abs((((c+h)*(x-1))+h)-(2*x-1)*t)/(2*x-1.0); double e2 = Math.abs((((c+h)*(y-1))+h)-(2*y-1)*t)/(2*y-1.0); // double e1 = Math.abs(exp1 - t*1.0); // double e2 = Math.abs(exp2 - t*1.0); // System.out.println(e1+ " "+e2); if(e1 <= e2){ System.out.println(2*x-1); } else{ System.out.println(2*y-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
from decimal import * T = int(input()) def ctemp(x): return (h*(x+1)+c*x)/(2*x+1) def ctemp2(x): return (h*(x+1)+c*x)/(2*x+1) for _ in range(T): h,c,t = map(int,input().split()) now = abs((h+c)/2-t) if (h+c)/2 >= t: print(2) else: l = 0 r = h while r > l + 1: m = (r+l)//2 temp = ctemp(m) if temp > t: l = m else: r = m h = Decimal(h) c = Decimal(c) t = Decimal(t) temp1 = ctemp2(l) temp2 = ctemp(r) if abs(temp1-t) <= abs(temp2-t): print(2*l+1) else: print(2*r+1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from sys import stdin, gettrace from fractions import Fraction if not gettrace(): def input(): return next(stdin)[:-1] # def input(): # return stdin.buffer.readline() def main(): def solve(): h,c,t = map(int, input().split()) if t <= (h+c)/2: print(2) return if t >= h: print(1) return m = (h-t)//(2*t - h - c) t1 = Fraction((m+1) * h + m*c,(2*m+1)) t2 = Fraction((m+2) * h + (m+1)*c,(2*m+3)) if abs(t1 -t) <= abs(t2 -t): print(2*m+1) else: print(2*m+3) q = int(input()) for _ in range(q): 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 main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { double h, c, x; cin >> h >> c >> x; double lw = (h + c) / 2.0; if (x <= lw) { cout << 2 << '\n'; continue; } int l = -1, r = h; int mn = 0; double best = 100000000.0; while (r - l > 1) { double mid = (l + r) / 2; double mde = (mid * (h + c) + h) / (2 * mid + 1); if (abs(mde - x) < best) { best = abs(mde - x); mn = mid * 2 + 1; } else if (abs(mde - x) == best && mn > mid * 2 + 1) { mn = mid * 2 + 1; } if (mde > x) l = mid; else r = mid; } cout << mn << '\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.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; public class C { // change to name String nameIn = getClass().getSimpleName() + ".in"; String nameOut = null; public static void main(String[] args) throws IOException { new C().run(); // change to name } //don't touch this private void run() throws IOException { File input = null; if(nameIn != null && (input = new File(nameIn)).exists()) { try( FileReader fr = new FileReader(input); BufferedReader br = new BufferedReader(fr); ) { load_inputs(br); } } else { try( InputStreamReader fr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(fr); ) { load_inputs(br); } } } //don't touch this PrintWriter select_output() throws FileNotFoundException { if(nameOut != null) { return new PrintWriter(nameOut); } return new PrintWriter(System.out); } private void load_inputs(BufferedReader br) throws IOException { try( PrintWriter pw = select_output(); ) { int T = Integer.valueOf(br.readLine().trim()); while(T-- > 0) { double[] hct = Arrays.stream(br.readLine().trim().split("\\s+")).mapToDouble(v -> Double.valueOf(v)).toArray(); solve(hct[0], hct[1], hct[2], pw); } } } private void solve(final double h, final double c, final double t, final PrintWriter pw) { long res = 0; if(t <= (h + c) / 2) { res = 2; } else { long i0 = (long)Math.ceil((h - c) / (2 * t - h - c)); i0 += (1 - i0 % 2); long dt0 = (long)Math.abs(((i0 + 1) / 2 * h + (i0 - 1) / 2 * c) - i0 * t); long dt1 = (long)Math.abs(((i0 - 1) / 2 * h + (i0 - 3) / 2 * c) - (i0 - 2) * t); if(dt0 * (i0 - 2) < dt1 * i0) { res = i0; } else { res = i0 - 2; } } pw.println(res); 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
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
import sys if sys.subversion[0] == "PyPy": import io, atexit sys.stdout = io.BytesIO() atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue())) sys.stdin = io.BytesIO(sys.stdin.read()) input = lambda: sys.stdin.readline().rstrip() RS = raw_input RI = lambda x=int: map(x,RS().split()) RN = lambda x=int: x(RS()) ''' ...................................................................... ''' def floatCmp(num1,den1,num2,den2): v1 = num1*den2 v2 = num2*den1 if v1==v2: return 0 elif v1<v2: return -1 else: return 1 for _ in xrange(RN()): h,c,t = RI() if 2*t <= (h+c): print 2 else: a = t-c b = 2*t -(h+c) Xm = (a+b-1)/b xs = max(1,Xm-10) num = abs(xs*b-a); den = 2*xs-1 ans = xs for x in xrange(xs+1,Xm+10): num2 = abs(x*b-a); den2 = 2*x-1 if floatCmp(num,den,num2,den2)>0: ans = x num,den = num2,den2 print 2*ans-1
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
#!/usr/bin/env python from __future__ import division, print_function import os import sys from io import BytesIO, IOBase from fractions import Fraction if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def oddVal(i, h, c): if i == 1: return Fraction(h, 1) else: return Fraction((i // 2 + 1) * h + (i // 2) * c, i) def compare(a, b, t): if abs(t - a) < abs(t - b): return 0 elif abs(t - a) == abs(t - b): return 1 return 2 def main(): T = int(input()) for _ in range(T): h, c, t = map(int, input().split()) l, r = 1, 10 ** 7 closest = Fraction(h, 1) result = 1 tFraction = Fraction(t, 1) if abs(t - (h + c) / 2) < abs(t - closest): closest = Fraction(h + c, 2) result = 2 while l <= r: mid = (l + r) // 2 if mid % 2 == 0: mid -= 1 # print(l, r, mid) curTemp = oddVal(mid, h, c) compareResult = compare(closest, curTemp, tFraction) if compareResult == 1 and mid < result: closest = curTemp result = mid elif compareResult == 2: closest = curTemp result = mid if curTemp <= t: r = mid - 2 else: l = mid + 2 print(result) # 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()
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 bisect import decimal import os from collections import Counter import bisect from collections import defaultdict import math import random import heapq from math import sqrt import sys from functools import reduce, cmp_to_key from collections import deque import threading from itertools import combinations from io import BytesIO, IOBase from itertools import accumulate # sys.setrecursionlimit(200000) # mod = 10**9+7 # mod = 987 def lcm(a,b): return (a*b)//math.gcd(a,b) def sort_dict(key_value): return sorted(key_value.items(), key = lambda kv:(kv[1], kv[0])) def list_input(): return list(map(int,input().split())) def num_input(): return map(int,input().split()) def string_list(): return list(input()) def decimalToBinary(n): return bin(n).replace("0b", "") def binaryToDecimal(n): return int(n,2) def solve(): h,c,t = num_input() if h == t: print(1) else: try: ans = math.ceil((h-t)/(abs(abs(2*t-h)-c)))*2+1 except ZeroDivisionError: print(2) return t1 = (h+c)/2 t2 = decimal.Decimal(c*(ans//2)+(h*(ans//2+1)))/decimal.Decimal(ans) x = abs(t2-t) while x >= abs(decimal.Decimal(c*((ans-2)//2)+(h*((ans-2)//2+1)))/decimal.Decimal(ans-2)-t): ans -= 2 t2 = decimal.Decimal(c*(ans//2)+(h*(ans//2+1)))/decimal.Decimal(ans) x = abs(t2-t) t2 = decimal.Decimal(c*(ans//2)+(h*(ans//2+1)))/decimal.Decimal(ans) if abs(t1-t) < abs(t2-t): print(2) else: print(ans) decimal.getcontext().prec = 46 t = int(input()) #t = 1 for _ 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
# | # _` | __ \ _` | __| _ \ __ \ _` | _` | # ( | | | ( | ( ( | | | ( | ( | # \__,_| _| _| \__,_| \___| \___/ _| _| \__,_| \__,_| import sys import math import operator as op from functools import reduce from sys import maxsize from decimal import * def read_line(): return sys.stdin.readline()[:-1] def read_int(): return int(sys.stdin.readline()) def read_int_line(): return [int(v) for v in sys.stdin.readline().split()] def read_float_line(): return [float(v) for v in sys.stdin.readline().split()] def ncr(n, r): r = min(r, n-r) numer = reduce(op.mul, range(n, n-r, -1), 1) denom = reduce(op.mul, range(1, r+1), 1) return numer / denom def nc2(n): return n*(n-1)//2 getcontext().prec = 64 t = read_int() for i in range(t): h,c,t = read_int_line() if t==h: print(1) continue if 2*t==h+c: print(2) continue x = (h-t)/(2*t-h-c) if x<0: print(2) continue if x==int(x): print(int(2*x+1)) else: x = int(x) v1 = Decimal((2*x+3)*abs(((x*(h+c)+h) - t*(2*x+1)))) v2 = Decimal((2*x+1)*abs((((x+1)*(h+c)+h) - t*(2*x+3)))) if v1<=v2: ans = 2*x+1 else: ans = 2*x+3 print(ans)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.util.*; public class Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); int t=Integer.parseInt(bu.readLine()); while(t-->0) { String s[]=bu.readLine().split(" "); int h=Integer.parseInt(s[0]),c=Integer.parseInt(s[1]),o=Integer.parseInt(s[2]); if(o>=h) sb.append("1\n"); else if(2*o<=h+c) sb.append("2\n"); else { long min=0,max=1000000000l; while(max-min>1) { long mid=(min+max)/2; if(o*(2l*mid+1)>=((mid+1)*h+mid*c)) max=mid; else min=mid; } if(((min+1)*h+min*c)*(2l*max+1)+((max+1)*h+max*c)*(2l*min+1)<=2l*o*(2l*min+1)*(2l*max+1)) sb.append(2l*min+1+"\n"); else sb.append(2l*max+1+"\n"); } } System.out.print(sb); } }
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.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import javax.sound.sampled.ReverbType; import java.util.*; import java.io.*; public class Main { static ArrayList<Integer> adj[]; // static PrintWriter out = new PrintWriter(System.out); static int[][] notmemo; static int k; static int[] a; static int b[]; static int m; static class Pair implements Comparable<Pair> { int d; int s; public Pair(int b, int l) { d = b; s = l; } @Override public int compareTo(Pair o) { if (o.d - o.s > this.d - this.s) { return 1; } else if (o.d - o.s < this.d - this.s) { return -1; } else return 0; } } static Pair s1[]; static ArrayList<Pair> adjlist[]; // static char c[]; public static long mod = (long) (1e9 + 7); static int V; static long INF = (long) 1E16; static int n; static char c[]; static int d[]; static int z; static Pair p[]; static int R; static int C; static int K; static long grid[][]; public static void main(String args[]) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int T=sc.nextInt(); label:while(T-->0) { long h=sc.nextInt(); long c=sc.nextInt(); long t=sc.nextInt(); if(((h+c)/2)-t>=0) { System.out.println(2); continue label; } long p1=h-t; long p2=(2*t)-h-c; long k=2*(p1/p2)+1; long t1=(Math.abs(((k+1)/2)*h+(k/2)*c-(t*k))*(k+2)); long t2=(Math.abs(((k+3)/2)*h+((k+2)/2)*c-(t*(k+2))))*k; if(t1>t2) { System.out.println(k+2); } else { System.out.println(k); } } out.flush(); } static ArrayList<Integer> euler=new ArrayList<>(); static int dp(int i,int left,int right) { if(i==n) return 0; if(memo[i][left][right]!=-1) { return memo[i][left][right]; } int ans=(int) 1e9; if((left==1&&right==0)||(right==1&&left==0)) { ans=Math.min(ans,dp(i+1,c[i]=='L'?0:1,c[(i+2)%n]=='L'?0:1)); if(c[i]=='L') { if(i==0) c[0]='R'; ans=Math.min(ans,1+dp(i+1,1,c[(i+2)%n]=='L'?0:1)); } else { if(i==0) c[0]='L'; ans=Math.min(ans,1+dp(i+1,0,c[(i+2)%n]=='L'?0:1)); } } else if(left==1&&right==1) { if(c[i]=='L') { ans=Math.min(ans,dp(i+1,0,c[(i+2)%n]=='L'?0:1)); } else { if(i==0) c[0]='L'; ans=Math.min(ans,1+dp(i+1,0,c[(i+2)%n]=='L'?0:1)); } } else if(left==0&&right==0) { if(c[i]=='R') { ans=Math.min(ans,dp(i+1,1,c[(i+2)%n]=='L'?0:1)); } else { if(i==0) c[0]='R'; ans=Math.min(ans,1+dp(i+1,1,c[(i+2)%n]=='L'?0:1)); } } return memo[i][left][right]=ans; } static ArrayList<Integer> arr; static long total[]; static TreeMap<Integer,Integer> map1; static int zz; //static int dp(int idx,int left,int state) { //if(idx>=k-((zz-left)*2)||idx+1==n) { // return 0; //} //if(memo[idx][left][state]!=-1) { // return memo[idx][left][state]; //} //int ans=a[idx+1]+dp(idx+1,left,0); //if(left>0&&state==0&&idx>0) { // ans=Math.max(ans,a[idx-1]+dp(idx-1,left-1,1)); //} //return memo[idx][left][state]=ans; //}21 static HashMap<Integer,Integer> map; static int maxa=0; static int ff=123123; static long dist[][]; static int[][][] memo; static long modmod=998244353; static int dx[]= {1,-1,0,0}; static int dy[]= {0,0,1,-1}; static class BBOOK implements Comparable<BBOOK>{ int t; int alice; int bob; public BBOOK(int x,int y,int z) { t=x; alice=y; bob=z; } @Override public int compareTo(BBOOK o) { return this.t-o.t; } } private static long lcm(long a2, long b2) { return (a2*b2)/gcd(a2,b2); } static class Edge implements Comparable<Edge> { int node;long cost ; long time; Edge(int a, long b,long c) { node = a; cost = b; time=c; } public int compareTo(Edge e){ return Long.compare(time,e.time); } } static void sieve(int N) // O(N log log N) { isComposite = new int[N+1]; isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number primes = new ArrayList<Integer>(); for (int i = 2; i <= N; ++i) //can loop till i*i <= N if primes array is not needed O(N log log sqrt(N)) if (isComposite[i] == 0) //can loop in 2 and odd integers for slightly better performance { primes.add(i); if(1l * i * i <= N) for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in modified sieve isComposite[j] = 1; } } static TreeSet<Integer> factors; static ArrayList<Integer> primeFactors(int N) // O(sqrt(N) / ln sqrt(N)) { ArrayList<Integer> factors = new ArrayList<Integer>(); //take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); while(1l*p * p <= N) { while(N % p == 0) { factors.add(p); N /= p; } p = primes.get(++idx); } if(N != 1) // last prime factor may be > sqrt(N) factors.add(N); // for integers whose largest prime factor has a power of 1 return factors; } static class UnionFind { int[] p, rank, setSize; int numSets; int max[]; public UnionFind(int N) { p = new int[numSets = N]; rank = new int[N]; setSize = new int[N]; for (int i = 0; i < N; i++) { p[i] = i; setSize[i] = 1; } } public int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); } public boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); } public int chunion(int i,int j, int x2) { if (isSameSet(i, j)) return 0; numSets--; int x = findSet(i), y = findSet(j); int z=findSet(x2); p[x]=z;; p[y]=z; return x; } public void unionSet(int i, int j) { if (isSameSet(i, j)) return; numSets--; int x = findSet(i), y = findSet(j); if (rank[x] > rank[y]) { p[y] = x; setSize[x] += setSize[y]; } else { p[x] = y; setSize[y] += setSize[x]; if (rank[x] == rank[y]) rank[y]++; } } public int numDisjointSets() { return numSets; } public int sizeOfSet(int i) { return setSize[findSet(i)]; } } static class Quad implements Comparable<Quad> { int u; int v; char state; int turns; public Quad(int i, int j, char c, int k) { u = i; v = j; state = c; turns = k; } public int compareTo(Quad e) { return (int) (turns - e.turns); } } static long manhatandistance(long x, long x2, long y, long y2) { return Math.abs(x - x2) + Math.abs(y - y2); } static long fib[]; static long fib(int n) { if (n == 1 || n == 0) { return 1; } if (fib[n] != -1) { return fib[n]; } else return fib[n] = ((fib(n - 2) % mod + fib(n - 1) % mod) % mod); } static class Point implements Comparable<Point>{ long x, y; Point(long counth, long counts) { x = counth; y = counts; } @Override public int compareTo(Point p ) { return Long.compare(p.y*1l*x, p.x*1l*y); } } static TreeSet<Long> primeFactors(long N) // O(sqrt(N) / ln sqrt(N)) { TreeSet<Long> factors = new TreeSet<Long>(); // take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); while (p * p <= N) { while (N % p == 0) { factors.add((long) p); N /= p; } if (primes.size() > idx + 1) p = primes.get(++idx); else break; } if (N != 1) // last prime factor may be > sqrt(N) factors.add(N); // for integers whose largest prime factor has a power of 1 return factors; } static boolean visited[]; /** * static int bfs(int s) { Queue<Integer> q = new LinkedList<Integer>(); * q.add(s); int count=0; int maxcost=0; int dist[]=new int[n]; dist[s]=0; * while(!q.isEmpty()) { * * int u = q.remove(); if(dist[u]==k) { break; } for(Pair v: adj[u]) { * maxcost=Math.max(maxcost, v.cost); * * * * if(!visited[v.v]) { * * visited[v.v]=true; q.add(v.v); dist[v.v]=dist[u]+1; maxcost=Math.max(maxcost, * v.cost); } } * * } return maxcost; } **/ static boolean[] vis2; static boolean f2 = false; static long[][] matMul(long[][] a2, long[][] b, int p, int q, int r) // C(p x r) = A(p x q) x (q x r) -- O(p x q x // r) { long[][] C = new long[p][r]; for (int i = 0; i < p; ++i) { for (int j = 0; j < r; ++j) { for (int k = 0; k < q; ++k) { C[i][j] = (C[i][j] + (a2[i][k] % mod * b[k][j] % mod)) % mod; C[i][j] %= mod; } } } return C; } public static int[] schuffle(int[] a2) { for (int i = 0; i < a2.length; i++) { int x = (int) (Math.random() * a2.length); int temp = a2[x]; a2[x] = a2[i]; a2[i] = temp; } return a2; } static boolean vis[]; static HashSet<Integer> set = new HashSet<Integer>(); static long modPow(long ways, long count, long mod) // O(log e) { ways %= mod; long res = 1; while (count > 0) { if ((count & 1) == 1) res = (res * ways) % mod; ways = (ways * ways) % mod; count >>= 1; } return res % mod; } static long gcd(long l, long o) { if (o == 0) { return l; } return gcd(o, l % o); } static int[] isComposite; static int[] valid; static ArrayList<Integer> primes; static ArrayList<Integer> l1; static TreeSet<Integer> primus = new TreeSet<Integer>(); static void sieveLinear(int N) { int[] lp = new int[N + 1]; //lp[i] = least prime divisor of i for(int i = 2; i <= N; ++i) { if(lp[i] == 0) { primus.add(i); lp[i] = i; } int curLP = lp[i]; for(int p: primus) if(p > curLP || p * i > N) break; else lp[p * i] = i; } } public static long[] schuffle(long[] a2) { for (int i = 0; i < a2.length; i++) { int x = (int) (Math.random() * a2.length); long temp = a2[x]; a2[x] = a2[i]; a2[i] = temp; } return a2; } static 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 char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } public int[] nxtArr(int n) throws IOException { int[] ans = new int[n]; for (int i = 0; i < n; i++) ans[i] = nextInt(); return ans; } } public static int[] sortarray(int a[]) { schuffle(a); Arrays.sort(a); return a; } public static long[] sortarray(long a[]) { schuffle(a); Arrays.sort(a); 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
import java.util.*; import java.io.*; public class poker{ public static void main(String []args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int trials = Integer.parseInt(br.readLine()); for(int trial = 0; trial < trials; trial++){ StringTokenizer st = new StringTokenizer(br.readLine()); int h = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); int t = Integer.parseInt(st.nextToken()); if(t <= (h+c)/2) System.out.println(2); else{ int diff = h-c; double avg = ((double)h + c)/2; double estimate = diff/(t - avg); int est = (int)estimate; int lower = 0, upper = 0, mid = 0; if(est % 4 == 1){ lower = est - 3; upper = est + 1; } else if(est % 4 == 3){ lower = est - 1; upper = est + 3; } else if(est % 4 == 2){ mid = est - 4; lower = est; upper = est + 4; } else{ lower = est - 2; upper = est + 2; } double lowerbound = (double)diff/lower; double upperbound = (double)diff/upper; int ans = 0; //double d = Math.min(Math.abs(lowerbound-(t-avg)),Math.abs(upperbound-(t-avg))); if(Math.abs(lowerbound-(t-avg)) <= Math.abs(upperbound-(t-avg))) ans = lower/2; else ans = upper/2; System.out.println(ans); //if(d > ) } } } }
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
try: 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: 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) except: pass
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int q; cin >> q; while (q--) { double t, h, c; cin >> h >> c >> t; if (h == t) { cout << 1 << '\n'; } else if (h + c == 2 * t) { cout << 2 << '\n'; } else { double x = abs((t - h) / (h + c - 2 * t)); double y = floor(x), z = ceil(x); y = ((h + c) * y + h) / (2 * y + 1); z = ((h + c) * z + h) / (2 * z + 1); if (abs(t - h) <= min({abs(t - (h + c) / 2), abs(y - t), abs(z - t)})) cout << 1 << '\n'; else if (abs(t - (h + c) / 2) <= min(abs(y - t), abs(z - t))) cout << 2 << '\n'; else if (abs(y - t) <= abs(z - t)) cout << 2 * floor(x) + 1 << '\n'; else cout << 2 * ceil(x) + 1 << '\n'; } } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.Scanner; public class MixingWater { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int i=0; i<n; i++) { int hot=sc.nextInt(); int cold=sc.nextInt(); int goal=sc.nextInt(); if(goal<=(hot+cold)/2) { System.out.println(2); } else if(goal>=hot) { System.out.println(1); } else { int t=(goal-hot)/(hot+cold-2*goal); t=Math.max(0, t-5); long closest=hot-goal; while((hot+(hot+cold)*t)>goal*(2*t+1)) { closest=(hot+(hot+cold)*t)-goal*(2*t+1); t++; } if(Math.abs((hot+(hot+cold)*t)-goal*(2*t+1))*(2*t-1)<closest*(2*t+1)) { System.out.println(2*t+1); } else { System.out.println(2*t-1); } } } sc.close(); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
def read_int(): return int(input()) def read_ints(): return map(int, input().split(' ')) tt = read_int() for case_num in range(tt): h, c, t = read_ints() if 2 * t <= h + c: print(2) continue l = 1 r = int(1e7) def calc(x): return x * h + (x - 1) * c while l <= r: mid = (l + r) // 2 temp = calc(mid) if temp >= t * (2 * mid - 1): l = mid + 1 else: r = mid - 1 a = calc(l - 1) b = calc(l) p = 2 * l - 3 q = 2 * l - 1 if abs(a * q - t * p * q) <= abs(b * p - t * p * q): print(p) else: print(q)
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 cc; import java.util.*; public class Test { static int caly(int h,int c,int t) { int ret; ret=(h-t)/(2*t-h-c); return ret; } public static void main(String [] arsg) { Scanner s=new Scanner(System.in); int t=s.nextInt(); //Random r=new Random(); while(t-->0) { int h=s.nextInt(); int c=s.nextInt(); int temp=s.nextInt(); if(temp==(h+c)/2) { System.out.println("2"); continue; } int y=caly(h,c,temp); if(y<0) {System.out.println("2"); continue; }int a=y; int b=y+1; if(Math.abs((y*(h+c)+h-temp*(2*y+1))*(2*y+3))<=Math.abs(((y+1)*(h+c)+h-temp*(2*y+3))*(2*y+1))) System.out.println(2*y+1); else System.out.println(2*y+3); } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long int x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } long long power(long long a, long long b) { long long res = 1; a %= 998244353; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % 998244353 % 998244353; a = a * a % 998244353; } return res; } long long gcd(long long a, long long b) { if (a < b) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long i, j, k, m, l, r, n, T; cin >> T; while (T--) { long double h, c, t; cin >> h >> c >> t; if (t == h) { cout << 1 << "\n"; } else if (t == (h + c) / 2) { cout << 2 << "\n"; } else if (t < (h + c) / 2) { cout << 2 << "\n"; } else { l = 0; r = 1000001; long long mid = 0; long double dif = 1000000; long long ans = 0; long double tp = 2.0 * h + c; tp = tp / 3; if (t > tp) { if (abs(h - t) > abs(tp - t)) { cout << 3 << "\n"; continue; } else { cout << 1 << "\n"; continue; } } while (l <= r) { mid = (l + r) / 2; long double tp = 0; tp = tp + (mid + 1) * h + mid * c; tp = tp / (2.0 * mid + 1); if (tp < t) { r = mid - 1; } else { l = mid + 1; } if (abs(tp - t) < dif) { dif = abs(tp - t); ans = 2 * mid + 1; } } dif = 1000000; long long fin = 0; for (i = max(ans - 10, 3LL); i <= ans + 10; i++) { if (i % 2 == 1) { tp = ((i - 1) / 2) * c + (((i - 1) / 2) + 1) * h; tp = tp / i; if (abs(tp - t) < dif) { dif = abs(tp - t); fin = i; } } } cout << fin << "\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 int power(long long int x, long long int n, long long int m) { long long int res = 1; while (n > 0) { if (n & 1) res = (res * x) % m; x = (x * x) % m; n = n >> 1; } return res; } long long int maxSubsetSum(vector<long long int> v) { long long int max_so_far = LLONG_MIN, max_ending_here = 0; for (long long int i = 0; i < (long long int)v.size(); i++) { max_ending_here += v[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int q; cin >> q; while (q--) { long double h, c, t, res = 1e7; cin >> h >> c >> t; long double d1 = (h + c) / 2; long long int lo = 1, hi = 1e12, ans = hi; while (lo <= hi) { long long int mid = (lo + hi) / 2; long double d = (mid * h + (mid - 1) * c) / (long double)(2 * mid - 1); if (d - t == 0) { ans = 2 * mid - 1; break; } else if (d - t < 0) { hi = mid - 1; } else { lo = mid + 1; } if (res >= abs(d - t)) { if (res == abs(d - t)) ans = min(ans, 2 * mid - 1); else ans = 2 * mid - 1; res = abs(d - t); } } if (res == abs(d1 - t)) ans = min(ans, (long long int)2); else if (res > abs(d1 - t)) ans = min(ans, (long long int)2); 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 sys from collections import defaultdict from copy import copy from fractions import Fraction as FR R = lambda t = int: t(input()) RL = lambda t = int: [t(x) for x in input().split()] RLL = lambda n, t = int: [RL(t) for _ in range(n)] def solve(): h, c, t = RL() P = [(FR(abs(h - t)), 1), (abs(FR((h + c), 2) - t), 2)] F = lambda x: (abs(FR((h * (x + 1) + x * c), (2*x+1)) - t), 2 * x + 1) if 2 * t - h - c != 0: x = (h - t) // (2 * t - h - c) for i in range(x, x + 2): if i >= 0: P += [F(i)] print(min(P)[1]) T = R() for t in range(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
from decimal import * cases = int(input()) def get_delta(h, t, x): return h * (1 + x) / (1 + 2*x) - t for _ in range(cases): h, c, t = [int(x) for x in input().split()] if t <= (h + c) / 2: print(2) continue # h = Decimal(h - c) # t = Decimal(t - c) h = h - c t = t - c x = 0 delta = get_delta(h, t, x) while delta > 0: if x == 0: x = 1 else: x *= 2 delta = get_delta(h, t, x) lo = x // 2 hi = x while lo + 1 < hi: # print(lo, hi) mid = (hi - lo) // 2 + lo delta = get_delta(h, t, mid) if delta > 0: lo = mid else: hi = mid # print(lo, hi) # print(get_delta(h, t, lo)) # print(get_delta(h, t, hi)) if abs(get_delta(Decimal(h), Decimal(t), lo)) <= abs(get_delta(Decimal(h), Decimal(t), hi)): x = lo else: x = hi print(1 + 2*x)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys input = sys.stdin.readline """ infinite water: h = hot water c = cold water (c < h) """ from decimal import Decimal from math import floor, ceil def calc_temp(cups, h, c): # cups = total cups if cups <= 0: return 0 if cups % 2 == 0: return ((h * (cups/2) + c * (cups/2))) / cups else: return Decimal(((h * (cups//2 + 1) + c * (cups//2)))) / Decimal(cups) def solve(h, c, t): if t >= h: return 1 elif t <= (h+c)/2: return 2 else: x = (c-t)/(h+c-2*t) #print(x) lower = max(floor(x), 1) upper = ceil(x) + 2 best_abs = float("inf") best_cups = None t = Decimal(t) for i in range(lower, upper): t_b = calc_temp(2*i-1, h, c) diff = abs(t_b-t) if diff < best_abs: best_abs = diff best_cups = 2*i-1 return best_cups res = [] t = int(input()) for _ in range(t): h, c, t = map(int, input().split()) res.append(solve(h, c, t)) for r in res: print(r) """ for i in range(1, 100): print(calc_temp(i, 30, 20)) """
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 Counter import math import sys from bisect import bisect,bisect_left,bisect_right def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def mod(): return 10**9+7 for _ in range(INT()): #n = INT() # s = input() h,c,t = MAP() #arr = LIST() if h == t: print(1) elif (h+c)/2 >= t: print(2) else: x = (t-c)//(2*t-h-c) y = x+1 ans1 = abs(x*h + (x-1)*c + t*(1-2*x))/(2*x-1) ans2 = abs(y*h + (y-1)*c + t*(1-2*y))/(2*y-1) if ans1 <= ans2: 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 java.util.*; import java.lang.*; import java.io.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader s = new FastReader(); static long h,c,t; public static void solve(){ h = s.nextLong(); c = s.nextLong(); t = s.nextLong(); // At or below midpoint if(t <= (h + c) / 2) { System.out.println(2); return; } // One hot cup if(t == h) { System.out.println(1); return; } // We'll do 2k + 1 iterations // gives (h * (k + 1) + c * k) / (2k + 1) int lo = 1, hi = 2_000_000, m, f = -1; while(lo <= hi) { m = lo + hi >> 1; // if we use 2m + 1 pours, are we at or below t? long a = h * (m + 1) + c * m, b = 2 * m + 1; if(a <= b * t) { f = m; hi = m - 1; // try using fewer } else { lo = m + 1; // we need more iterations } } long an = t * (2 * f + 1) - h * (f + 1) - c * f; long ad = 2 * f + 1; long bn = h * f + c * (f - 1) - t * (2 * f - 1); long bd = 2 * f - 1; if(an * bd < bn * ad) System.out.println(2 * f + 1); else System.out.println(2 * f - 1); } public static void main(String[] args) { int T=1; T = s.nextInt(); while(T>0){ // System.out.println("TEST CASE: "+T); solve(); T--; } } } class Pair{ int first; int second; Pair(int a,int b){ this.first = a; this.second = b; } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from sys import stdin, stdout # 30, 10, 20 # (30x + 10y) / (x + y) = 20 # x = y || x = y+1 # 40x / 2x = 20 # (30x + 10x - 10) / (2x - 1) = 20 # (10x + 30x - 30) / (2x - 1) = 20 # (40x - 10) / (2x - 1) # 40x/(2x-1) - 10/(2x-1) # 40x/(2x-1) - 30/(2x-1) # (30x + 10(x - 1)) / (2x - 1) def mixing_water(h, c, t): r1 = 2 v1 = (h + c)//2 rv2 = [] if h >= c: rv2 = bs1(h, c, t) else: rv2 = bs2(h, c, t) r2 = rv2[0] v2 = rv2[1] dif = abs(t-v1) - abs(t-v2) if dif == 0: return min(r1, r2) elif dif > 0: return r2 else: return r1 # h >= c def bs1(h, c, t): l = 1 r = 2 ** 31 - 1 v1 = 0 while l < r: m = (l + r) // 2 v1 = ((h + c)*m - c) / (2*m - 1) #print(v1) if v1 > t: l = m + 1 else: r = m #v1 = ((h + c) * r - c) / (2 * r - 1) #v2 = ((h + c) * (r-1) - c) / (2 * (r-1) - 1) v1 = ((h + c) * r - c) v2 = ((h + c) * (r-1) - c) t1 = (2 * r - 1) t2 = (2 * (r-1) - 1) #print( ((999977 + 17) * 249991 - 17) / (2 * 249991 - 1) ) #print(r) #print(v1) #print(v2) #if r > 1 and abs(t - v2) <= abs(v1 - t): if r > 1 and abs(t*t1*t2 - v2*t1) <= abs(v1*t2 - t*t1*t2): #return [(r-1)*2-1, v2] return [(r - 1) * 2 - 1, v2/t2] else: #return [r*2-1, v1] return [r * 2 - 1, v1/t1] # h < c def bs2(h, c, t): l = 1 r = 2**31-1 v1 = 0 while l < r: m = (l + r) // 2 v1 = ((h + c)*m - c) / (2*m - 1) if v1 < t: l = m + 1 else: r = m #v1 = ((h + c) * r - c) / (2 * r - 1) #v2 = ((h + c)*(r-1) - c) / (2*(r-1) - 1) v1 = ((h + c) * r - c) v2 = ((h + c) * (r - 1) - c) t1 = (2 * r - 1) t2 = (2 * (r - 1) - 1) #if r > 1 and abs(v2 - t) <= abs(t - v1): if r > 1 and abs(v2*t1 - t*t1*t2) <= abs(t*t1*t2 - v1*t2): #return [(r-1)*2-1, v2] return [(r - 1) * 2 - 1, v2/t2] else: #return [r*2, v1] return [r * 2, v1/t1] if __name__ == '__main__': T = int(stdin.readline()) for i in range(T): hct = list(map(int, stdin.readline().split())) stdout.write(str(mixing_water(hct[0], hct[1], hct[2])) + '\n')
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.util.*; public class Solution { public static void main(String args[]) throws IOException { InOut inout = new InOut(); Resolver resolver = new Resolver(inout); resolver.solve(); inout.flush(); } private static class Resolver { final long LONG_INF = (long) 1e18; final int INF = 1000000007; final int MOD = 998244353; long f[]; InOut inout; Resolver(InOut inout) { this.inout = inout; } void initF(int n) { f = new long[n + 1]; f[1] = 1; for (int i = 2; i <= n; i++) { f[i] = (f[i - 1] * i) % MOD; } } int d[] = {0, -1, 0, 1, 0}; int dfs(int r, int c, int n, int m, char g[][]) { g[r][c] = '*'; int res = 1; for (int i = 0; i < 4; i++) { int x = r + d[i]; int y = c + d[i + 1]; if (x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '*') { continue; } res += dfs(x, y, n, m, g); } return res; } private static class Tree { int min = (int) 1e7; int left; int right; Tree(int left, int right) { this.left = left; this.right = right; } static void updateUp(Map<Integer, Tree> lineTree, int k) { lineTree.get(k).min = Math.min(lineTree.get(k << 1).min, lineTree.get(k << 1 | 1).min); } static void build(int l, int r, Map<Integer, Tree> lineTree, int k) { lineTree.put(k, new Tree(l, r)); if (l == r) { return; } int mid = l + (r - l) / 2; build(l, mid, lineTree, k << 1); build(mid + 1, r, lineTree, k << 1 | 1); } static int query(int l, int r, int L, int R, Map<Integer, Tree> lineTree, int k) { if (L > R) { return (int) 1e7; } if (L <= l && r <= R) { return lineTree.get(k).min; } if (r < L || l > R) { return (int) 1e7; } int mid = l + (r - l) / 2; return Math.min(query(l, mid, L, R, lineTree, k << 1) , query(mid + 1, r, L, R, lineTree, k << 1 | 1)); } static void update(int l, int r, int idx, int val, Map<Integer, Tree> lineTree, int k) { if (l == r) { lineTree.get(k).min = val; updateUp(lineTree, k >> 1); return; } int mid = l + (r - l) / 2; if (mid >= idx) { update(l, mid, idx, val, lineTree, k << 1); } else { update(mid + 1, r, idx, val, lineTree, k << 1 | 1); } if (k > 1) { updateUp(lineTree, k >> 1); } } } double pc(double m, double h, double c) { return (m * (h + c) + h) / (2.0 * m + 1.0); } void solve() throws IOException { int tt = 1; boolean hvt = true; if (hvt) { tt = nextInt(); } for (int cs = 1; cs <= tt; cs++) { long h = nextInt(); long c = nextInt(); long t = nextInt(); long md = h + c; if (md >= t * 2) { format("2"); } else { long l = 1; long r = (long) 1e9; r = binSearch(l, r, k -> (k * (h + c) + h > t * (2 * k + 1))); l = r - 1; boolean ok = 2 * t * (2 * l + 1) * (2 * r + 1) >= (r * (h + c) + h) * (2 * l + 1) + (l * (h + c) + h) * (2 * r + 1); if (ok) { format("%d", l * 2 + 1); } else { format("%d", r * 2 + 1); } } format("\n"); } // int n = nextInt(); // int a[] = new int[n]; // int mx[] = new int[n]; // int sum[] = new int[n + 1]; // Map<Integer, Tree> treeMap = new HashMap<>(n << 1); // Tree.build(1, n, treeMap, 1); // int lftMx[] = new int[n + 1]; // int mn = 0; // for (int i = 0; i < n; i++) { // a[i] = nextInt(); // sum[i + 1] = sum[i] + a[i]; // lftMx[i + 1] = sum[i + 1] - mn; // mn = Math.min(mn, sum[i + 1]); // Tree.update(1, n, i + 1, sum[i + 1], treeMap, 1); // } // Stack<Integer> stack = new Stack<>(); // for (int i = 0; i < n; i++) { // while (!stack.isEmpty() && a[stack.peek()] <= a[i]) { // stack.pop(); // } // if (stack.isEmpty()) { // mx[i] = lftMx[i]; // } else { // int j = stack.peek(); // if (j == i - 2) { // mx[i] = Math.max(mx[j] + sum[i + 1] - sum[j + 1] // , a[i - 1]); // } else if (j < i - 2) { // mx[i] = Math.max(mx[j] + sum[i + 1] - sum[j + 1] // , sum[i] - Tree.query(1, n, j + 1, i - 1, treeMap, 1)); // } else { // mx[i] = mx[i - 1] + a[i]; // } // } // mx[i] = Math.max(0, mx[i]); // stack.push(i); // } // int res = 0; // for (int i = 0; i < n; i++) { // res = Math.max(res, mx[i]); // } // format("%d", res); } String next(int n) throws IOException { return inout.next(n); } int nextInt() throws IOException { return inout.nextInt(); } long nextLong(int n) throws IOException { return inout.nextLong(n); } long[] anLong(int i, int j) throws IOException { long a[] = new long[j + 1]; for (int k = i; k <= j; k++) { a[i] = nextLong(20); } return a; } void print(String s, boolean nextLine) { inout.print(s, nextLine); } void format(String format, Object... obj) { inout.format(format, obj); } void flush() { inout.flush(); } void swap(int a[], int i, int j) { a[i] ^= a[j]; a[j] ^= a[i]; a[i] ^= a[j]; } int getP(int x, int p[]) { if (p[x] == 0 || p[x] == x) { return x; } return p[x] = getP(p[x], p); } void union(int x, int y, int p[]) { if (x < y) { p[y] = x; } else { p[x] = y; } } boolean topSort() { int n = edges.length - 1; int d[] = new int[n + 1]; for (int i = 1; i <= n; i++) { for (int j = 0; j < edges[i].size(); j++) { d[edges[i].get(j)[0]]++; } } List<Integer> list = new ArrayList<>(); for (int i = 1; i <= n; i++) { if (d[i] == 0) { list.add(i); } } for (int i = 0; i < list.size(); i++) { for (int j = 0; j < edges[list.get(i)].size(); j++) { int t = edges[list.get(i)].get(j)[0]; d[t]--; if (d[t] == 0) { list.add(t); } } } return list.size() == n; } class BinaryIndexedTree { int n = 1; long C[]; BinaryIndexedTree(int sz) { while (n <= sz) { n <<= 1; } C = new long[n]; } int lowbit(int x) { return x & -x; } void add(int x, long val) { while (x < n) { C[x] += val; x += lowbit(x); } } long getSum(int x) { long res = 0; while (x > 0) { res += C[x]; x -= lowbit(x); } return res; } int binSearch(long sum) { if (sum == 0) { return 0; } int n = C.length; int mx = 1; while (mx < n) { mx <<= 1; } int res = 0; for (int i = mx / 2; i >= 1; i >>= 1) { if (C[res + i] < sum) { sum -= C[res + i]; res += i; } } return res + 1; } } //Binary tree class TreeNode { int val; int tier = -1; TreeNode parent; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } } //binary tree dfs void tierTree(TreeNode root) { if (null == root) { return; } if (null != root.parent) { root.tier = root.parent.tier + 1; } else { root.tier = 0; } tierTree(root.left); tierTree(root.right); } //LCA start TreeNode[][] lca; TreeNode[] tree; void lcaDfsTree(TreeNode root) { if (null == root) { return; } tree[root.val] = root; TreeNode nxt = root.parent; int idx = 0; while (null != nxt) { lca[root.val][idx] = nxt; nxt = lca[nxt.val][idx]; idx++; } lcaDfsTree(root.left); lcaDfsTree(root.right); } TreeNode lcaTree(TreeNode root, int n, TreeNode x, TreeNode y) { if (null == root) { return null; } if (-1 == root.tier) { tree = new TreeNode[n + 1]; tierTree(root); } if (null == lca) { lca = new TreeNode[n + 1][31]; lcaDfsTree(root); } int z = Math.abs(x.tier - y.tier); int xx = x.tier > y.tier ? x.val : y.val; while (z > 0) { final int zz = z; int l = (int) binSearch(0, 31 , k -> zz < (1 << k)); xx = lca[xx][l].val; z -= 1 << l; } int yy = y.val; if (x.tier <= y.tier) { yy = x.val; } while (xx != yy) { final int xxx = xx; final int yyy = yy; int l = (int) binSearch(0, 31 , k -> (1 << k) <= tree[xxx].tier && lca[xxx][(int) k] != lca[yyy][(int) k]); xx = lca[xx][l].val; yy = lca[yy][l].val; } return tree[xx]; } //LCA end //graph List<int[]> edges[]; void initGraph(int n, int m, boolean hasW, boolean directed) throws IOException { edges = new List[n + 1]; for (int i = 1; i <= n; i++) { edges[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int f = nextInt(); int t = nextInt(); int w = hasW ? nextInt() : 0; edges[f].add(new int[]{t, w}); if (!directed) { edges[t].add(new int[]{f, w}); } } } long binSearch(long l, long r, BinSearch sort) { while (l < r) { long m = l + (r - l) / 2; if (sort.binSearchCmp(m)) { l = m + 1; } else { r = m; } } return l; } void getDiv(Map<Integer, Integer> map, int n) { int sqrt = (int) Math.sqrt(n); for (int i = sqrt; i >= 2; i--) { if (n % i == 0) { getDiv(map, i); getDiv(map, n / i); return; } } map.put(n, map.getOrDefault(n, 0) + 1); } boolean[] generatePrime(int n) { boolean p[] = new boolean[n + 1]; p[2] = true; for (int i = 3; i <= n; i += 2) { p[i] = true; } for (int i = 3; i <= Math.sqrt(n); i += 2) { if (!p[i]) { continue; } for (int j = i * i; j <= n; j += i << 1) { p[j] = false; } } return p; } boolean isPrime(long n) { //determines if n is a prime number int p[] = {2, 3, 5, 233, 331}; int pn = p.length; long s = 0, t = n - 1;//n - 1 = 2^s * t while ((t & 1) == 0) { t >>= 1; ++s; } for (int i = 0; i < pn; ++i) { if (n == p[i]) { return true; } long pt = pow(p[i], t, n); for (int j = 0; j < s; ++j) { long cur = llMod(pt, pt, n); if (cur == 1 && pt != 1 && pt != n - 1) { return false; } pt = cur; } if (pt != 1) { return false; } } return true; } long llMod(long a, long b, long mod) { return (a * b - (long) ((double) a / mod * b + 0.5) * mod + mod) % mod; // long r = 0; // a %= mod; // b %= mod; // while (b > 0) { // if ((b & 1) == 1) { // r = (r + a) % mod; // } // b >>= 1; // a = (a << 1) % mod; // } // return r; } long pow(long a, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans = ans * a; } a = a * a; n >>= 1; } return ans; } long pow(long a, long n, long mod) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans = llMod(ans, a, mod); } a = llMod(a, a, mod); n >>= 1; } return ans; } private long[][] initC(int n) { long c[][] = new long[n][n]; for (int i = 0; i < n; i++) { c[i][0] = 1; } for (int i = 1; i < n; i++) { for (int j = 1; j <= i; j++) { c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; } } return c; } /** * ps: n >= m, choose m from n; */ private int cmn(long n, long m) { if (m > n) { n ^= m; m ^= n; n ^= m; } m = Math.min(m, n - m); long top = 1; long bot = 1; for (long i = n - m + 1; i <= n; i++) { top = (top * i) % MOD; } for (int i = 1; i <= m; i++) { bot = (bot * i) % MOD; } return (int) ((top * pow(bot, MOD - 2, MOD)) % MOD); } long gcd(long a, long b) { if (a < b) { return gcd(b, a); } while (b != 0) { long tmp = a % b; a = b; b = tmp; } return a; } boolean isEven(long n) { return (n & 1) == 0; } interface BinSearch { boolean binSearchCmp(long k); } } private static class InOut { private BufferedReader br; private StreamTokenizer st; private PrintWriter pw; InOut() { br = new BufferedReader(new InputStreamReader(System.in)); st = new StreamTokenizer(br); pw = new PrintWriter(new OutputStreamWriter(System.out)); st.ordinaryChar('\''); st.ordinaryChar('\"'); st.ordinaryChar('/'); } private long[] anLong(int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } private String next(int len) throws IOException { char ch[] = new char[len]; int cur = 0; char c; while ((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t') ; do { ch[cur++] = c; } while (!((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t')); return String.valueOf(ch, 0, cur); } private int nextInt() throws IOException { st.nextToken(); return (int) st.nval; } private long nextLong(int n) throws IOException { return Long.parseLong(next(n)); } private double nextDouble() throws IOException { st.nextToken(); return st.nval; } private String[] nextSS(String reg) throws IOException { return br.readLine().split(reg); } private String nextLine() throws IOException { return br.readLine(); } private void print(String s, boolean newLine) { if (null != s) { pw.print(s); } if (newLine) { pw.println(); } } private void format(String format, Object... obj) { pw.format(format, obj); } private void flush() { 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
#include <bits/stdc++.h> using namespace std; int main() { long long tt; cin >> tt; label: while (tt--) { long long h, c, t; cin >> h >> c >> t; if (h == t) { cout << 1 << "\n"; goto label; } long long x; long double answer; long long ans; if (double(h - t) <= abs((h + c) / 2.0 - (t * 1.0))) { answer = double(h - t); ans = 1; } else { answer = abs((h + c) / 2.0 - (t * 1.0)); ans = 2; } if ((h + c) == 2 * t) { cout << 2 << "\n"; goto label; } x = (abs)((t - c) / (2 * t - (h + c))); long long y = x + 1; if (x == 0) x = 2; if (abs(((x * 1.0 * (h + c) - c) / (2 * x - 1) * 1.0) - t) > abs(((y * 1.0 * (h + c) - c) / (2 * y - 1) * 1.0) - t)) { if (abs(((y * 1.0 * (h + c) - c) / (2 * y - 1) * 1.0) - t) < answer) cout << 2 * y - 1 << "\n"; else cout << ans << "\n"; } else if (abs(((x * 1.0 * (h + c) - c) / (2 * x - 1) * 1.0) - t) < answer) { cout << 2 * x - 1 << "\n"; } else cout << ans << "\n"; } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.InputStream; import java.io.PrintStream; import java.math.BigDecimal; import java.math.MathContext; import java.util.Scanner; public class Problem1359C { private static long solveSingle(int h, int c, int t) { if (t <= (h + c) / 2) return 2; long hotN = (t - c) / (2 * t - h - c); long ost = (t - c) % (2 * t - h - c); if (ost > 0) { BigDecimal t1 = new BigDecimal(h * hotN + c * (hotN - 1)); t1 = t1.divide(new BigDecimal((2 * hotN - 1.0)), MathContext.DECIMAL128); BigDecimal t2 = new BigDecimal(h * (hotN + 1) + c * hotN); t2 = t2.divide(new BigDecimal((2 * hotN + 1.0)), MathContext.DECIMAL128); BigDecimal tbd = new BigDecimal(t); BigDecimal dt1 = t1.subtract(tbd).abs(); BigDecimal dt2 = t2.subtract(tbd).abs(); if (dt2.subtract(dt1).doubleValue() < 0) { hotN++; } } return 2 * hotN - 1; } public static void solve(InputStream in, PrintStream out) throws Exception { Scanner scanner = new Scanner(in); for (int i = 0, k = scanner.nextInt(); i < k; i++) { out.println(solveSingle(scanner.nextInt(), scanner.nextInt(), scanner.nextInt())); } } public static void main(String[] args) throws Exception { solve(System.in, System.out); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
def solve(): h, c, t = [int(x) for x in input().split()] if h == c: print(1) elif t >= h: print(1) elif 2 * t <= h + c: print(2) else: ans = (h - c) // (2 * t - h - c) if ans % 2 == 0: ans += 1 variants = [] variants.append(ans) variants.append(2) variants.append(1) if ans > 1: variants.append(ans - 2) variants.append(ans + 2) print(min(variants, key=lambda ans: abs(t - (h * (ans // 2 + ans % 2) + c * (ans // 2)) / ans))) t = int(input()) for _ 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
import java.io.*; import java.util.*; public class Test { public static void main(String[] args) { FastScanner in = new FastScanner(); // Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); int h, c, t, x, y, minx; double min; while (T-- > 0) { h = in.nextInt(); c = in.nextInt(); t = in.nextInt(); if (6 * t >= (5 * h + c)) { System.out.println(1); } else if (2 * t <= h + c) { System.out.println(2); } else { x = (t - c - 1) / (2 * t - c - h); if (((2 * t * x + t - x * h - h - x * c) * (2 * x - 1)) >= ((x * h + x * c - c - 2 * t * x + t) * (2 * x + 1))) { x--; } System.out.println(2*x+1); } } } } class Node { long l, r; public Node(int l, int r) { this.l = l; this.r = r; } } 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[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long nextLong() { 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
import java.io.*; import java.util.*; public class Main { private static final boolean N_CASE = true; private void solve() { int h = sc.nextInt(); int c = sc.nextInt(); int t = sc.nextInt(); long left = 0, right = Integer.MAX_VALUE; while (left <= right) { long mid = (left + right) / 2; if (t * (mid * 2 + 1) < ((mid + 1) * h + mid * c)) { left = mid + 1; } else { right = mid - 1; } } if (left > Integer.MAX_VALUE) { out.println(2); } else if (left <= 0) { out.println(1); } else { long k1 = left + 1; long k2 = left; long k3 = left; long k4 = left - 1; if (t * (k1 + k2) * (k3 + k4) - (k1 * h + k2 * c) * (k3 + k4) >= (k3 * h + k4 * c) * (k1 + k2) - (k1 + k2) * (k3 + k4) * t) { out.println((left - 1) * 2 + 1); } else { out.println(left * 2 + 1); } } } private void run() { int T = N_CASE ? sc.nextInt() : 1; for (int t = 0; t < T; ++t) { solve(); } } private static MyWriter out; private static MyScanner sc; private static class MyScanner { BufferedReader br; StringTokenizer st; private MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } List<Integer> nextList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextInt()); } return list; } } private static class MyWriter extends PrintWriter { private MyWriter(OutputStream outputStream) { super(outputStream); } void printArray(int[] a) { for (int i = 0; i < a.length; ++i) { print(a[i]); print(i == a.length - 1 ? '\n' : ' '); } } void printlnArray(int[] a) { for (int v : a) { println(v); } } void printList(List<Integer> list) { for (int i = 0; i < list.size(); ++i) { print(list.get(i)); print(i == list.size() - 1 ? '\n' : ' '); } } void printlnList(List<Integer> list) { list.forEach(this::println); } } public static void main(String[] args) { out = new MyWriter(new BufferedOutputStream(System.out)); sc = new MyScanner(); new Main().run(); out.close(); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
t = int(input()) for _ in range(t): h, c, g = [int(x) for x in input().split()] av = (h + c)/2 if g == h: print(1) elif g <= av + 0.001: print(2) else: # after 2n+1 pours (n = 0, 1, ...) we have a temp of ((n+1)*h + n*c)/(2*n+1) = g, # so we have n*(h+c) + h = g*(2*n+1), # or n*(h+c) + h = g*2*n + g # or n*(h+c-g*2) = g - h # or n ~= (g-h)/(h+c-g*2) n = int((g-h)/(h+c-g*2)) best_n = -1 for j in range(max(n-3, 0), n+3): if best_n == -1 or \ abs(((best_n + 1)*h + best_n*c) - (2*best_n+1)*g)*(2*j+1) > \ abs(((j + 1)*h + j*c) - g*(2*j+1))*(2*best_n+1): best_n = j print(2*best_n+1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for i in range(int(input())): h, c, t= map(int, input().split()) result = float('inf') avg = (h+c)/2 D = {} if(t==h): result = 1 elif(t in (c,avg)): result = 2 else: D[abs(t-c)] = 2 D[abs(t-h)] = 1 if(t-avg > 0): X = (h-avg)/(t-avg) if(X<3): X = (h-avg)/3 x = 3 elif(int(X)&1==0): x = int(X)+1 X = (h-avg)/(int(X)+1) else: x= int(X) X = (h-avg)/x X1 = abs(t - avg - (h-avg)/x ) X2 = t - avg - (h-avg)/(x+2) if(X1 > X2): X = (h-avg)/(x+2) x += 2 X = t - avg-X if(X!=abs(t-h)): D[X] = x elif(t-avg < 0): X = abs(t-avg) D[X] = 2 result = D[min(D.keys())] print(result)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; const long long N = 200005; bool col2(pair<long long, long long> p1, pair<long long, long long> p2) { if (p1.second == p2.second) return p1.first > p2.first; return p1.second > p2.second; } struct comp { bool operator()(long long const &x, long long const &y) { return x > y; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); ; long long t; cin >> t; while (t--) { double h, c, t; cin >> h >> c >> t; if (t == h) cout << 1 << endl; else if ((2 * t < (h + c)) || (c + h) / 2 == t) cout << 2 << endl; else { double q1 = (h - t) / (2 * t - (c + h)); double a1 = floor(q1); double a2 = ceil(q1); double q2 = (a1 * c + (a1 + 1) * h) / (2 * a1 + 1); double q3 = ((a2)*c + (a2 + 1) * h) / (2 * a2 + 1); if (abs(q2 - t) > abs(q3 - t)) cout << (long long)(2 * a2 + 1) << endl; else cout << (long long)(2 * a1 + 1) << endl; } } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math from decimal import * T= int(input()) def f(j): a=Decimal((h+c)*(j//2) + h*(j%2))/Decimal(j) return abs(a-t) for i in range(T): h,c,t= map(int,input().split()) if h==t: print(1) elif (h+c)//2 == t: print(2) else: k=(h-t)//(2*t-h-c) z=(c-t)//(h+c-2*t) ans=[1,2,abs(2*k+1),abs(2*k+3),abs(2*k-1),abs(2*z+1),abs(2*z+3),abs(2*z-1)] res=0 for j in range(0,len(ans)): if f(ans[j])<=f(ans[res]): res=j print(ans[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
T = int(input()) for _ in range(T): h, c, t = map(int, input().split()) if h + c >= t * 2: print(2) else: l, r = 0, 10 ** 9 while r - l > 1: m = (l + r) // 2 if h * (m + 1) + c * m >= t * (2 * m + 1): l = m else: r = m if (2 * l + 1) * ((2 * r + 1) * t - (h * (r + 1) + c * r)) >= (2 * r + 1) * (h * (l + 1) + c * l - (2 * l + 1) * t): print(l * 2 + 1) else: print(r * 2 + 1)
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.InputStreamReader; public class Sola3 { public int minCups(int h, int c, int t){ // 12/4 if(((h+c)%2) == 0 && (h+c)/2 == t)return 2; if(h == t)return 1; int x = c - t; int y = h+c - 2*t; if(x < 0 && y <0){ x = x * -1; y = y*-1; } if(x == 0|| (x < 0 && y >0) || (x >0 && y < 0)){ int diff2 = (h+c)-2*t; if(diff2 < 0)diff2=diff2*-1; int diff1 = h - t; if(diff1 < 0) diff1 = diff1*-1; if(2*diff1 <= diff2)return 1; else return 2; } int n = x/y; int mod = x%y; if(mod > 0){ int diff1 = ((h+c)*n - c) - t*(2*n-1); int diff2 = ((h+c)*(n+1) - c) - t*(2*n+1); if(diff1 < 0)diff1 *= -1; if(diff2 < 0)diff2 *= -1; diff1 *= (2*n+1); diff2 *= (2*n-1); /*float f1 = ((h+c)*n - c)/(2*n-1) - t; float f2 = ((h+c)*(n+1) - c)/(2*n+1) - t; System.out.println(f1+","+f2+"," +diff1+","+diff2); if(f1 < 0)f1 *= -1; if(f2 < 0)f2 *= -1;*/ if(diff1 <= diff2)return n+n-1; else return n+n+1; } return (n+n-1); } public static void main(String[] args)throws Exception{ Sola3 sd = new Sola3(); BufferedReader obj = new BufferedReader(new InputStreamReader(System.in)); String lineNo = obj.readLine(); int lines = Integer.parseInt(lineNo); while(lines-- > 0 ){ String line = obj.readLine(); String[] d = line.split(" "); int n = Integer.parseInt(d[0]); int m = Integer.parseInt(d[1]); int k = Integer.parseInt(d[2]); System.out.println(sd.minCups(n, m, k)); } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys input = sys.stdin.buffer.readline def print(val): sys.stdout.write(str(val) + '\n') def val(x,h,c): return (h-c)/(2*(x*2 -1)) def prog(): for _ in range(int(input())): L = 1 R = 10**6 h,c,t = map(int,input().split()) if t <= (h+c)/2: print(2) else: t -= (h+c)/2 while R > L: m = (R+L)//2 temp = val(m,h,c) if temp > t: L = m+1 else: R = m-1 left = val(L,h,c) if left > t: right = val(L+1,h,c) if abs(t-left) <= abs(t-right): print(2*L-1) else: print(2*L+1) else: right = val(L-1,h,c) if abs(t-left) >= abs(t-right): print(2*L-3) else: print(2*L-1) prog()
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.nio.file.Path; import java.util.*; public class Solution { static class Pair{ String s; int pos; Pair(String s, int pos){ this.s = s; this.pos = pos; } } static class PairInt{ int first; int second; PairInt(int first,int second){ this.first = first; this.second = second; } } static class PairPair{ PairInt first; int second; PairPair(PairInt first,int second){ this.first = first; this.second = second; } } @SuppressWarnings("unchecked") public static void main(String[] args) throws IOException { FastScanner fs = new FastScanner(); int inputs = fs.nextInt(); while (inputs-- > 0){ int hot = fs.nextInt(); int cold = fs.nextInt(); int reqTemp = fs.nextInt(); if(reqTemp>=hot){ System.out.println(1); }else if((hot+cold)/2 >= reqTemp){ System.out.println(2); }else{ int cups = (hot-reqTemp)/(2*reqTemp-hot-cold); int firstNum = Math.abs((hot*(cups+1)+cold*cups)-reqTemp*(2*cups+1)); int firstDeno = cups*2+1; int secondNum = Math.abs((hot*(cups+2)+cold*(cups+1))-reqTemp*(2*(cups+1)+1)); int secondDeno = cups*2+3; if(firstNum*secondDeno<=secondNum*firstDeno){ System.out.println(2*cups+1); }else{ System.out.println(2*cups+3); } } } } static long binaryExponentiation(long a,long b){ long res = 1; while(b>0){ if((b&1) == 1){ res = res*a; } a = a*a; b = b>>1; } return res; } void reverseSort(int[] arr){ Arrays.sort(arr); } 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[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArray(int n,String str) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { 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
import java.util.*; import java.io.*; public class _1359_C { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(in.readLine()); for(int i = 0; i < t; i++) { StringTokenizer line = new StringTokenizer(in.readLine()); int h = Integer.parseInt(line.nextToken()); int c = Integer.parseInt(line.nextToken()); int target = Integer.parseInt(line.nextToken()); if((h + c) / (double)2 >= target) { out.println(2); continue; } long lbound = 0; long rbound = (long)Math.pow(10, 18); long res = -1; long bigger = -1; while(lbound < rbound) { long avg = (lbound + rbound) / 2; double temp = calctemp(avg, h, c); if(temp > target) { bigger = Math.max(bigger, avg); lbound = avg + 1; }else if(temp < target) { rbound = avg; }else { res = 2 * avg + 1; break; } } if(res > -1) { out.println(res); }else { long num1 = h * (bigger + 1) + c * bigger - target * (2 * bigger + 1); long b2 = bigger + 1; long num2 = target * (2 * b2 + 1) - h * (b2 + 1) - c * b2; long cross1 = num1 * (2 * b2 + 1); long cross2 = num2 * (2 * bigger + 1); if(cross1 <= cross2) { out.println(bigger * 2 + 1); }else { out.println((bigger + 1) * 2 + 1); } } } in.close(); out.close(); } static double calctemp(long x, int h, int c) { return (double)(x + 1) / (2 * x + 1) * h + (double)x / (2 * x + 1) * c; } }
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 fractions def calc_temp(h, c, curr): numerator = h * curr[0] + c * curr[1] denominator = curr[0] + curr[1] return fractions.Fraction(numerator, denominator) def solve(h, c, t): if h == t or t >= (c + 5 / 6 * (h - c)): return 1 if t <= (h + c) / 2: return 2 start = 1 end = pow(10, 10) curr = None while start <= end: mid = (start + end) // 2 curr = (mid + 1, mid) current_temperature = calc_temp(h, c, curr) if current_temperature == t: return curr[0] + curr[1] if current_temperature < t: end = mid - 1 if current_temperature > t: start = mid + 1 ans = None deviation = h - t for i in range(-3, 3, 1): if curr[1] < 0: continue temp = (curr[0] + i, curr[1] + i) current_temperature = calc_temp(h, c, temp) if abs(current_temperature - t) < deviation: deviation = abs(current_temperature - t) ans = temp[0] + temp[1] return ans def main(): t = int(input()) test_case_number = 1 while test_case_number <= t: h, c, temp = map(int, input().split()) print(solve(h, c, temp)) test_case_number += 1 main()
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
T = int(input()) for _ in range(T): h,c,t = map(int,input().split()) if h+c >= 2*t: print(2) else: x = (h-t)//(2*t-c-h) y = x+1 if abs(h*(x+1)+c*x-t*(2*x+1))*(2*y+1) <= abs(h*(y+1)+c*y-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
#include <bits/stdc++.h> using namespace std; const long long int INF = (long long int)1e18; const long long int MOD = (long long int)1e9 + 7; const long long int maxn = (long long int)2e5 + 10, L = 23; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int T; cin >> T; while (T--) { long double h, c, t; cin >> h >> c >> t; long long int ans = 1; double lst = 1e18; if (abs(h - t) <= abs((h + c) / 2.0 - t)) { ans = 1, lst = abs(h - t); } else { ans = 2, lst = abs((h + c) / 2.0 - t); } long double delta = t - (h + c) / 2.0; long long int inx = (h - c) / delta / 2.0; if (inx % 2 == 0) --inx; for (long long int i = max(3ll, inx - 1000); i <= inx + 1000; i += 2) { long double now = (h + c) / 2.0 + (h - c) / i / 2.0; if (lst > abs(now - t)) { lst = abs(now - t); ans = i; } } 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
#-------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- from math import gcd, ceil def pre(s): n = len(s) pi = [0] * n for i in range(1, n): j = pi[j - 1] while j and s[i] != s[j]: j = pi[j - 1] if s[i] == s[j]: j += 1 pi[i] = j return pi def prod(a): ans = 1 for each in a: ans = (ans * each) return ans def lcm(a, b): return a * b // gcd(a, b) def binary(x, length=16): y = bin(x)[2:] return y if len(y) >= length else '0' * (length - len(y)) + y def solve(h, c, t): def temp(x): return abs((t * (2 * x + 1) - ((x + 1) * h + x * c)) / (2 * x + 1)) if t <= (h + c) // 2: return 2 L, R = 0, 1e7 while R != L: s = (R - L) // 3 m1 = L + s m2 = R - s if temp(m2) >= temp(m1): R = m2 - 1 else: L = m1 + 1 return int(2 * R + 1) T = int(input()) while T: h, c, t = map(int, input().split()) print(solve(h, c, t)) T -= 1
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys __author__ = 'ratmir' from collections import deque from math import asin, pi, tan alphabet = "abcdefghijklmnopqrstuvwxyz" def solve(n, a, graph): return 1 def execute(): [t] = [int(x) for x in sys.stdin.readline().split()] results = [] for ti in range(0, t): [h, c, t] = [int(x1) for x1 in sys.stdin.readline().split()] if t == h: results.append(1) continue if 2 * t <= h + c: results.append(2) continue # h + x*(h+c) < t * (2x+1) # h +x*(h+c) < x * 2t + t # h - t < x*(2t-h-c) # x> (h-t)/(2t-h-c) mch = h - t mzn = 1 mn = 1 c_ = 1.0 * (h - t) / (2 * t - h - c) i = int(c_) to_try = [i-2, i - 1, i, i + 1, i+2] for x in to_try: if x > 0: diffch = abs(t * (2 * x + 1) - (h + x * (h + c))) diffzn = 2 * x + 1 #print 2*x+1, diffch, diffzn if diffch*mzn < mch*diffzn: mch = diffch mzn = diffzn mn = 2 * x + 1 results.append(mn) print(''.join('{}\n'.format(k) for k in results)) execute()
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.*; import java.util.*; public class Contest1 { static int[][][]memo; static int[]a; static int dp(int state,int mx,int idx){ if (idx==a.length) return state==0?0:-(mx-30); if (memo[state][mx][idx]!=-1) return memo[state][mx][idx]; int ans=0; if (state==0){ ans = dp(0,mx,idx+1); ans = Math.max(ans,dp(1,a[idx]+30,idx+1)+a[idx]); } else { ans=-(mx-30); ans = Math.max(ans,dp(1,Math.max(a[idx]+30,mx),idx+1)+a[idx]); } return memo[state][mx][idx]=ans; } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-->0) { int h = sc.nextInt(); int c = sc.nextInt(); int t1 = sc.nextInt(); int ans=2; double avg = (c+h)/2.0; if (t1<=avg){ pw.println(2); continue; } // if (t1==h){ //// pw.println(1); //// continue; //// } long[]diff = new long[2]; diff[0]=1000000000l; diff[1]=1; int ans2=0; int low =0; int hi =(int)1e9; while (low<=hi){ int mid = low+hi>>1; long v = 1l*h*(mid+1)+1l*c*mid; if (1.0*v/(2*mid+1)>=t1){ low=mid+1; ans2=mid; diff[0]=v-t1*(2l*(mid)+1); diff[1]=2*mid+1; } else { hi=mid-1; } } ans=ans2+1; long[]diff2= new long[2]; diff2[0] = 1l*h*(ans+1)+1l*c*(ans)-t1*(2l*ans+1); diff2[1] =2*ans+1; diff[0]=Math.abs(diff[0]); diff2[0]=Math.abs(diff2[0]); if (diff[0]*diff2[1]-diff2[0]*diff[1]>0){ ans2=ans; } pw.println(2*ans2+1); } pw.flush(); } static long gcd(long a,long b){ if (a==0) return b; return gcd(b%a,a); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
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.*; import java.util.*; import java.math.*; import java.awt.Point; public class Main { //static final long MOD = 998244353L; //static final long INF = 1000000000000000007L; static String letters = "abcdefghijklmnopqrstuvwxyz"; static final long MOD = 1000000007L; static final int INF = 1000000007; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int Q = sc.ni(); for (int q = 0; q < Q; q++) { long H = sc.nl(); long C = sc.nl(); long T = sc.nl(); if (2*T <= H+C) { pw.println(2); continue; } else { int low = 0; int high = 10000000; while (low < high-1) { int med = (low+high)/2; long num = H*(med+1)+C*med; long den = 2*med+1; if (num == den*T) { low = med; high = med; break; } else if (num < den*T) { high = med; } else { low = med; } } long tLnum = H*(low+1)+C*(low); long tLden = 2*low+1; long tHnum = H*(high+1)+C*(high); long tHden = 2*high+1; long lv = (tLnum-T*tLden)*tHden; long hv = (T*tHden-tHnum)*tLden; if (lv <= hv) { pw.println(2*low+1); } else { pw.println(2*high+1); } } } pw.close(); } public static long dist(long[] p1, long[] p2) { return (Math.abs(p2[0]-p1[0])+Math.abs(p2[1]-p1[1])); } //Find the GCD of two numbers public static long gcd(long a, long b) { if (a < b) return gcd(b,a); if (b == 0) return a; else return gcd(b,a%b); } //Fast exponentiation (x^y mod m) public static long power(long x, long y, long m) { if (y < 0) return 0L; long ans = 1; x %= m; while (y > 0) { if(y % 2 == 1) ans = (ans * x) % m; y /= 2; x = (x * x) % m; } return ans; } public static int[] shuffle(int[] array) { Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); int temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } return array; } public static long[] shuffle(long[] array) { Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); long temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } return array; } public static int[][] shuffle(int[][] array) { Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); int[] temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } return array; } public static int[][] sort(int[][] array) { //Sort an array (immune to quicksort TLE) Arrays.sort(array, new Comparator<int[]>() { @Override public int compare(int[] a, int[] b) { return a[0]-b[0]; //ascending order } }); return array; } public static long[][] sort(long[][] array) { //Sort an array (immune to quicksort TLE) Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); long[] temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } Arrays.sort(array, new Comparator<long[]>() { @Override public int compare(long[] a, long[] b) { if (a[0] < b[0]) return -1; else return 1; } }); return array; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { 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 ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { double h, c, t; cin >> h >> c >> t; long long ans = 0; long long l = 0; long long r = 1000000; long long diff = 10000000000; double cnt = 100000000.100; while (l <= r) { long long mid = l + (r - l) / 2; double calc = (h * (mid + 1) + mid * c) / (2 * mid + 1); if (calc > t) { l = mid + 1; } else { r = mid - 1; } if (abs(calc - t) < cnt) { diff = mid * 2 + 1; cnt = abs(calc - t); } if (abs(calc - t) == cnt) { diff = min(diff, 2 * mid + 1); } } ans = diff; if (t == h) { ans = 1; } if (t <= (c + h) / 2) { ans = 2; } cout << ans << endl; } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from sys import stdin input = stdin.readline 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 l, r = 1, 1000000000 while l < r: m = (l + r) >> 1 if m * h + (m - 1) * c > (2 * m - 1) * t: l = m + 1 else: r = m k = l k3 = k - 1 if k3 > 0 and 2 * t * (2 * k - 1) * (2 * k3 - 1) >= (k * h + (k - 1) * c) * (2 * k3 - 1) + (k3 * h + (k3 - 1) * c) * (2 * k - 1): k, x = k3, (k3 * h + (k3 - 1) * c) / (2 * k3 - 1) else: x = (k * h + (k - 1) * c) / (2 * k - 1) mn = min(abs(h - t), abs((h + c) / 2 - t), abs(x - t)) if abs(h - t) == mn: print(1) elif abs((h + c) / 2 - t) == mn: print(2) 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 fractions import Fraction as frac t = int(input()) for _ in range(t): hotTemp, coldTemp, barrelTemp = map(int, input().rstrip().split()) if barrelTemp == hotTemp: print(1) elif (hotTemp+coldTemp) >= barrelTemp*2: print(2) else: cups1 = (barrelTemp - coldTemp) // ((2 * barrelTemp) - (hotTemp + coldTemp)) cups2 = cups1+1 finalTemp1 = frac((hotTemp * cups1) + (coldTemp * (cups1 - 1)), (2 * cups1 - 1)) finalTemp2 = frac((hotTemp*cups2) + (coldTemp*(cups2-1)),(2*cups2 - 1)) if abs(barrelTemp - finalTemp1) <= abs(barrelTemp - finalTemp2): cups1 = 2*cups1 - 1 print(cups1) else: cups2 = 2*cups2 - 1 print(cups2)
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; long long int ans = 1; double dif = (h - t) * 1.0, tdif = (c + h) * (1.0) / (2.0); if (abs(tdif - t) < dif) { dif = abs(tdif - t); ans = 2; } long long int l = 1, r = 10000000; while (l <= r) { long long int mid = (l + r) / 2; double tdif1 = ((mid * (c + h) - c) * (1.0)) / (2 * mid - 1); double tdif2 = (((mid + 1) * (c + h) - c) * (1.0)) / (2 * (mid + 1) - 1); double ab1 = abs(t - tdif1); double ab2 = abs(t - tdif2); if (ab1 > ab2) { if (ab2 < dif) { dif = ab2; ans = 2 * mid + 1; } else if (ab2 == dif) ans = min(ans, 2 * mid + 1); l = mid + 1; } else { if (ab1 < dif) { dif = ab1; ans = 2 * mid - 1; } else if (ab1 == dif) ans = min(ans, 2 * mid - 1); r = mid - 1; } } cout << ans << "\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
// Working program using Reader Class import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.*; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { //Reader sc=new Reader(); Scanner sc=new Scanner(System.in); int t=sc.nextInt(); StringBuilder print=new StringBuilder(); while(t-->0){ long h=sc.nextInt(),c=sc.nextInt(),T=sc.nextInt(); if(2*T<=(h+c)){ print.append("2\n"); continue; } double avg=(h+c)/2.0; double x=T-avg; double k=h-avg; long ans=(long)Math.round(k/x); ans+=1-ans%2; long a=ans/2+1,b=ans/2; double diff1=T*ans - (a*h*1.0+b*c*1.0); double diff2=T*(1.0*ans-2.0)- ((a-1)*h*1.0+(b-1)*c*1.0); //System.out.println( diff1+" "+diff2+" "+a+" "+b); if( Math.abs(diff1*(ans-2.0)) >= Math.abs(diff2*(ans)) )ans-=2; print.append(ans).append("\n"); } System.out.print(print); } }
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 io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline for _ in range(int(input())): h, c, t = map(int, input().split()) if h <= t: print(1) elif (h + c) // 2 >= t: print(2) else: n_c = (h - t) // (2 * t - h - c) n_h = n_c + 1 if abs((n_c * c + n_h * h) - t * (n_c + n_h)) * (n_c + n_h + 2) > abs((n_c * c + n_h * h + h + c) - t * (n_c + n_h + 2))*(n_c + n_h) : print(n_c + n_h + 2) else: print(n_c + n_h)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math def f(k,h,c,t): return ((2*k)+3)*abs(k*(h+c)+h-(2*t*k)-t)<=((2*k)+1)*abs((k+1)*(h+c)+h-(2*t*k)-(3*t)) t=int(input()) for _ in range(t): ar=input().split() h=int(ar[0]) c=int(ar[1]) t=int(ar[2]) if t==h: print(1) else: if 2*t<=h+c: print(2) else: z=((h-t)/(-h-c+2*t)) x=math.floor(z) #y=math.ceil(z) fuck=f(x,h,c,t) if fuck==1: 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
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 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 if h * (mid + 1) + c * mid >= t * (2 * mid + 1): 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
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder ans = new StringBuilder(); int tt = sc.nextInt(); while (tt-- > 0) { int h = sc.nextInt(); int c = sc.nextInt(); int t = sc.nextInt(); if (2*t <= h+c) { ans.append(2).append('\n'); } else if (t == h) { ans.append(1).append('\n'); } else { long ng = 0; long ok = 1_000_000L; while (ng+1 < ok) { long m = (ok+ng)/2; if ((h+c)*m+h <= t*(2*m+1)) { ok = m; } else { ng = m; } } if ((2*ok-1)*((h+c)*ok+h)+(2*ok+1)*((h+c)*(ok-1)+h) > 2*t*(2*ok-1)*(2*ok+1)) { ans.append(ok*2+1).append('\n'); } else { ans.append(ok*2-1).append('\n'); } } } System.out.print(ans); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math from decimal import Decimal n = int(input()) for i in range(n): h,c,t = map(int,input().split()) if t<=(h+c)/2: print(2) continue avg = (h-t)/(2*t-h-c) a = max(0,math.floor(avg)+1) b = max(0,math.floor(avg)) t1 = Decimal((a+1)*h+a*c)/(2*a+1) t2 = Decimal((b+1)*h+b*c)/(2*b+1) ans = 0 if abs(t-t2)<=abs(t-t1): ans = b else: ans = a print(2*ans+1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h,c,t = map(int,input().split()) if(h==t): print(1) else: if(((h+c)/2) >= t): print(2) else: dif=(t-((h+c)/2));j=int(abs((c-h)//(2*dif))) if(j%2==0): j+=1 if(dif-(h-c)/(2*j)>=abs(dif-(h-c)/(2*(j-2)))): print(j-2) else: print(j)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys input = sys.stdin.buffer.readline import math T = int(input()) for _ in range(T): h, c, t = map(int, input().split()) if t <= (h+c)/2: print(2) else: if (h-t)%(2*t-h-c)==0: x = (h-t)//(2*t-h-c) print(2*x+1) else: x = (h-t)/(2*t-h-c) x1 = math.floor(x) x2 = math.ceil(x) A = (h*(x1+1)+c*x1)*(2*x2+1)-t*(2*x1+1)*(2*x2+1) B = t*(2*x1+1)*(2*x2+1)-(h*(x2+1)+c*x2)*(2*x1+1) if A > B: print(2*x2+1) else: print(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 stdin ############################################################### def iinput(): return int(stdin.readline()) def sinput(): return input() def minput(): return map(int, stdin.readline().split()) def linput(): return list(map(int, stdin.readline().split())) ############################################################### T = iinput() while T: T-=1 h, c, t = minput() if h == t: print(1) elif (h+c)//2 >= t: print(2) else: k = (h - t) // (2 * t - h - c) if abs((k * (h + c) + h) - t * (2 * k + 1)) * (2 * k + 3) <= abs(((k + 1) * (h + c) + h) - t * (2 * k + 3)) * ( 2 * k + 1): print(2 * k + 1) else: print(2 * k + 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 sys import stdin, stdout from decimal import * cin = stdin.readline cout = stdout.write getcontext().prec = 50 for _ in range(int(cin())): h, c, t = map(int, cin().split()) if (h+c)/2 >= t: cout('2\n') continue i = (c-t) // (h+c-t-t) if abs(t - Decimal(h*i + c*(i-1))/Decimal(i+i-1)) <= abs(t - Decimal(h*(i+1) + c*i)/Decimal(i+i+1)): cout(str(i+i-1) + '\n') #print(abs(t - Decimal((h*i + c*(i-1))/(i+i-1))), abs(t - Decimal((h*(i+1) + c*i)/(i+i+1)))) else: cout(str(i+i+1) + '\n') #credit - arif vai ''' if max((h+c)/2, t) - min((h+c)/2, t) <= 1e-6: cout('2\n') continue i = (c-t) // (h+c-t-t) if i <= 0: cout('2\n') continue diff = Decimal(max(h, t) - min(h, t) + 1) #print(diff) i = max(1, i) #print('00001 ', i) while True: temp = Decimal(h*i + c*(i-1)) / Decimal(i+i-1) if Decimal(max(temp, t) - min(temp, t)) < diff: #print('i ', i) i += 1 diff = Decimal(max(temp, t) - min(temp, t)) print('d ', diff) else: #p = (h + c)/2 #if max((h + c)/2, t) - min((h + c)/2,t) <= diff: # cout('2\n') #else: cout(str(i+i-1-2) + '\n') break '''
PYTHON3