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.*; import java.math.BigDecimal; import java.math.MathContext; import java.text.*; public class C1359 { static int c, h, t; static long ans; static BigDecimal best; static double eps = 0; public static void solve(long k) { BigDecimal bc = BigDecimal.valueOf(c); BigDecimal bk = BigDecimal.valueOf(k); BigDecimal bh = BigDecimal.valueOf(h); BigDecimal bt = BigDecimal.valueOf(t); BigDecimal den = BigDecimal.valueOf(2).multiply(bk).add(BigDecimal.ONE); BigDecimal num = bk.multiply(bc.add(bh)).add(bh); BigDecimal bf = num.divide(den, new MathContext(32)); BigDecimal bd = bt.subtract(bf).abs(); if (bd.compareTo(best) == 0) { ans = Math.min(ans, 2 * k + 1); } else { if (bd.compareTo(best) < 0) { best = bd; ans = 2 * k + 1; } } } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tc = sc.nextInt(); while (tc-- > 0) { h = sc.nextInt(); c = sc.nextInt(); t = sc.nextInt(); ans = 2; best = BigDecimal.valueOf(Math.abs(t - (c + h) / 2.0)); double k = 1.0 * (t - h) / (c + h - 2.0 * t); long k1 = (long) Math.floor(k); long k2 = k1 + 1; if (k1 >= 0) solve(k1); if (k2 >= 0) solve(k2); // pw.println(k); solve(0); pw.println(ans); } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } 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 { return Double.parseDouble(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextInt(); return arr; } 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
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int h, c, T; cin >> h >> c >> T; if (h == T) { cout << 1 << endl; continue; } else if ((h + c) >= 2 * T) { cout << 2 << endl; } else { long long int x, y; long double p, q, dif1, dif2; x = (T - c) / (2 * T - h - c); y = x + 1; p = ((h * x) + (c * (x - 1))) / (1.0 * (2 * x - 1)); q = ((h * y) + (c * (y - 1))) / (1.0 * (2 * y - 1)); dif1 = T - p; if (dif1 < 0) dif1 *= -1; dif2 = (T - q); if (dif2 < 0) dif2 *= -1; if (dif1 <= dif2) cout << 2 * x - 1 << endl; else cout << 2 * y - 1 << endl; } } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; int main() { int T, c, h, t; cin >> T; while (T--) { cin >> h >> c >> t; if (2 * t <= h + c) cout << "2" << endl; else { if (t == h) cout << "1" << endl; else { double n; n = (double)(h - t) / (2 * t - h - c); int nmin = floor(n); int nmax = ceil(n); double tmin = h - (double)(h - c) * nmax / (2 * nmax + 1); double tmax = h - (double)(h - c) * nmin / (2 * nmin + 1); if ((tmax - t) <= (t - tmin)) cout << 2 * nmin + 1 << endl; else cout << 2 * nmax + 1 << endl; } } } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
def getCups(h,c,t,x): x = round(x) minDiff = (100000000000,1) cups = -100 for i in range(int(x)-1-2, int(x)+2+2): # print(i) if i % 2 == 1: # v = (h*((i+1)/2) + c*((i-1)/2))/i v = (h*((i+1)/2) + c*((i-1)/2), i) else: v = ((h+c)/2, 1) diff = (abs(v[0]-t*i), i) # print(i, abs(v-t)) if diff[0]*minDiff[1] < minDiff[0]*diff[1]: minDiff = diff cups = i return cups t = int(input()) for _ in range(t): h,c,t = [int(i) for i in input().split()] avg = (h+c)/2 if t >= h: print(1) elif t <= avg: print(2) else: x = (h-c)/(2*t-h-c) print(getCups(h,c,t,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 import math as mt from decimal import * getcontext().prec = 50 input=sys.stdin.buffer.readline def cal(i,c,h): return (Decimal(i*(c+h)+h)/Decimal((2*i)+1)) t=int(input()) #t=1 for __ in range(t): #n=int(input()) #l=list(map(int,input().split())) h,c,temp=map(int,input().split()) #print(temp,(h+c)/2) if temp<=(h+c)/2: print(2) else: #i1=(499979-1)/2 #i2=(499981-1)/2 #print(i1,i2) #print(abs((cal(i1,c,h))-(temp)),abs(((cal(i2,c,h))-(temp)))) #print(abs(cal((499979-1)//2,c,h)-temp),abs(cal(y+1,c,h)-temp)) y=mt.floor((temp-h)/(c+h-(2*temp))) #print(y) if abs(cal(y,c,h)-temp)<=abs(cal(y+1,c,h)-temp): print(2*y+1) else: y+=1 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; template <typename T> void read(T &n) { n = 0; T f = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1, c = getchar(); while (isdigit(c)) n = n * 10 + c - '0', c = getchar(); n *= f; } template <typename T> void write(T n) { if (n < 0) putchar('-'), n = -n; if (n > 9) write(n / 10); putchar(n % 10 + '0'); } long long a, b, c; long double calc(int x) { return 1.0 * ((x + 1) * a + x * b) / (2 * x + 1); } int main() { int T; cin >> T; while (T--) { scanf("%lld%lld%lld", &a, &b, &c); if (c == a) puts("1"); else if (c * 2 <= a + b) puts("2"); else { long long x = floor(1.0 * (c - a) / (a + b - 2 * c)); long double ans1 = calc(x), ans2 = calc(x + 1); long long x1 = ((x + 1) * a + x * b - c * (2 * x + 1)) * (2 * x + 3), x2 = (-(x + 2) * a - (x + 1) * b + (2 * x + 3) * c) * (2 * x + 1); if (x1 <= x2) { printf("%d\n", 2 * x + 1); } else printf("%d\n", 2 * x + 3); } } 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
/* If you want to aim high, aim high Don't let that studying and grades consume you Just live life young ****************************** What do you think? What do you think? 1st on Billboard, what do you think of it Next is a Grammy, what do you think of it However you think, I’m sorry, but shit, I have no fucking interest ******************************* Higher, higher, even higher, to the point you won’t even be able to see me https://www.a2oj.com/Ladder16.html ******************************* NEVER DO 300IQ CONTESTS EVER 300IQ AS WRITER = EXTRA NONO */ import java.util.*; import java.io.*; import java.math.*; public class x1359C { static double EPS = -1e8; public static void main(String omkar[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int Tt = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(Tt-->0) { st = new StringTokenizer(infile.readLine()); long H = Long.parseLong(st.nextToken()); long C = Long.parseLong(st.nextToken()); long T = Long.parseLong(st.nextToken()); if(2*T <= H+C) sb.append("2\n"); else { long low = 1L; long high = 1000000000L; while(low != high) { long mid = (low+high+1)/2; if(T*(2*mid-1) <= mid*H+(mid-1)*C) low = mid; else high = mid-1; } //just one long res = low; double num1 = Math.abs(T*(2*low-1)-(res*H+(res-1)*C)); double dem1 = 2*low-1; double num2 = Math.abs(T*(2*low+1)-((res+1)*H+C*res)); double dem2 = 2*low+1; if(num1*dem2 <= num2*dem1) res = 2*res-1; else res = 2*res+1; sb.append(res+"\n"); } } System.out.print(sb); } public static double calc(long H, long C, long T, long mid) { double res = mid*H+(mid-1)*C; res /= (2*mid-1); return Math.abs(res-T); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; template <typename T> void write(vector<T> &a) { for (auto it = a.begin(); it != a.end(); it++) cout << *it << " "; cout << "\n"; } void random(long long int nMin, long long int nMax, long long int n) { srand(time(0)); for (long long int i = 0; i < n; i++) { double var = nMin + (long long int)((double)rand() / ((double)RAND_MAX + 1) * (nMax - nMin + 1)); cout << var << " "; } cout << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << setprecision(15); { long long int T; cin >> T; while (T--) { double h, c, t; cin >> h >> c >> t; double mean = (h + c) / 2; if (t >= h) { cout << 1 << "\n"; continue; } if (t <= mean) { cout << 2 << "\n"; continue; } double total, count, minDiff, avg, resCount; minDiff = abs(h - t); resCount = 1; avg = LLONG_MAX; if (abs(mean - t) < minDiff) { resCount = 2; minDiff = abs(mean - t); } long long int x = (c - t) / (h + c - 2 * t); double bef = (x * h + x * c - c) / (2 * x - 1); x++; double after = (x * h + x * c - c) / (2 * x - 1); bef = abs(bef - t); after = abs(after - t); if (bef <= after) { cout << 2 * (x - 1) - 1 << "\n"; } else cout << 2 * 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
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; void solve() { long long h, c, t; cin >> h >> c >> t; double ans = h - t; long long taken = 1; if (abs((h + c + 0.0) / 2 - t) <= ans) { ans = abs((h + c + 0.0) / 2 - t); taken = 2; } double y = (h - t + 0.0) / (2 * t - (h + c)); if (y >= 0) { long long x = (int)y; if (abs(((x + 1) * h + x * c + 0.0) / (2 * x + 1) - t) <= ans) { ans = abs(((x + 1) * h + x * c + 0.0) / (2 * x + 1) - t); taken = 2 * x + 1; } ++x; if (abs(((x + 1) * h + x * c + 0.0) / (2 * x + 1) - t) <= ans) { ans = abs(((x + 1) * h + x * c + 0.0) / (2 * x + 1) - t); taken = 2 * x + 1; } --x; if (abs(((x + 1) * h + x * c + 0.0) / (2 * x + 1) - t) <= ans) { ans = abs(((x + 1) * h + x * c + 0.0) / (2 * x + 1) - t); taken = 2 * x + 1; } } cout << taken << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int MAX = 100000000; static double h, c, t; public static void main(String[] args) throws Exception { Reader.init(System.in); int T = Reader.nextInt(), cups; StringBuilder sb = new StringBuilder(); for (int i = 0; i < T; i++) { h = Reader.nextInt(); c = Reader.nextInt(); t = Reader.nextInt(); cups = quickCheck(h, c, t); sb.append(cups); sb.append("\n"); } System.out.println(sb.toString()); } public static int quickCheck(double h, double c, double t) { if (t == h) { return 1; } if (t * 2 <= h + c) { return 2; } double k = (h-t)/(2*t-h-c); double kFloor = Math.floor(k); double kCeil = Math.ceil(k); if (kFloor == kCeil) { return (int)k * 2 + 1; } double diffFloor = cmpF(kFloor); double diffCeil = cmpC(kFloor); if (diffFloor <= diffCeil) { // <= because we want minimum num of cups return (int)kFloor * 2 + 1; } else { return (int)kCeil * 2 + 1; } } public static double cmpF(double k) { return Math.abs( (k*(h+c)+h) - t*(2*k+1) )*(2*k+3); } public static double cmpC(double k) { return Math.abs( ((k+1)*(h+c)+h) - t*(2*k+3) )*(2*k+1); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; int main() { int T; long long h, c, t, lo, hi, mid, l; scanf("%d", &T); for (int tc = 0; tc < T; tc++) { scanf("%lld%lld%lld", &h, &c, &t); if ((t << 1) <= h + c) printf("2\n"); else { lo = 0; hi = 500005; while ((hi - lo) > 1) { mid = (lo + hi) >> 1; if ((mid + 1) * h + mid * c <= t * ((mid << 1) + 1)) hi = mid; else lo = mid; } l = lo; if ((2 * l + 3) * (l * h + l * c + h) + (2 * l + 1) * (l * h + l * c + 2 * h + c) <= 2 * t * (2 * l + 1) * (2 * l + 3)) printf("%lld\n", (lo << 1) + 1); else printf("%lld\n", (lo << 1) + 3); } } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math T = int(input()) for _ in range(T): h,c,t = map(int,input().split()) if (2*t<=h+c): print(2) else: A = (h-t)//((2*t)-h-c) k = 2*A+1 Ans = 100000000000 ans = 0 for i in range(k+3,k-1,-1): A = abs((((i+1)//2)*h+(i//2)*c)-t*i) if (A*ans<=Ans*i): Ans = A ans = i print(ans)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys from fractions import Fraction 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(Fraction(t) - Fraction(x * h + (x-1) * c) / Fraction(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(Fraction(t) - Fraction(h+c)/Fraction(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
""" ____ _ _____ / ___|___ __| | ___| ___|__ _ __ ___ ___ ___ | | / _ \ / _` |/ _ \ |_ / _ \| '__/ __/ _ \/ __| | |__| (_) | (_| | __/ _| (_) | | | (_| __/\__ \ \____\___/ \__,_|\___|_| \___/|_| \___\___||___/ """ """ ░░██▄░░░░░░░░░░░▄██ ░▄▀░█▄░░░░░░░░▄█░░█░ ░█░▄░█▄░░░░░░▄█░▄░█░ ░█░██████████████▄█░ ░█████▀▀████▀▀█████░ ▄█▀█▀░░░████░░░▀▀███ ██░░▀████▀▀████▀░░██ ██░░░░█▀░░░░▀█░░░░██ ███▄░░░░░░░░░░░░▄███ ░▀███▄░░████░░▄███▀░ ░░░▀██▄░▀██▀░▄██▀░░░ ░░░░░░▀██████▀░░░░░░ ░░░░░░░░░░░░░░░░░░░░ """ import sys import math import collections from collections import deque #sys.stdin = open('input.txt', 'r') #sys.stdout = open('output.txt', 'w') from functools import reduce from sys import stdin, stdout, setrecursionlimit setrecursionlimit(2**20) def factors(n): return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) for _ in range(int(stdin.readline())): # n = int(stdin.readline().strip('\n')) # b = str(stdin.readline().strip('\n')) h, c, t = list(map(int, stdin.readline().split())) # n, m = list(map(int, stdin.readline().split())) # s = list(str(stdin.readline().strip('\n'))) # n = len(a) #k = int(stdin.readline().strip('\n')) if h == t: print(1) elif t <= (h + c) / 2: print(2) else: k = (t - c - 1) // (2 * t - h - c) ans = 2 * k + 1 if(((4 * k * k - 1) * (2 * t - h - c)) >= (2 * (h - c) * k)): 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
// practice with kaiboy import java.io.*; import java.util.*; public class CF1359C extends PrintWriter { CF1359C() { super(System.out); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1359C o = new CF1359C(); o.main(); o.flush(); } void main() { int q = sc.nextInt(); while (q-- > 0) { int h = sc.nextInt(); int c = sc.nextInt(); int t = sc.nextInt(); int ans; if (t * 2 <= h + c) ans = 2; else { int x = (h - t) / (t * 2 - h - c); int y = x + 1; int qx = x * 2 + 1; int px = x * (h + c) + h - qx * t; int qy = y * 2 + 1; int py = qy * t - y * (h + c) - h; ans = ((long) px * qy <= (long) py * qx ? x : y) * 2 + 1; } 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.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 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; MyScanner in = new MyScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, MyScanner in, PrintWriter out) { int T = in.Int(); while (T-- > 0) { int h = in.Int(); int c = in.Int(); int t = in.Int(); if (t >= h) { out.println(1); continue; } if (t <= (h + c) / 2) { out.println(2); continue; } double low = 0, hi = 1e9; for (int i = 0; i < 1000; i++) { double ll = (low * 2 + hi) / 3; double hh = (low + hi * 2) / 3; double temp1 = calc(h, c, t, ll); double temp2 = calc(h, c, t, hh); if (temp1 < temp2) { hi = hh; } else { low = ll; } } long ans = (long) low; double temp1 = calc(h, c, t, ans); double temp2 = calc(h, c, t, ans + 1); if (Double.compare(temp1, temp2) > 0) { ans++; } out.println(ans * 2 + 1); } } private double calc(int h, int c, int t, double val) { double x = t * (val * 2 + 1); double y = h * (val + 1); double z = c * val; return Math.abs(x - (y + z)) / (val * 2 + 1); } } static class MyScanner { private BufferedReader in; private StringTokenizer st; public MyScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { String rl = in.readLine(); if (rl == null) { return null; } st = new StringTokenizer(rl); } catch (IOException e) { return null; } } return st.nextToken(); } public int Int() { 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
// package com.company; import java.util.*; import java.lang.*; import java.io.*; //****Use Integer Wrapper Class for Arrays.sort()**** public class DE3 { static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] Args){ FastReader scan=new FastReader(); int t=1; t=scan.nextInt(); while(t-->0){ long h=scan.nextInt(); long c=scan.nextInt(); long tt=scan.nextInt(); if(tt==h){ out.println(1); }else if(tt*2<=h+c){ out.println(2); }else{ int l=1; int r=(int)1e6; // h=(long)1e6; // c=(long)1; // out.println(r); int in=1; // out.println((r*(h+c)+h)/(double)(2*r+1)); while(l<=r){ int mid=(l+r)/2; long form=0; form=(h+c)*mid+h; long comp=tt*(2*mid+1); // out.println(comp-form); if(form<=comp){ in=mid; r=mid-1; }else{ l=mid+1; } } int ans=in; long f1=tt*(2*in+1)-(h+c)*in-h; long f2=(h+c)*(in-1)+h-tt*(2*in-1); if(f1*(2*in-1)>=f2*(2*in+1)){ ans=in-1; } out.println(ans*2+1); } } out.flush(); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.*; public class solution { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); for(int tt=0;tt<t;tt++) { long h=scan.nextInt(), c=scan.nextInt(), target=scan.nextInt(); long l=-1, r=100000000L; while (r-l>1) { long mid=(r+l)/2; if (go(mid*2+1,h,c,target).greater(go((mid+1)*2+1,h,c,target))) { l=mid; } else { r=mid; } } long res=(l+1)*2+1; if(!go(2,h,c,target).greater(go(res,h,c,target))) { res=2; } if(!go(1,h,c,target).greater(go(res,h,c,target))) { res=1; } System.out.println(res); } } public static frac go(long times, long h, long c, long target) { long num=0, den=0; if(times%2==0) { num=h+c; den=2; } else { num+=(h+c)*(times/2); num+=h; den=times; } frac f=new frac(num,den); return f.dif(target); } static class frac { long num,den; frac(long num, long den) { this.num=num; this.den=den; } boolean greater(frac o) { return num*o.den>o.num*den; } frac dif(long x) { frac res=new frac(num,den); res.num-=x*res.den; res.num=Math.abs(res.num); return res; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h,c,t = list(map(int, input().split())) av = (h+c)/2 if(t >= h): print(1) continue elif(t<=av): print(2) continue x = (h-t)//(2*t - (h+c)) t1 = ((x+1)*h + x*c) t2 = ((x+2)*h + (x+1)*c) if(abs(t*(2*x+1)-t1)*(2*x+3) <= abs(t*(2*x+3)-t2)*(2*x+1)): print(x*2 + 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
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 9; const long long mod = 1e9 + 7; vector<bool> prime(MAX, 1); vector<int> spf(MAX, 1), primes; void sieve() { prime[0] = prime[1] = 0; spf[2] = 2; for (long long i = 4; i < MAX; i += 2) { spf[i] = 2; prime[i] = 0; } primes.push_back(2); for (long long i = 3; i < MAX; i += 2) { if (prime[i]) { primes.push_back(i); spf[i] = i; for (long long j = i * i; j < MAX; j += i) { prime[j] = 0; if (spf[j] == 1) { spf[j] = i; } } } } } template <typename T> void in(T &x) { cin >> x; } template <typename T, typename U> void in(T &x, U &y) { cin >> x >> y; } template <typename T, typename U, typename V> void in(T &x, U &y, V &z) { cin >> x >> y >> z; } template <typename T> void ps(T x) { cout << x << " "; } template <typename T> void ps(const vector<T> &x, int n) { for (int i = 0; i < n; i++) { cout << x[i]; (i == n - 1) ? cout << '\n' : cout << " "; } } template <typename T> void pl(T x) { cout << x << '\n'; } template <typename T> void pl(const vector<T> &x, int n) { for (int i = 0; i < n; i++) { cout << x[i] << "\n"; } } template <typename T> T power(T a, T b) { T res = 1; while (b) { if (b & 1) { res = res * a; } a = a * a; b = b >> 1; } return res; } template <typename T> T power(T a, T b, T m) { T res = 1; while (b) { if (b & 1) { res = (res * a) % m; } a = (a * a) % m; b = b >> 1; } return res % m; } void virtual_main() {} void real_main() { long long h, c, t; cin >> h >> c >> t; long long low = 1, high = 1000000000; long double m = (h + c) / 2.0; long double d = abs((long double)t - m); if (m >= (long double)t) { pl(2); return; } long long cnt = 100; while ((high - low) > 2) { long long pointer = (low + high) / 2; if (pointer % 2 == 0) { pointer--; } long long value = ((pointer + 1) / 2) * h + (pointer / 2) * c; if (value <= t * pointer) { long long ans1 = pointer; pointer -= 2; long long v1 = value; value = ((pointer + 1) / 2) * h + (pointer / 2) * c; if (value <= t * pointer) { high = pointer; continue; } long long d1 = abs(ans1 * t - v1), d2 = abs(pointer * t - value); if ((d1 * pointer) < (d2 * ans1)) { pl(ans1); return; } else { pl(pointer); return; } } else { low = pointer; } } long long v1 = ((h * ((low + 1) / 2) + (low / 2) * c)); long long v2 = ((h * ((high + 1) / 2) + (high / 2) * c)); long long d1 = abs(v1 - low * t), d2 = abs(v2 - high * t); if (d1 * high <= d2 * low) { cout << low << "\n"; return; } else { cout << high << "\n"; return; } cout << 1 << "\n"; } signed main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); virtual_main(); long long test_cases = 1; cin >> test_cases; for (long long i = 1; i <= test_cases; i++) { real_main(); } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
def mix(high, low, n, target): upp = abs((n + 1) * high + n * low - target * (2 * n + 1)) low = 2 * n + 1 return [upp, low] def solve(high, low, target): if 2 * target <= high + low: print(2) return left = 0 right = 1000 * 1000 * 1000 * 1000 while right - left > 30: mid1 = (2 * left + right) // 3 mid2 = (left + 2 * right) // 3 [u1, l1] = mix(high, low, mid1, target) [u2, l2] = mix(high, low, mid2, target) if u1 * l2 < u2 * l1: right = mid2 else: left = mid1 u = 1000 * 1000 * 1000 * 1000 * 1000 l = 1 res = -1 for i in range(left, right + 1): [u1, l1] = mix(high, low, i, target) if u1 * l < u * l1: u = u1 l = l1 res = i print(2 * res + 1) t = int(raw_input()) for _ in range(t): [h, c, t] = map(int, raw_input().split()) res = solve(h, c, t)
PYTHON
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("O3") using namespace std::chrono; using namespace std; long long mul(long long a, long long b, long long m = 1000000007) { long long res = 0; a = a % m; while (b > 0) { if (b % 2 == 1) res = (res + a) % m; a = (a * 2) % m; b /= 2; } return res % m; } long long expMod(long long a, long long b, long long m = 1000000007) { long long x = 1, y = a; while (b) { if (b % 2 == 1) { x = mul(x, y, m); } y = mul(y, y, m); b = b / 2; } return x; } long long pw(long long a, long long b) { long long c = 1, m = a; while (b) { if (b & 1) c = (c * m); m = (m * m); b /= 2; } return c; } long long pwmd(long long a, long long b) { long long c = 1, m = a; while (b) { if (b & 1) c = (c * m) % 1000000007; m = (m * m) % 1000000007; b /= 2; } return c; } long long mmi(long long a) { long long inverse = pwmd(a, 1000000007 - 2); return inverse; } long long nCrModp(long long n, long long r) { long long C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (long long i = 1; i < n + 1; ++i) for (long long j = min(i, r); j >= 1; --j) C[j] = (C[j] + C[j - 1]) % 1000000007; return C[r]; } int my_log(long long n, int b) { long long i = 1; int ans = 0; while (1) { if (i > n) { ans--; break; } if (i == n) break; i *= b; ans++; } return ans; } inline bool kthbit(long long n, int k) { return (n >> k) & 1; } inline long long setkthbit(long long n, int k) { return n | (1ll << k); } inline long long unsetkthbit(long long n, int k) { return n & ~(1ll << k); } inline long long flipkthbit(long long n, int k) { return n ^ (1ll << k); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long double T, c, h, tmp; cin >> T; while (T--) { cin >> h >> c >> tmp; if (h <= tmp) { cout << 1 << "\n"; continue; } long double mid = (h + c) / 2; if (tmp <= mid) { cout << 2 << "\n"; continue; } long double prev = 1e18; long double z = abs(tmp - ((h + c) / 2)); prev = min(prev, z); long long y = floor((tmp - c) / (2 * tmp - h - c)); long double r = (y * h + (y - 1) * c) / (2 * y - 1); r = abs(r - tmp); prev = min(r, prev); long double s = ((y + 1) * h + y * c) / (2 * (y + 1) - 1); s = abs(s - tmp); prev = min(s, prev); if (prev == z) cout << 2 << "\n"; else if (prev == r) cout << 2 * y - 1 << "\n"; else cout << 2 * (y + 1) - 1 << "\n"; } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; mt19937_64 mt(time(0)); long long gcd(long long a, long long b) { if (a > b) swap(a, b); if (a == 0) return b; return gcd(b % a, a); } pair<long long, long long> get_res(long long a, long long b, long long n, long long c) { long long x = (n + 1) * a + n * b; long long y = 2 * n + 1; x = abs(x - y * c); long long tmp = gcd(x, y); return {x / tmp, y / tmp}; } bool my_less(pair<long long, long long> x, pair<long long, long long> y) { return x.first * y.second < y.first * x.second; } int solve(int a, int b, int c) { if (c >= a) return 1; if ((a + b) >= c * 2) return 2; int tmp = (a - c) / (2 * c - a - b); pair<long long, long long> res = get_res(a, b, tmp, c); int ret = tmp; if (tmp - 1 >= 0) { pair<long long, long long> res1 = get_res(a, b, tmp - 1, c); if (my_less(res1, res)) { res = res1; ret = tmp - 1; } } pair<long long, long long> res2 = get_res(a, b, tmp + 1, c); if (my_less(res2, res)) { ret = tmp + 1; } return 2 * ret + 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(10); cout << fixed; int t; cin >> t; while (t--) { int a, b, c; cin >> a >> b >> c; cout << solve(a, b, c) << '\n'; } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from sys import stdin,stdout input=stdin.readline def print(x): stdout.write(str(x)+'\n') def solve(): h,c,t=map(int,input().split()) if t<=(h+c)/2: print(2) else: k=(h-t)//(2*t-h-c) k=2*k+1 c1= (k//2+1)*h + (k//2)*c - t*k c2= ((k+2)//2+1)*h + ((k+2)//2)*c - t*(k+2) if abs(c1)*(k+2)<=abs(c2)*(k): print(k) else: print(k+2) t=1 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
inp = lambda cast=int: [cast(x) for x in input().split()] printf = lambda s='', *args, **kwargs: print(str(s).format(*args), flush=True, **kwargs) t, = inp() for _ in range(t): h, c, t = inp() if 2*t <= h+c: print(2) else: x = (t-c)//(2*t-h-c) y = x+1 res1 = (x*h + (x-1)*c)*(2*y-1) - t*(2*x-1)*(2*y-1) res2 = t*(2*x-1)*(2*y-1) - (y*h + (y-1)*c)*(2*x-1) if res1 <= res2: print(2*x-1) else: print(2*y-1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#: Author - Soumya Saurav from collections import defaultdict from collections import OrderedDict from collections import deque from itertools import combinations from itertools import permutations import bisect,math,heapq,sys,io, os, time from decimal import Decimal start_time = time.time() ONLINE_JUDGE = __debug__ if ONLINE_JUDGE: input = sys.stdin.readline if not ONLINE_JUDGE: sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") alphabet = "abcdefghijklmnopqrstuvwxyz" from fractions import Fraction as f for tc in range(int(input())): H, C, T = map(int, input().split()) ans = f(H + C, 2) cups = 2 if abs(H - T) <= abs(ans - T): ans = H cups = 1 if H + C != 2 * T: n = (T - H) // (H + C - 2 * T) itr = max(0, n - 3) while itr <= n + 3: a, b = itr + 1, itr #print(a,b,H,C) temp = f(a * H + b * C, a + b) if abs(temp - T) < abs(ans - T): ans = temp cups = a + b itr += 1 print(cups) if not ONLINE_JUDGE: print("Time Elapsed :",time.time() - start_time,"seconds") sys.stdout.close()
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math t = int(input()) for _ in range(t): h, c, t = map(int, input().split()) t3 = (h + c) / 2 if t == c or 2 * t <= h + c: print(2) elif t == h: print(1) elif t3 == t: print(2) else: x1 = int((h - t) / (2 * t - h - c)) x2 = x1 + 1 if abs((x1 * (h + c) + h) - (2 * x1 + 1) * t) * (2 * x2 + 1) <= abs((x2 * (h + c) + h) - (2 * x2 + 1) * t) * (2 * x1 + 1): print(2 * x1 + 1) else: print(2 * x2 + 1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
def cups(x,h,c,t): error = (x+1) * h + x * c - t * (2*x+1) return abs(error) def f(h,c,t): if (h+c) >= 2*t: return 2 x = (h-t) // (2*t-h-c) e1 = cups(x,h,c,t) e2 = cups(x+1,h,c,t) if e1 * (2*x+3) <= e2 * (2*x+1): return 2*x+1 else: return 2*x+3 T = int(input()) for t in range(T): h,c,t = map(int, input().rstrip().split()) print(f(h,c,t))
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 import math from decimal import * from fractions import Fraction getcontext().prec=50 a = int(stdin.readline()) for b in range(0, a): A = stdin.readline().split() h = int(A[0]) c = int(A[1]) t = int(A[2]) if (c + h) == 2 * t: print(2) continue if (h + c) / 2 > t: print(2) continue AA = 1 / 2 * (1 + ((h - c) / 2) / ((t - (h + c) / 2))) S = int(math.floor(AA)) T = int(math.ceil(AA)) S = max(1, S) T = max(1, T) HH = abs(Fraction((S * (c + h) - c) ,(2 * S - 1)) - t) II = abs(Fraction((T * (c + h) - c) , (2 * T - 1)) - t) JJ = abs((c + h) / 2 - t) KK = abs(h - t) final = min(HH, II, JJ, KK) if final == HH: print(2 * S - 1) elif final == II: print(2 * T - 1) elif final == JJ: print(2) else: print(1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
t=int(input()) for _ in range(t): h,c,t=map(int,input().split()) avg=(h+c)/2 if t==h: print(1) continue if avg>=t: print(2) continue else: n1=int((t-c)/(2*t-h-c)) n2=2*t*(2*n1-1)*(2*n1+1) n3=((n1-1)*(c+h)+h)*(2*n1+1)+(n1*(c+h)+h)*(2*n1-1) if(n2>=n3): print(2*n1-1) else: print(2*n1+1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; int32_t main() { long long t; cin >> t; while (t--) { long long c, h; cin >> h >> c; long long y; cin >> y; if (y >= h) { cout << 1 << endl; } else if (2 * y <= (c + h)) { cout << 2 << endl; } else { long long a = (2 * y) - (c + h); long long b = h - y; long long k = b / a; double d = k * (h + c) + h; double e = d + (h + c); d /= (2 * k + 1); e /= (2 * (k + 1) + 1); if (fabs(d - y) > fabs(e - y)) k++; cout << 2 * k + 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
#include <bits/stdc++.h> using namespace std; using ll = long long; const int inf = 1e9; const double eps = 2e-6; int h, c, t; double f(ll i) { return abs(t - (h * (i + 1) + c * i) / (2.0 * i + 1)); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { cin >> h >> c >> t; if (t <= (h + c) / 2.0) { cout << "2\n"; continue; } int a = 0, b = 1e9; while (b - a >= 5) { int mid = (a + b) / 2; if (f(mid) > f(mid + 1)) a = mid; else b = mid + 1; } for (int i = a; i <= b; ++i) if (f(a) > f(i)) a = i; cout << 2 * a + 1 << '\n'; } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedOutputStream; import java.io.UncheckedIOException; import java.nio.charset.Charset; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author mikit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; LightScanner in = new LightScanner(inputStream); LightWriter out = new LightWriter(outputStream); CMixingWater solver = new CMixingWater(); solver.solve(1, in, out); out.close(); } static class CMixingWater { public void solve(int testNumber, LightScanner in, LightWriter out) { int testCases = in.ints(); for (int testCase = 1; testCase <= testCases; testCase++) { long h = in.longs(), c = in.longs(), t = in.longs(); if (t * 2 <= h + c) { out.ans(2).ln(); continue; } else if (t >= h) { out.ans(1).ln(); continue; } long min = 0, max = 1_000_000_000L; while (max - min > 1) { long mid = (min + max) / 2; if (t * (mid * 2 + 1) >= ((mid + 1) * h + mid * c)) max = mid; else min = mid; } if (((min + 1) * h + min * c) * (max * 2 + 1) + ((max + 1) * h + max * c) * (min * 2 + 1) <= 2 * t * (min * 2 + 1) * (max * 2 + 1)) { out.ans(min * 2 + 1).ln(); } else { out.ans(max * 2 + 1).ln(); } } } } static class LightWriter implements AutoCloseable { private final Writer out; private boolean autoflush = false; private boolean breaked = true; public LightWriter(Writer out) { this.out = out; } public LightWriter(OutputStream out) { this(new OutputStreamWriter(new BufferedOutputStream(out), Charset.defaultCharset())); } public LightWriter print(char c) { try { out.write(c); breaked = false; } catch (IOException ex) { throw new UncheckedIOException(ex); } return this; } public LightWriter print(String s) { try { out.write(s, 0, s.length()); breaked = false; } catch (IOException ex) { throw new UncheckedIOException(ex); } return this; } public LightWriter ans(String s) { if (!breaked) { print(' '); } return print(s); } public LightWriter ans(long l) { return ans(Long.toString(l)); } public LightWriter ans(int i) { return ans(Integer.toString(i)); } public LightWriter ln() { print(System.lineSeparator()); breaked = true; if (autoflush) { try { out.flush(); } catch (IOException ex) { throw new UncheckedIOException(ex); } } return this; } public void close() { try { out.close(); } catch (IOException ex) { throw new UncheckedIOException(ex); } } } static class LightScanner implements AutoCloseable { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public LightScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public String string() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new UncheckedIOException(e); } } return tokenizer.nextToken(); } public int ints() { return Integer.parseInt(string()); } public long longs() { return Long.parseLong(string()); } public void close() { try { this.reader.close(); } catch (IOException ex) { throw new UncheckedIOException(ex); } } } }
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
# t1: h = (h+c)/2 + (h-c)/2 # t2: (h+c)/2 # t3: (2h+c)/3 = (h+c)/2 + (h-c)/6 # t4: (h+c)/2 # t5: (3h+2c)/5 = (h+c)/2 + (h-c)/10 # t6: (h+c)/2 # output 1: (h+c)/2 + (h-c)(1/(1x3)) <= t <= h # output 3: (h+c)/2 + (h-c)(2/(3x5)) <= t < ... # output 5: (h+c)/2 + (h-c)(3/(5x7)) <= t < ... # output 7: (h+c)/2 + (h-c)(4/(7x9)) <= t < ... # 1x3/1 = (2^2-1)/1 = 4x1 - 1/1 # 3x5/2 = (4^2-1)/2 = 4x2 - 1/2 # 5x7/3 = (6^2-1)/3 = 4x3 - 1/3 # 7x9/4 = (8^2-1)/4 = 4x4 - 1/4 import math T = int(input()) for _ in range(T): h,c,t = map(int,input().split()) if 2*t <= h+c: print(2) else: estimate = (h-c)//(2*t-(h+c))//2 if (h+c)*(4*(estimate+1)**2-1) + 2*(h-c)*(estimate+1) <= 2*t*(4*(estimate+1)**2-1): print(2*estimate+1) else: print(2*estimate+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
n =int(input()) while n>0: n-=1 h,c,t = map(int,input().split()) # temp = h # k=1 # for i in range(10): # if k%2: # temp+=c # else: # temp+=h # k+=1 # print(k,temp/k) if(t>=h): print(1) elif t<=(h+c)/2: print(2) else: k = int(((t-h)/(h-2*t+c))) t1 = abs((h+c)*k+h-t*(2*k+1))*(2*k+3) t2 = abs((h+c)*(k+1)+h-t*(2*k+3))*(2*k+1) if t1<=t2: print(k*2+1) else: print(k*2+3)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; float h, c, t; float helper(int a) { return (a * h + (a - 1) * c) / (2 * a - 1); } void solve() { cin >> h >> c >> t; if (t == h) { cout << "1\n"; return; } if (t <= (h + c) / 2) { cout << 2 << "\n"; return; } long long int low = 1LL, high = 1e9; while (low <= high) { long long int mid = (low + high) / 2; float temp = helper(mid); if (temp > t) { low = mid + 1; } else { high = mid - 1; } } long long int answer; float best = (float)(INT_MAX); for (long long int i = max(0LL, low - 5); i < low + 5; i++) { if (abs(helper(i) - t) < best) { best = abs(helper(i) - t); answer = i; } } cout << 2 * answer - 1 << "\n"; return; } int main() { int tc; cin >> tc; while (tc--) { 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 double asize = 1005; long double c, h, t, m, two = 2.0, three = 3.0, x; long double hotonbase(long double v) { long double cups = 1 + (2 * v), total = (h + c) * v + h; return total / cups; } long double bs() { long double ans = 0; for (int i = 61; i >= 0; i--) { long double chal = (1ll << i); if (hotonbase(ans + chal) >= t) ans += chal; } return ans; } int main() { long double n; cin >> n; while (n--) { cin >> h >> c >> t; long double s = 0, ee = 1; m = (h + c) / two; if (t >= h) { cout << "1\n"; } else if (t <= m) { cout << "2\n"; } else { long double a = bs(); long double b = a + 1; long double c = a - 1; long double da = abs(hotonbase(a) - t), db = abs(hotonbase(b) - t), dc = abs(hotonbase(c) - t), mmm = min(da, min(db, dc)); if (c >= 0 && mmm == dc) cout << 2 * c + 1 << "\n"; else if (mmm == da) cout << 2 * a + 1 << "\n"; else cout << 2 * b + 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 math test = int(input()) for _ in range(test): h,c,t = map(int, input().split()) if t==h: print(1) elif t <= (h+c)//2: print(2) else: l = 1 r = 2**31 -1 flag = True while(l<r): mid = (l+r)//2 if ((h+c)*mid+h)==t*(2*mid+1): print(2*mid+1) flag = False break elif ((h+c)*mid+h)>t*(2*mid+1): l = mid+1 else: r = mid-1 if flag: l = max(0,l-3) r = r+3 diff = l for i in range(l,r): if abs(((h+c)*i+h)-t*(2*i+1))*(2*diff+1) < abs(((h+c)*diff+h)-t*(2*diff+1))*(2*i+1): diff = i print(2*diff+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, stdout input = lambda: stdin.readline().strip() fast_print = stdout.write from math import sqrt, floor, ceil from bisect import bisect_right def dist(n, h, c, t): return abs((n*h+(n-1)*c)-t*(2*(n)-1)) T = int(input()) for _ in range(T): h, c, t = map(int, input().split()) if h==t: fast_print('1\n') elif (h+c-2*t)>=0: fast_print('2\n') else: n0 = max((c-t)//(h+c-2*t), 1) n1 = max(n0+1, 2) first = dist(n0, h, c, t)*(2*(n1)-1) second = dist(n1, h, c, t)*(2*(n0)-1) m = min(first, second) ans = 10**50 if m==first: ans = min(ans, n0) if m==second: ans = min(ans, n1) ans = 2*ans-1 fast_print(str(ans)+'\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; void solve() { long long h, c, t; cin >> h >> c >> t; if ((h + c) / 2 >= t) { cout << 2 << '\n'; return; } long long k = 2 * ((t - h) / (h + c - 2 * t)) + 1; long long val2 = abs((k + 2) / 2 * c + (k + 3) / 2 * h - t * (k + 2)) * k; long long val1 = abs(k / 2 * c + (k + 1) / 2 * h - t * k) * (k + 2); if (val1 <= val2) { cout << k << '\n'; } else cout << k + 2 << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) solve(); }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys from functools import cmp_to_key input = sys.stdin.readline def main(): tc = int(input()) def cmp(a, b): dif = a[0] * b[1] - a[1] * b[0] return dif if dif != 0 else a[1] - b[1] cmp = cmp_to_key(cmp) ans = [] for _ in range(tc): h, c, t = map(int, input().split()) if h + c == 2 * t: ans.append(2) else: x = max(0, (t - h) // (h + c - 2 * t)) cands = [ ((h + c) - 2 * t, 2), (x * (h + c) + h - (2 * x + 1) * t, 2 * x + 1), ((x + 1) * (h + c) + h - (2 * x + 3) * t, 2 * x + 3) ] cands = map(lambda t: (abs(t[0]), t[1]), cands) ans.append(min(cands, key = cmp)[1]) print('\n'.join(map(str, ans))) main()
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys input = sys.stdin.readline def judge(x): return (2*x+1)*h+x*(c-h)>=t*(2*x+1) def binary_search(): l, r = 0, 10**18 while l<=r: m = (l+r)//2 if judge(m): l = m+1 else: r = m-1 return r T = int(input()) for _ in range(T): h, c, t = map(int, input().split()) if 2*t<=h+c: print(2) continue b = binary_search() if (2*b+1)*(2*b+3)*(h-t)+(2*b+3)*b*(c-h)<=(2*b+1)*(2*b+3)*(t-h)-(2*b+1)*(b+1)*(c-h): print(2*b+1) else: print(2*b+3)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math t=int(input()) for _ in range(t): h,c,t=map(int,input().split()) if (h==t): print(1) continue ans1=(h+c)/t if ((h+c)/2>=t): print(2) continue ans2=0 if (ans1==int(ans1)): print(int(ans1)) else: ans2=(t-h)//(h+c-2*t) op1=ans2 op2=ans2+1 t1=((h+c)*op1+h) t2=((h+c)*op2+h) d1=abs(t*(2*op1+1)-t1)/(2*op1+1) d2=abs(t*(2*op2+1)-t2)/(2*op2+1) if (d1<=d2): print(2*op1+1) else: print(2*op2+1) #print("ans2 = ",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
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; long long h, c, t; long double calc(long long x) { long double th = x * h + x * c; th /= (2 * x); return abs(th - t); } long long ternary_search(long long lo, long long hi) { while (lo < hi - 2) { long long m1 = (lo * 2 + hi) / 3; long long m2 = (lo + hi * 2) / 3; if (calc(m1) <= calc(m2)) hi = m2; else lo = m1; } long double val = 1e9; long long idx = lo; for (long long i = lo; i <= hi; i++) { if (val > calc(i)) { idx = i; val = calc(i); } } return idx; } long double calc2(long long x) { long double th = x * h + (x - 1) * c; th /= (2 * x - 1); return abs(th - t); } long long ternary_search2(long long lo, long long hi) { while (lo < hi - 2) { long long m1 = (lo * 2 + hi) / 3; long long m2 = (lo + hi * 2) / 3; if (calc2(m1) <= calc2(m2)) hi = m2; else lo = m1; } long double val = 1e9; long long idx = lo; for (long long i = lo; i <= hi; i++) { if (val > calc2(i)) { idx = i; val = calc2(i); } } return idx; } 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; long long ans = ternary_search(1, 1e12); long long ans2 = ternary_search2(1, 1e12); if (calc(ans) < calc2(ans2)) cout << ans * 2 << "\n"; else cout << ans2 * 2 - 1 << "\n"; } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys import string input = sys.stdin.readline import math #import numpy #letters = list(string.ascii_lowercase) from decimal import Decimal l = int(input()) for i in range(l): n = list(map(int, input().split())) a,b = n[0] - n[1], n[2] - n[1] #print(a,b) try: s = int(2/(2 - a/b)) - 1 #s = abs(s) except: s = 0 #print(s) best = 99999999 for j in range(max(s//2,0), s//2 + 2): p = Decimal((a * (j+1))) r = Decimal((2*j + 1)) m_num = p/r #print(m_num) m_num = abs(m_num - b) #print(j, 'j', m_num) if m_num < best: best = m_num ans = 2*j + 1 if a<=b: print(1) elif b<=a/2: print(2) else: if best < abs(a-b): print(ans) else: print(1) #print((24999 * 10**5) / 49999) #print((25000 * 10**5) / 50000)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h,c,t=map(int,input().split()) if t<=(h+c)/2:print(2) else: k=(h-t)//(2*t-h-c) if abs((2*k+3)*t-k*h-2*h-k*c-c)*(2*k+1)<abs((2*k+1)*t-k*h-h-k*c)*(2*k+3):print(2*k+3) else:print(2*k+1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import os import sys from io import BytesIO, IOBase from collections import defaultdict as dd def main(): for _ in range(int(input())): h,c,t=map(int,input().split()) if t<=(h+c)/2: print(2) else: n=(c-t)//(h+c-2*t) if abs(n*(h+c)-c-t*(2*n-1))*(2*n+1)>abs((n+1)*(h+c)-c-t*(2*n+1))*(2*n-1): print(2*n+1) else: print(2*n-1) 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") 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
/** * @author derrick20 */ import java.io.*; import java.util.*; public class MixingWater { public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); while (T-->0) { long h = sc.nextLong(); long c = sc.nextLong(); long t = sc.nextLong(); if (2 * t <= (c + h)) { out.println(2); } else { int lo = 0; int hi = (int) 1e9; while (lo < hi) { // find last position that's positive (try that and also +1) // 1100 int mid = (lo + hi + 1) / 2; if (calcNum(h, c, mid) >= calcDenom(mid) * t) { lo = mid; } else { hi = mid - 1; } } int a = lo; // try here and next int curr = a; int next = a + 1; long denom1 = calcDenom(curr); long num1 = calcNum(h, c, curr) - t * denom1; long denom2 = calcDenom(next); long num2 = t * denom2 - calcNum(h, c, next); out.println(num1 * denom2 <= num2 * denom1 ? 2 * curr + 1 : 2 * next + 1); } } out.close(); } static long calcNum(long h, long c, long a) { return ((a + 1) * h + a * c); } static long calcDenom(long a) { return 2 * a + 1; } static void generate() { int h = (int) (200 * Math.random()); int c = (int) (h * Math.random()); int t = (int) (((h + c) / 2) + (h - (h + c) / 2) * Math.random()); System.out.println(h + " " + c + " " + t); } static class FastScanner { private int BS = 1<<16; private char NC = (char)0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt=1; boolean neg = false; if(c==NC)c=getChar(); for(;(c<'0' || c>'9'); c = getChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=getChar()) { res = (res<<3)+(res<<1)+c-'0'; cnt*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=getChar(); while(c>32) { res.append(c); c=getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=getChar(); while(c!='\n') { res.append(c); c=getChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=getChar(); if(c==NC)return false; else if(c>32)return true; } } } static void ASSERT(boolean assertion, String message) { if (!assertion) throw new AssertionError(message); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): p=0 a,b,c=map(int,input().split()) d=[] h=0 t=0 e=[] if a<=c and b<=c: print(1) elif 2*c-a-b<=0: print(2) else: k=(a-c)//(2*c-a-b) if abs(k*(a+b)+a-c*(2*k+1))*(2*k+3)<=abs((k+1)*(a+b)+a-c*(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
import java.io.*; import java.util.*; public class C implements Runnable { boolean oj = false; Reader scn; PrintWriter out; String INPUT = ""; void solve() { int t = scn.nextInt(); while (t-- > 0) { int hot = scn.nextInt(), cold = scn.nextInt(), temp = scn.nextInt(); if (temp == hot) { out.println(1); continue; } if (2 * temp <= cold + hot) { out.println(2); continue; } long n = (temp - cold) / (2 * temp - cold - hot); double d1 = Math.abs(temp * 1L * (2 * n - 1) - (n * (cold + hot) - cold)) / (2 * n - 1.0); n++; double d2 = Math.abs(temp * 1L * (2 * n - 1) - (n * (cold + hot) - cold)) / (2 * n - 1.0); if (d1 <= d2) { n--; } out.println(2 * n - 1); } } public void run() { long time = System.currentTimeMillis(); boolean judge = System.getProperty("ONLINE_JUDGE") != null || oj; out = new PrintWriter(System.out); scn = new Reader(judge); solve(); out.flush(); if (!judge) { System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } } public static void main(String[] args) { new Thread(null, new C(), "Main", 1 << 28).start(); } class Reader { InputStream is; public Reader(boolean oj) { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } String next() { int b = skip(); StringBuilder stringB = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') stringB.appendCodePoint(b); b = readByte(); } return stringB.toString(); } String nextLine() { int b = skip(); StringBuilder stringB = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') stringB.appendCodePoint(b); b = readByte(); } return stringB.toString(); } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } char[] next(int n) { char[] buff = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buff[p++] = (char) b; b = readByte(); } return n == p ? buff : Arrays.copyOf(buff, p); } int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long[] nextLongArr(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[] nextIntArr(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } char[][] nextMat(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } long[][] next2Long(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArr(m); } return arr; } int[][] next2Int(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArr(m); } return arr; } long[] shuffle(long[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } return arr; } long[] uniq(long[] arr) { arr = scn.shuffle(arr); Arrays.parallelSort(arr); long[] rv = new long[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } int[] uniq(int[] arr) { arr = scn.shuffle(arr); Arrays.parallelSort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } long[] reverse(long[] arr) { int i = 0, j = arr.length - 1; while (i < j) { arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; i++; j--; } return arr; } int[] reverse(int[] arr) { int i = 0, j = arr.length - 1; while (i < j) { arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; i++; j--; } return arr; } long[] compres(long[] arr) { int n = arr.length; long[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } int[] compres(int[] arr) { int n = arr.length; int[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } void deepFillLong(Object array, long val) { if (!array.getClass().isArray()) { throw new IllegalArgumentException(); } if (array instanceof long[]) { long[] intArray = (long[]) array; Arrays.fill(intArray, val); } else { Object[] objArray = (Object[]) array; for (Object obj : objArray) { deepFillLong(obj, val); } } } void deepFillInt(Object array, int val) { if (!array.getClass().isArray()) { throw new IllegalArgumentException(); } if (array instanceof int[]) { int[] intArray = (int[]) array; Arrays.fill(intArray, val); } else { Object[] objArray = (Object[]) array; for (Object obj : objArray) { deepFillInt(obj, val); } } } void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } } }
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 * getcontext().prec = 18 for t in range(int(input())): n,m,k=map(int,input().split()) l=1 r=10**9 while r-l>3: mid=(r+l)//2 if mid%2==0: mid-=1 '''if mid%2==0: c=mid//2 chk=((c*n)+(c-1)*m)/(mid-1) if chk<k: r=mid-1 else: l=mid-1 else:''' c=(mid//2)+1 chk=((c*n)+(c-1)*m)/(mid) if chk<k: r=mid else: l=mid chk=Decimal((l//2+1)*n+(l//2)*m)/Decimal(l) chk1=Decimal((r//2+1)*n+(r//2)*m)/Decimal(r) #print(l,r) #print(chk,chk1) if(abs(chk-k)>abs(chk1-k)): ans=r chk=chk1 else: ans=l chk1=(n+m)/(2) if(abs(chk-k)>abs(chk1-k)): print(2) else: print(ans)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys input = sys.stdin.readline flush = sys.stdout.flush for _ in range(int(input())): h, c, t = map(int, input().split()) m = h + c >> 1 if t <= m: print(2) continue a = (h - t) // (2*t - h - c) b = a + 1 print(2*a + 1 if 2*t*(2*a+1)*(2*b+1) >= (2*b+1)*((a+1)*h+a*c)+(2*a+1)*((b+1)*h+b*c) else 2 * b + 1)
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.Random; import java.util.StringTokenizer; public class C { //Solution by Sathvik Kuthuru public static void main(String[] args) { FastReader scan = new FastReader(); PrintWriter out = new PrintWriter(System.out); Task solver = new Task(); int t = scan.nextInt(); for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out); out.close(); } static class Task { public void solve(int testNumber, FastReader scan, PrintWriter out) { long h = scan.nextInt(), c = scan.nextInt(), t = scan.nextInt(); long low = 1, high = (int) (1e8); long num = -1, den = -1; while(low <= high) { long mid = (low + high) / 2; long val = mid * 2; if(val - 2 > 0 && get(h, c, val, t) * (val - 2) >= get(h, c, val - 2, t) * val) { num = get(h, c, val - 2, t); den = val - 2; high = mid - 1; } else low = mid + 1; } low = 0; high = (int) (1e8); while(low <= high) { long mid = (low + high) / 2; long val = mid * 2 + 1; if(val - 2 > 0 && get(h, c, val, t) * (val - 2) >= get(h, c, val - 2, t) * val) { long currNum = get(h, c, val - 2, t); if(den == -1 || (currNum * den < num * (val - 2)) || (val - 2 < den && currNum * den == num * (val - 2))) { num = currNum; den = val - 2; } high = mid - 1; } else low = mid + 1; } out.println(den); } long get(long h, long c, long val, long t) { if(val % 2 == 0) { long aa = (h + c) * val / 2; return Math.abs(t * val - aa); } else { long aa = h * (val / 2 + 1) + c * (val / 2); return Math.abs(t * val - aa); } } } static void shuffle(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 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
for tt in range(int(input())): h, c, t = map(int, input().split()) if t <= (h + c) // 2: print(2) else: num = int((t - h) / (h + c - 2 * t)) num = 2 * num + 1 if num % 2: op1 = num op2 = num + 2 c1 = abs(((op1 // 2 + 1) * h + (op1 // 2) * c) - t * op1) c2 = abs(((op2 // 2 + 1) * h + (op2 // 2) * c) - t * op2) if c1 * op2 <= c2 * op1: print(op1) else: print(op2)
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.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.function.Function; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author out_of_the_box */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(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, OutputWriter out) { long h = in.nextLong(); h <<= 1L; long c = in.nextLong(); c <<= 1L; long t = in.nextLong(); t <<= 1L; long a = (h + c) / 2L; if (t <= a) { out.println(2); } else { h >>= 1L; c >>= 1L; t >>= 1L; long finalH = h; long finalC = c; long finalT = t; Fraction ft = new Fraction(finalT, 1L); Function<Long, Boolean> func = some -> ((getTemp(some, finalH, finalC).compareTo(ft)) < 0); long mx = getMax(1, func); long intx = BinarySearch.searchLastZero(0L, mx, func); long othx = BinarySearch.searchFirstOne(0L, mx, func); Fraction val = getTemp(intx, h, c); Fraction vall = getTemp(othx, h, c); if (val.sub(ft).abs().compareTo(vall.sub(ft).abs()) <= 0) { // if (Double.compare(Math.abs(val.sub(ft)), Math.abs(vall.sub(ft))) <= 0) { out.println(2L * intx + 1L); } else { out.println(2L * othx + 1L); } // long num = h-t; // long denom = 2L*t-h-c; // if (num%denom == 0L) { // long x = num/denom; // double temp = getTemp(x, h, c); // MiscUtility.assertion(Double.compare(temp, t) == 0, // String.format("Temp [%f] and t[%d] are not eq.", temp, t)); // out.println(2L*x+1L); // } else { // long x = num/denom; // double val = getTemp(x,h,c); // double vall = getTemp(x+1L,h,c); // double dif = Math.abs(val-t); // double diff = Math.abs(vall-t); // if (dif <= diff) { // out.println(2L*x+1L); // } else { // out.println(2L*x+3L); // } // } } } private Fraction getTemp(long x, long h, long c) { return new Fraction(x * h + h + x * c, 2 * x + 1); } private long getMax(long start, Function<Long, Boolean> func) { long mx = (Long.MAX_VALUE >> 1L); while (!func.apply(start) && start <= mx) { start <<= 1L; } return start; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(long i) { writer.println(i); } public void println(int i) { writer.println(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class Fraction implements Comparable<Fraction> { public long num; public long denom; public Fraction(long num, long denom) { if (denom == 0) { throw new RuntimeException("Denominator cannot be 0"); } this.num = num; this.denom = denom; } public Fraction add(Fraction other) { long lcm = MathUtility.lcm(denom, other.denom); long mul = lcm / denom; long omul = lcm / other.denom; long nu = num * mul + other.num * omul; return new Fraction(nu, lcm); } public Fraction sub(Fraction other) { return add(new Fraction(-other.num, other.denom)); } public Fraction abs() { return new Fraction(Math.abs(num), Math.abs(denom)); } public int compareTo(Fraction o) { return Long.compare(this.num * o.denom, this.denom * o.num); } } static class MathUtility { public static long hcf(long a, long b) { if (a == 0L) { return b; } return hcf(b % a, a); } public static long lcm(long a, long b) { if (a > b) return lcm(b, a); long hcf = hcf(a, b); return (b / hcf) * a; } } static class BinarySearch { public static long searchLastZero(long start, long end, Function<Long, Boolean> valueFunc) { long low = start; long high = end - 1L; while (low < high) { long mid = low + (high - low) / 2L; if (valueFunc.apply(mid + 1L)) { high = mid; } else { low = mid + 1L; } } if (!valueFunc.apply(low)) { return low; } else { return start - 1L; } } public static long searchFirstOne(long start, long end, Function<Long, Boolean> valueFunc) { long low = start; long high = end - 1L; while (low < high) { long mid = low + (high - low) / 2L; if (valueFunc.apply(mid)) { high = mid; } else { low = mid + 1L; } } if (valueFunc.apply(low)) { return low; } else { return end; } } } }
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 Decimal t = int(input()) for _ in range(t): h,c,t = map(int, input().split()) if t <= (h+c)/2: print(2) continue if h<t: print(1) continue amt=1 k=500000//2 while k>0: while ((amt+k)*h + ((amt+k)-1)*c) > t * ((amt+k)+(amt+k)-1): amt+=k k//=2 l = abs(Decimal(amt*h + (amt-1)*c) / Decimal(2*amt-1) - t) o = abs(Decimal((amt+1)*h + ((amt+1)-1)*c) / Decimal(2*(amt+1)-1) - t) # l = abs((amt*h + (amt-1)*c) - t*(2*amt-1)) # o = abs(((amt+1)*h + ((amt+1)-1)*c) - t*(2*(amt+1)-1)) # print(f"\t{l}") # print(f"\t{o}") if l <= o: print(2*amt-1) else: print(2*(amt+1)-1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from __future__ import division from sys import stdin out = [] for _ in range(int(input())): h, c, t = map(int, stdin.readline().split()) d1, d2 = h - t, t - c if d1 >= d2: out.append(2) else: lo = (c - t) // (h + c - 2 * t) hi = lo + 1 tem1 = abs((lo * h + (lo - 1) * c) - (t * (2 * lo - 1))) * (2 * hi - 1) tem2 = abs((hi * h + (hi - 1) * c) - (t * (2 * hi - 1))) * (2 * lo - 1) if tem1 <= tem2: out.append(lo * 2 - 1) else: out.append(hi * 2 - 1) print('\n'.join(map(str, out)))
PYTHON
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; 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 gtix; while (len >= 1) { ll mid = low + len / 2; if (f(mid) >= t) { gtix = mid; low = mid; } len /= 2; } ll besti = 2; double besty = fabs(t - (h + c) / 2.0); for (ll i = max<ll>(1, gtix - 100); i <= gtix + 100; ++i) { double cy = fabs(t - f(i)); if (cy < besty) { besti = i; besty = cy; } } cout << besti << endl; } int main() { int t; cin >> t; while (t--) solve(); return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from fractions import Fraction as F from math import isclose for _ in range(int(input())): h, c, t = map(int, input().split()) if 2 * t <= h + c: print(2) continue def f(hot): return (hot * h + (hot-1) * c) def g(hot): return abs(t - F(hot * h + (hot-1) * c, hot * 2 - 1)) lo = 1 hi = 10 ** 9 while lo < hi: mid = (hi + lo) // 2 temp = f(mid) cmp = t * (mid * 2 - 1) if temp == cmp: print(mid * 2 - 1) break elif temp < cmp: hi = mid else: lo = mid + 1 else: bi = 1 for lo in range(max(1, lo-10), lo + 10): if g(lo) < g(bi): bi = lo print(bi * 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
from decimal import Decimal from math import floor from sys import stdin, stdout int_in = lambda: int(stdin.readline()) arr_in = lambda: [int(x) for x in stdin.readline().split()] mat_in = lambda rows: [arr_in() for y in range(rows)] str_in = lambda: stdin.readline().strip() out = lambda o: stdout.write("{}\n".format(o)) arr_out = lambda o: out(" ".join(map(str, o))) bool_out = lambda o: out("YES" if o else "NO") tests = lambda: range(1, int_in() + 1) case_out = lambda i, o: out("Case #{}: {}".format(i, o)) def check(x, h, c): return Decimal((x*h + (x-1)*c)) / Decimal((2*x - 1)) def solve(h, c, t): if h == c: return 1 if 2*t <= (h+c): return 2 point = (c - t) / (h + c - 2*t) lower = floor(point) start_at = max(1, lower) end_at = start_at + 2 best_i = -1 best_diff = float("inf") for i in range(start_at, end_at): curr = abs(t - check(i, h, c)) if curr < best_diff: best_diff = curr best_i = i return 2*best_i - 1 if __name__ == "__main__": for i in tests(): h, c, t = arr_in() out(solve(h, c, t))
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class C1359 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); StringTokenizer st; for (int n = Integer.parseInt(br.readLine()); n-- > 0; ) { st = new StringTokenizer(br.readLine()); long h = Integer.parseInt(st.nextToken()); long c = Integer.parseInt(st.nextToken()); long t = Integer.parseInt(st.nextToken()); long oneCupDiff = Math.abs(t - h); long twoCupsDiff = Math.abs(2 * t - h - c); long cups = (long) Math.ceil((1.0 * (t - c)) / (2 * t - h - c)); long cupsDiff = Math.abs((2 * cups - 1) * t - cups * h - (cups - 1) * c); if (cups - 1 < 0) { cupsDiff = 100_000_000; } long cupsOneLessDiff = Math.abs((2 * cups - 3) * t - (cups - 1) * h - (cups - 2) * c); if (cups - 2 < 0) { cupsOneLessDiff = 100_000_000; } if (2 * oneCupDiff <= twoCupsDiff && (2 * cups - 3) * oneCupDiff <= cupsOneLessDiff && (2 * cups - 1) * oneCupDiff <= cupsDiff) { out.println(1); } else if (twoCupsDiff <= 2 * oneCupDiff && (2 * cups - 3) * twoCupsDiff <= 2 * cupsOneLessDiff && (2 * cups - 1) * twoCupsDiff <= 2 * cupsDiff) { out.println(2); } else if (cupsOneLessDiff <= (2 * cups - 3) * oneCupDiff && 2 * cupsOneLessDiff <= (2 * cups - 3) * twoCupsDiff && (2 * cups - 1) * cupsOneLessDiff <= (2 * cups - 3) * cupsDiff) { out.println(2 * cups - 3); } else { out.println(2 * cups - 1); } } 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; int main() { int t; cin >> t; while (t--) { int h, c, t; cin >> h >> c >> t; if (t <= c) { cout << 2 << "\n"; continue; } if (t >= h) { cout << 1 << "\n" << "\n"; continue; } double val1, val2, val3, x; long long y; val1 = (double)(h + c) / 2; if (val1 == t) { cout << 2 << "\n"; continue; } x = (double)(t - h) / (h + c - 2 * t); y = x; if (x <= 0) y = 1; val2 = (double)(y * (h + c) + h) / (2 * y + 1); y++; val3 = (double)(y * (h + c) + h) / (2 * y + 1); int flag = 0; double min = h; if (abs(t - val1) < min) { min = abs(t - val1); flag = 1; } if (abs(t - val2) < min) { min = abs(t - val2); flag = 2; } if (abs(t - val3) < min) { min = abs(t - val3); flag = 3; } if (abs(t - h) <= min) { min = abs(t - h); flag = 4; } if (flag == 1) cout << 2 << "\n"; else if (flag == 2) cout << 2 * (y - 1) + 1 << "\n"; else if (flag == 3) cout << 2 * y + 1 << "\n"; else if (flag == 4) cout << 1 << "\n"; } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math from decimal import Decimal as d def calcT(th, tc, n): return (d((tc * n)) + ((n+1) * th)) / ((2*n) + 1) # Added Decimal function def main(th, tc, t): if (th + tc)/2 >= t: return 2 else: n = (t - th)/(tc + th - (2 * t)) # Comparing the ceil and floor values of n if abs(t - calcT(th, tc, math.ceil(n))) < abs(t - calcT(th, tc, math.floor(n))): return (math.ceil(n) * 2) + 1 return (math.floor(n) * 2) + 1 for _ in range(int(input())): th, tc, t = [int(i) for i in input().split()] print(main(th, tc, t))
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math tests = int(input()) for _ in range(tests): h,c,t = map(int,input().split()) if 2*t <= h + c: print(2) else: m = (h-t)/(2*t-h-c) if(m==0): print(1) else: m1 = math.ceil(m) m2 = math.floor(m) if(abs(((m2+1)*h+m2*c)-(2*m2+1)*t)<abs(((m1+1)*h+m1*c)-(2*m1+1)*t)): if(abs(((m2+1)*h+m2*c)/(2*m2+1)-t)<=abs(((m1+1)*h+m1*c)/(2*m1+1)-t)): print(2*m2+1) else: print(2*m1+1) else: print(2*m1+1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from collections import deque 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 - 11, base + 10): 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 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 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 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) #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 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
/** * @author egaeus * @mail [email protected] * @veredict Not sended * @url <https://codeforces.com/problemset/problem/> * @category ? * @date 28/05/2020 **/ import java.io.*; import java.math.BigDecimal; import java.math.MathContext; import java.util.*; import static java.lang.Integer.*; import static java.lang.Math.*; public class CFC { static int H, C,K; public static void main(String args[]) throws Throwable { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int T = parseInt(in.readLine()); for (int t = 0; t < T; t++) { StringTokenizer st = new StringTokenizer(in.readLine()); H = parseInt(st.nextToken()); C = parseInt(st.nextToken()); K = parseInt(st.nextToken()); BigDecimal k = BigDecimal.valueOf(K); double search = 1. * (H - K) / (2 * K - H - C); long a = Math.max(1, (long) (2 * Math.ceil(search) - 1)), b = Math.max(1, (long) Math.ceil(2 * Math.ceil(search) + 1)); BigDecimal[][] arr = new BigDecimal[][]{{BigDecimal.valueOf(a), f1(a)}, {BigDecimal.valueOf(b), f1(b)}, {BigDecimal.valueOf(2), f1(2)}}; Arrays.sort(arr, (A, B) -> A[1].subtract(k).abs().compareTo(B[1].subtract(k).abs()) != 0?A[1].subtract(k).abs().compareTo(B[1].subtract(k).abs()):A[0].compareTo(B[0])); System.out.println(arr[0][0].longValue()); } } static BigDecimal f1(long x) { return BigDecimal.valueOf(1. * ((x / 2) * C + ((x / 2) + (x % 2)) * H)).divide(BigDecimal.valueOf(x), MathContext.DECIMAL128); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h, c, t = map(int, input().split()) if h==t: print(1) elif (t-c<=h-t): print(2) else: temp = (t-h)/(h+c-2*t) if (int(temp)==temp): print(int(2*temp+1)) else: a = int(temp) b = a+1 if 2*t*(2*a+1)*(2*b+1) >= (2*b+1)*((a+1)*h+a*c)+(2*a+1)*((b+1)*h+b*c): print(2*a+1) else: print(2*b+1) # temp = round(abs((t-h)/(h+c-2*t))) # print(2*temp+1) # else: # t1 = round(temp) # print(2*t1+1) # t1 = int(temp) # m1 = ((t1+1)*c + (t1+2)*h)/(2*t1+3) - t # m2 = (t1*c + (t1+1)*h)/(2*t1+1) - t # print(m1, m2) # if abs(m1)<abs(m2): # print(2*t1 + 3) # else: # print(2*t1 + 1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys from collections import defaultdict as dd import heapq import math try: sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') except: pass input = sys.stdin.readline for i in range(int(input())): h,c,t=map(int,input().split()) l=t-(h+c)/2 if t==h: print(1) elif 2*t<=(h+c): print(2) else: k=int((((h-c)/(2*l))+1)/2) q=((h-c)/(2*(2*k+1)))-l w=((h-c)/(2*(2*k-1)))-l if q<0: q*=-1 if w<0: w*=-1 if q<w: print(2*k+1) else: print(2*k-1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; public class test2{ public static void main (String [] args) throws IOException { try { Scanner sc=new Scanner(System.in); int test=sc.nextInt(); for(int i=0;i<test;i++) { double h=sc.nextDouble(); double c=sc.nextDouble(); double t=sc.nextDouble(); double ans=0; double temp=(double)((h+c)/2); if(t<=temp){ ans=2; } else { if(t>=h) ans=1; else{ //double j=(double)h; double diff1=0,diff2=0; double x=1; double min=1000000; //while(j>temp) //{ // j=temp+((h-c)/((4*x)-2)); // diff=Math.abs(t-j); // if(diff<min) // min=diff; // else // { // x--; // break; // } // // break; // x++; //} double val=(h-c)/(2*(t-temp)); double near=Math.floor(val); if(near%2==0) { ans=near+1; } else { //diff1=t-temp-((h-c)/(2*near)); //diff1=Math.abs(diff1); //min=diff1; //ans=near; //diff2=t-temp-((h-c)/((2*near)+2)); //diff2=Math.abs(diff2); //if(diff2<min) //{ // ans=near+2; //} //if(diff1==diff2) //ans=near; double middle=((near)*(near+2))/(near+1); if(val>middle) ans=near+2; else ans=near; } //System.out.println(near); //System.out.println(diff1); //System.out.println(diff2); //System.out.println(val); } } System.out.println((int)ans); } } catch (Exception e) { return; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): h, c, t = map(int, input().split()) if h + c >= 2 * t: print(2) else: diff2 = 2*t - (h + c) hDiff2 = 2*h - (h + c) kDown = (hDiff2//diff2 - 1)//2 kUp = kDown + 1 diffDown = abs(diff2 - hDiff2/(2 * kDown + 1)) diffUp = abs(diff2 - hDiff2/(2 * kUp + 1)) if diffDown <= diffUp: print(2 * kDown + 1) else: print(2 * kDown + 3)
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 ecr88; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; public class C { InputStream is; PrintWriter out; String INPUT = ""; void solve() { // h // h/2+c/2 // 2h/3+c/3 for(int T = ni();T > 0;T--){ go(); } } void go() { long h = ni(), c = ni(), t = ni(); if(t >= h){ out.println(1); return; } if(2*t <= h+c){ out.println(2); return; } // ((a+1)h+ac)/(2a+1) >= t // (a+1)h+ac >= t(2a+1) // a(h+c-2t) >= t-h // a(2t-h-c) <= h-t // <= (h-t)/(2t-h-c) long amax = (h-t) / (2*t-h-c); long[] am = new long[]{(amax+1)*h+amax*c, 2*amax+1}; long[] amt = sub(am, new long[]{t, 1}); assert amt[0] >= 0; long[] ai = new long[]{(amax+2)*h+(amax+1)*c, 2*(amax+1)+1}; long[] ait = sub(new long[]{t, 1}, ai); assert ait[0] >= 0; if(compare(amt, ait) <= 0){ out.println(2*amax+1); }else{ out.println(2*amax+3); } } public static long[] reduce(long[] f) { if(f[1] == 0) { // n/0 f[0] = 1; }else if(f[0] == 0) { // 0/n f[1] = 1; }else { if(f[1] < 0) { // -a/-b ->a/b f[0] = -f[0]; f[1] = -f[1]; } long a = Math.abs(f[0]), b = f[1]; while (b > 0) { long c = a; a = b; b = c % b; } f[0] /= a; f[1] /= a; } return f; } public static long[] add(long[] a, long[] b){ return reduce(new long[]{a[0]*b[1]+a[1]*b[0], a[1]*b[1]}); } public static long[] sub(long[] a, long[] b){ return reduce(new long[]{a[0]*b[1]-a[1]*b[0], a[1]*b[1]}); } public static long[] mul(long[] a, long[] b){ return reduce(new long[]{a[0]*b[0], a[1]*b[1]}); } public static long[] div(long[] a, long[] b){ return reduce(new long[]{a[0]*b[1], a[1]*b[0]}); } public static int compare(long[] a, long[] b){return Long.signum(a[0] * b[1] - a[1] * b[0]);} public static long[] min(long[] a, long[] b){ return compare(a, b) <= 0 ? a : b; } public static long[] max(long[] a, long[] b){ return compare(a, b) >= 0 ? a : b; } public static int lowerBound(long[][] es, int p, long[] r) { int low = -1, high = p; while(high-low > 1){ int h = high+low>>>1; if(Long.compare(r[0]*es[h][1], r[1]*es[h][0]) <= 0){ high = h; }else{ low = h; } } return high; } public static Comparator<long[]> comp = (a, b) -> Long.signum(a[0]*b[1]-a[1]*b[0]); void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new C().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
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 bisect import * from collections import * from math import gcd,ceil,sqrt,floor,inf from heapq import * from itertools import * from operator import add,mul,sub,xor,truediv,floordiv from functools import * #------------------------------------------------------------------------ import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #------------------------------------------------------------------------ def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) #------------------------------------------------------------------------ from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc farr=[1] ifa=[] def fact(x,mod=0): if mod: while x>=len(farr): farr.append(farr[-1]*len(farr)%mod) else: while x>=len(farr): farr.append(farr[-1]*len(farr)) return farr[x] def ifact(x,mod): global ifa ifa.append(pow(farr[-1],mod-2,mod)) for i in range(x,0,-1): ifa.append(ifa[-1]*i%mod) ifa=ifa[::-1] def per(i,j,mod=0): if i<j: return 0 if not mod: return fact(i)//fact(i-j) return farr[i]*ifa[i-j]%mod def com(i,j,mod=0): if i<j: return 0 if not mod: return per(i,j)//fact(j) return per(i,j,mod)*ifa[j]%mod def catalan(n): return com(2*n,n)//(n+1) def isprime(n): for i in range(2,int(n**0.5)+1): if n%i==0: return False return True def lowbit(n): return n&-n def inverse(a,m): a%=m if a<=1: return a return ((1-inverse(m,a)*m)//a)%m class BIT: def __init__(self,arr): self.arr=arr self.n=len(arr)-1 def update(self,x,v): while x<=self.n: self.arr[x]+=v x+=x&-x def query(self,x): ans=0 while x: ans+=self.arr[x] x&=x-1 return ans ''' class SMT: def __init__(self,arr): self.n=len(arr)-1 self.arr=[0]*(self.n<<2) self.lazy=[0]*(self.n<<2) def Build(l,r,rt): if l==r: self.arr[rt]=arr[l] return m=(l+r)>>1 Build(l,m,rt<<1) Build(m+1,r,rt<<1|1) self.pushup(rt) Build(1,self.n,1) def pushup(self,rt): self.arr[rt]=self.arr[rt<<1]+self.arr[rt<<1|1] def pushdown(self,rt,ln,rn):#lr,rn表区间数字数 if self.lazy[rt]: self.lazy[rt<<1]+=self.lazy[rt] self.lazy[rt<<1|1]+=self.lazy[rt] self.arr[rt<<1]+=self.lazy[rt]*ln self.arr[rt<<1|1]+=self.lazy[rt]*rn self.lazy[rt]=0 def update(self,L,R,c,l=1,r=None,rt=1):#L,R表示操作区间 if r==None: r=self.n if L<=l and r<=R: self.arr[rt]+=c*(r-l+1) self.lazy[rt]+=c return m=(l+r)>>1 self.pushdown(rt,m-l+1,r-m) if L<=m: self.update(L,R,c,l,m,rt<<1) if R>m: self.update(L,R,c,m+1,r,rt<<1|1) self.pushup(rt) def query(self,L,R,l=1,r=None,rt=1): if r==None: r=self.n #print(L,R,l,r,rt) if L<=l and R>=r: return self.arr[rt] m=(l+r)>>1 self.pushdown(rt,m-l+1,r-m) ans=0 if L<=m: ans+=self.query(L,R,l,m,rt<<1) if R>m: ans+=self.query(L,R,m+1,r,rt<<1|1) return ans ''' class DSU:#容量+路径压缩 def __init__(self,n): self.c=[-1]*n def same(self,x,y): return self.find(x)==self.find(y) def find(self,x): if self.c[x]<0: return x self.c[x]=self.find(self.c[x]) return self.c[x] def union(self,u,v): u,v=self.find(u),self.find(v) if u==v: return False if self.c[u]<self.c[v]: u,v=v,u self.c[u]+=self.c[v] self.c[v]=u return True def size(self,x): return -self.c[self.find(x)] class UFS:#秩+路径 def __init__(self,n): self.parent=[i for i in range(n)] self.ranks=[0]*n def find(self,x): if x!=self.parent[x]: self.parent[x]=self.find(self.parent[x]) return self.parent[x] def union(self,u,v): pu,pv=self.find(u),self.find(v) if pu==pv: return False if self.ranks[pu]>=self.ranks[pv]: self.parent[pv]=pu if self.ranks[pv]==self.ranks[pu]: self.ranks[pu]+=1 else: self.parent[pu]=pv def Prime(n): c=0 prime=[] flag=[0]*(n+1) for i in range(2,n+1): if not flag[i]: prime.append(i) c+=1 for j in range(c): if i*prime[j]>n: break flag[i*prime[j]]=prime[j] if i%prime[j]==0: break #print(flag) return flag def dij(s,graph): d={} d[s]=0 heap=[(0,s)] seen=set() while heap: dis,u=heappop(heap) if u in seen: continue for v in graph[u]: if v not in d or d[v]>d[u]+graph[u][v]: d[v]=d[u]+graph[u][v] heappush(heap,(d[v],v)) return d def GP(it): return [[ch,len(list(g))] for ch,g in groupby(it)] class DLN: def __init__(self,val): self.val=val self.pre=None self.next=None def nb(i,j): for ni,nj in [[i+1,j],[i-1,j],[i,j-1],[i,j+1]]: if 0<=ni<n and 0<=nj<m: yield ni,nj @bootstrap def gdfs(r,p): if len(g[r])==1 and p!=-1: yield None for ch in g[r]: if ch!=p: yield gdfs(ch,r) yield None t=N() for i in range(t): h,c,t=RL() if t>=h: ans=1 elif t<=(h+c)/2: ans=2 else: k=(h-t)//(2*t-h-c) ma=(h+k*(h+c))*(2*k+3) mi=(h+(k+1)*(h+c))*(2*k+1) t*=(2*k+1)*(2*k+3) #print(mi,ma) if abs(ma-t)<=abs(mi-t): ans=k*2+1 else: ans=k*2+3 print(ans) ''' sys.setrecursionlimit(200000) import threading threading.stack_size(10**8) t=threading.Thread(target=main) t.start() t.join() ''' ''' sys.setrecursionlimit(200000) import threading threading.stack_size(10**8) t=threading.Thread(target=main) t.start() t.join() '''
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys import string input = sys.stdin.readline import math #import numpy #letters = list(string.ascii_lowercase) from decimal import Decimal l = int(input()) for i in range(l): n = list(map(int, input().split())) a,b = n[0] - n[1], n[2] - n[1] #print(a,b) try: s = int(2/(2 - a/b)) - 1 #s = abs(s) except: s = 0 #print(s) best = 99999999 for j in range(max(s//2,0), s//2 + 3): p = Decimal((a * (j+1))) r = Decimal((2*j + 1)) m_num = p/r #print(m_num) m_num = abs(m_num - b) #print(j, 'j', m_num) if m_num < best: best = m_num ans = 2*j + 1 if a<=b: print(1) elif b<=a/2: print(2) else: if best < abs(a-b): print(ans) else: print(1) #print((24999 * 10**5) / 49999) #print((25000 * 10**5) / 50000)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys from fractions import Fraction def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 10 MOD = 10 ** 9 + 7 EPS = 10 ** -10 def bisearch_max(mn, mx, func): ok = mn ng = mx while ok+1 < ng: mid = (ok+ng) // 2 if func(mid): ok = mid else: ng = mid return ok # m*2+1回カップを入れた時、温度がt以上かどうか def check(m): # 式変形で除算を避ける res = (h*(m+1) + c*m) return res >= t*(m*2+1) for _ in range(INT()): h, c, t = MAP() ev = (h+c) / 2 res = bisearch_max(0, 10**10, check) if res == 10**10-1: print(2) else: mn = INF ans = -1 for m in range(res, res+2): res = (h*(m+1) + c*m) # 分数クラスで小数を避ける diff = Fraction(abs(res - t*(m*2+1)), t*(m*2+1)) if diff < mn: mn = diff ans = m*2 + 1 print(ans)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.PriorityQueue; import java.util.StringTokenizer; 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); solve(in, out); out.close(); } private static void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0) { int h = in.nextInt(), c = in.nextInt(), temp = in.nextInt(); if (temp <= (h + c) / 2) { out.println(2); continue; } long k = getK(h, c , temp); out.println(k); } } private static long getK(int h, int c, int t) { int a = h - t; int b = 2 * t - c - h; int k = 2 * (a / b) + 1; long val1 = Math.abs(k / 2 * 1l * c + (k + 1) / 2 * 1l * h - t * 1l * k); long val2 = Math.abs((k + 2) / 2 * 1l * c + (k + 3) / 2 * 1l * h - t * 1l * (k + 2)); return val1 * (k + 2) <= val2 * k ? k : k + 2; } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
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()) s = [] for i in range(T): s.append(input()) for i in range(T): h, c, t = map(int,s[i].split()) if t == h: n = 1 elif t <= (h+c)/2: n = 2 else: k = (c-t)/(h+c-2*t) n = int(2*k-1) if n%2 == 0: n = n+1 k = (n+1)/2 tb = (k*h + (k-1)*c) / n n1 = n-2 k = (n1+1)/2 tb1 = (k*h + (k-1)*c) / n1 n2 = n+2 k = (n2+1)/2 tb2 = (k*h + (k-1)*c) / n2 if abs(t-tb1)<abs(t-tb) and abs(t-tb1)<abs(t-tb2): n = n1 if abs(t-tb2)<abs(t-tb) and abs(t-tb2)<abs(t-tb1): n = n2 print(n)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math T = input() while(T>0): h,c,t = map(int, raw_input().split()) if ((h + c) == 2*t): print 2 else: x = (c - t)/float(h + c - 2*t) count2 = math.ceil(x) count1 = math.floor(x) # print count1, count2 diff1 = abs(t - (h*count1 + c*(count1 - 1))/(2*count1 - 1)) diff2 = abs(t - (h*count2 + c*(count2 - 1))/(2*count2 - 1)) if (x == 0): print 2 elif count1 < 0 or count2 < 0: print 2 elif diff1 == diff2: d1 = ((h*count1 + c*(count1 - 1)) - t*(2*count1 - 1))*(2*count2 - 1) d2 = (t*(2*count2 - 1) - (h*count2 + c*(count2 - 1)))*(2*count1 - 1) if d1 <= d2: print int(2*count1 - 1) else: print int(2*count2 - 1) elif diff1 < diff2: print int(2*count1 - 1) else: print int(2*count2 - 1) T-=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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int MAX = 100000000; static double h, c, t; public static void main(String[] args) throws Exception { Reader.init(System.in); int T = Reader.nextInt(), cups; StringBuilder sb = new StringBuilder(); for (int i = 0; i < T; i++) { h = Reader.nextInt(); c = Reader.nextInt(); t = Reader.nextInt(); cups = quickCheck(h, c, t); sb.append(cups); sb.append("\n"); } System.out.println(sb.toString()); } public static int quickCheck(double h, double c, double t) { if (t == h) { return 1; } if (t * 2 <= h + c) { return 2; } double k = (h-t)/(2*t-h-c); double kFloor = Math.floor(k); double kCeil = Math.ceil(k); if (kFloor == kCeil) { return (int)k * 2 + 1; } double diffFloor = cmpDiff(kFloor); double diffCeil = cmpDiff(kFloor + 1); // System.out.printf("kF=%.0f kC=%.0f\ndF=%.20f dC=%.20f\n", kFloor, kCeil, diffFloor, diffCeil); if (diffFloor <= diffCeil) { // <= because we want minimum num of cups return (int)kFloor * 2 + 1; } else { return (int)kCeil * 2 + 1; } } public static double cmpDiff(double k) { return Math.abs((t*(2*k+1) - ((k+1)*h + k*c)) / (2*k+1)); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys import os.path import math if __name__ == '__main__': if os.path.isfile("in.txt"): sys.stdin = open("in.txt", "r", encoding="utf-8") for _ in range(int(input())): h, c, t = map(int, input().split()) if t >= h: print(1) elif t < h and 2 * t > h + c: temp = (t - h) / (h + c - 2 * t) n1 = math.floor(temp) n2 = math.ceil(temp) gap11 = abs((n1 + 1) * h + n1 * c - (2 * n1 + 1) * t) gap12 = 2 * n1 + 1 gap21 = abs((n2 + 1) * h + n2 * c - (2 * n2 + 1) * t) gap22 = 2 * n2 + 1 if gap11 * gap22 <= gap12 * gap21: print(2 * n1 + 1) else: print(2 * n2 + 1) else: print(2)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.math.*; import java.util.*; public class Sol2{ public static void main(String[] args) throws IOException{ FastIO sc = new FastIO(System.in); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { double h = sc.nextInt(); double c = sc.nextInt(); double b = sc.nextInt(); double avg = (h+c)/2; if(avg+.00000001>=b) { System.out.println(2); } else { double ans = (int)((h-b)/(b-avg))+1; if((int)ans%2==1) { double first = Math.abs((avg*((ans-1)/(ans)) + h/(ans)-b)); double second = Math.abs((avg)*(ans+1)/(ans+2) + h/(ans+2)-b); //System.out.println(first + " " + second); if(second+.000000001<first) { System.out.println((int)ans+2); }else { System.out.println((int)ans); } }else { System.out.println((int)ans+1); } } } out.close(); } static class FastIO { // Is your Fast I/O being bad? InputStream dis; byte[] buffer = new byte[1 << 17]; int pointer = 0; public FastIO(String fileName) throws IOException { dis = new FileInputStream(fileName); } public FastIO(InputStream is) throws IOException { dis = is; } int nextInt() throws IOException { int ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } long nextLong() throws IOException { long ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } byte nextByte() throws IOException { if (pointer == buffer.length) { dis.read(buffer, 0, buffer.length); pointer = 0; } return buffer[pointer++]; } String next() throws IOException { StringBuffer ret = new StringBuffer(); byte b; do { b = nextByte(); } while (b <= ' '); while (b > ' ') { ret.appendCodePoint(b); b = nextByte(); } return ret.toString(); } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math from fractions import Fraction def eq(A, B, x): return Fraction(x * (A+B) - B, 2 * x - 1) def main(): t = int(input()) for _ in range(t): A,B,C= [int(x) for x in input().split()] if (A+B) / 2 == C: if A-C <= ((A+B) / 2) - C: print('1') else: print('2') else: best_x = (B-C) / (A + B - 2*C) # print(best_x) if best_x < 1: if abs(A-C) <= abs(((A+B) / 2) - C): print('1') else: print('2') else: x1 = math.floor(best_x) x2 = math.ceil(best_x) # print(x1, x2) # print(abs(eq(A, B, x1) - C), abs(eq(A, B, x2) - C)) if abs(eq(A, B, x1) - C) <= abs(eq(A, B, x2) - C): res = (x1 * 2 - 1) else: res = (x2 * 2 - 1) print(res) main()
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from fractions import Fraction as F def solve(): h, c, t = map(int, input().split()) dt = abs(F(h - t)) ans = 1 def update(u, v): nonlocal dt, ans if u <= 0 or v < 0: return w = F(u * h + v * c, u + v) if abs(w - t) < dt: dt = abs(w - t) ans = u + v update(1, 1) ax = (h - t) // (2 * t - h - c) if 2 * t - h - c else 0 for x in range(ax - 3, ax + 4): update(x + 1, x) print(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
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { double h, c, tt; cin >> h >> c >> tt; if (2 * tt <= h + c) { cout << "2" << "\n"; continue; } long long r = (h - tt) / (2 * tt - h - c); long long s = r + 1; double r1, s1; r1 = r * c + (r + 1) * h; s1 = s * c + (s + 1) * h; r1 = r1 / (2 * r + 1); s1 = s1 / (2 * s + 1); r1 = abs(tt - r1); s1 = abs(tt - s1); if (r1 <= s1) cout << 2 * r + 1 << "\n"; else cout << 2 * s + 1 << "\n"; } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys, math,os from io import BytesIO, IOBase from bisect import bisect_left as bl, bisect_right as br, insort #from heapq import heapify, heappush, heappop from collections import defaultdict as dd, deque, Counter #from itertools import permutations,combinations def data(): return sys.stdin.readline().strip() def mdata(): return list(map(int, data().split())) def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n') def out(var) : sys.stdout.write(str(var)+'\n') sys.setrecursionlimit(100000) from fractions import Fraction INF = float('inf') mod = 998244353 def main(): for t in range(int(data())): h, c, t = mdata() if (h + c) / 2 >= t: out(2) continue if t==h: out(1) continue k = int((h - t) // abs((h + c) / 2 - t)) k -= 2 if k % 2 != 0: k -= 1 k=max(0,k) if Fraction(abs(2*t*(k+5) - ((k + 4) * (h + c) + 2*h)), (k + 5)) < Fraction(abs(2*t*(k+1) - ((k) * (h + c) + 2*h)), (k + 1)) and Fraction(abs(2*t*(k+5) - ((k + 4) * (h + c) + 2*h)), (k + 5)) < Fraction(abs(2*t*(k+3) - ((k + 2) * (h + c) + 2*h)), (k + 3)): out(k + 5) elif Fraction(abs(2*t*(k+1) - ((k) * (h + c) + 2*h)), (k + 1)) <= Fraction(abs(2*t*(k+3) - ((k + 2) * (h + c) + 2*h)), (k + 3)): out(k + 1) else: out(k + 3) if __name__ == '__main__': main()
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys from fractions import Fraction def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 10 MOD = 10 ** 9 + 7 EPS = 10 ** -10 def bisearch_max(mn, mx, func): ok = mn ng = mx while ok+1 < ng: mid = (ok+ng) // 2 if func(mid): ok = mid else: ng = mid return ok # m*2+1回カップを入れた時、温度がt以上かどうか def check(m): # 式変形で除算を避ける res = (h*(m+1) + c*m) return res >= t*(m*2+1) for _ in range(INT()): h, c, t = MAP() ev = (h+c) / 2 res = bisearch_max(0, 10**10, check) if res == 10**10-1: print(2) else: mn = INF ans = -1 for m in range(max(res-10, 0), res+10): res = (h*(m+1) + c*m) # 分数クラスで小数を避ける diff = Fraction(abs(res - t*(m*2+1)), t*(m*2+1)) if diff < mn: mn = diff ans = m*2 + 1 print(ans)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
numcases = int(input()) for _ in range(numcases): h, c, t = map(int, input().split()) ans = 0 minc = float('inf') if h + c != t*2: diff = t - h change_diff = t*2 - (h + c) if diff > 0 and change_diff < 0 or diff < 0 and change_diff > 0: mindiff = abs(diff)//abs(change_diff) val1 = mindiff*(t*2 - (h + c)) + t - h val2 = (mindiff + 1)*(t*2 - (h + c)) + t -h if abs(val1)/(mindiff*2 + 1) <= abs(val2)/(mindiff*2 + 3): ans = mindiff*2 + 1 else: ans = mindiff*2 + 3 if abs(t*2 - h + c) < abs(val1) and abs(t*2 - h + c) < abs(val2): ans = 2 elif diff == 0: ans = 1 else: if abs(t*2 - (h + c)) > abs(t - h): ans = 1 else: ans = 2 else: 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
import java.io.*; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; /** * @author Tran Anh Tai * @template for CP codes */ public class ProbC { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } // main solver static class Task{ public void solve(InputReader in, PrintWriter out) { int test = in.nextInt(); for (int i = 0 ; i < test; i++){ int h = in.nextInt(); int c = in.nextInt(); int t = in.nextInt(); BigDecimal d = new BigDecimal(Math.abs((double) (h + c) / 2 - t) + ""); int result = 2; if (t >= h || h == c){ out.println(1); } else if (t <= (h + c) / 2){ out.println(2); } else{ double ratio = (double)(t - c) / (h - c); double au = 1 / (2 - 1 / ratio); int time = (int)au; BigDecimal a = new BigDecimal(t + ""); BigDecimal x = eval(h, c, time).subtract(a); BigDecimal y = a.subtract(eval(h, c, time + 1)); if (x.compareTo(d) == -1){ result = 2 * time - 1; d = x; } if (y.compareTo(d) == -1){ result = 2 * time + 1; } out.println(result); } } } BigDecimal eval(int h, int c, int time){ BigDecimal m = new BigDecimal((long)h * time + (long)c * (time - 1) + ""); BigDecimal n = new BigDecimal(2 * time - 1 + ""); return m.divide(n, 6, RoundingMode.CEILING); } } // fast input reader class; static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong(){ return Long.parseLong(nextToken()); } } }
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; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long tt; cin >> tt; while (tt--) { float h, c, t, tot, x2; float d1 = 0.0, d2 = 0.0, a = 0.0, min = 100000000, diff = 0; long long i; cin >> h >> c >> t; a = (h + c) / 2; if (t <= a) tot = 2; else { long long x1 = floor((h - t) / (t - a)); for (i = x1; i <= x1 + 5; i++) { if (i % 2 == 1) { diff = abs(((i - 1) * a + h) / i - t); if (diff < min) { min = diff; tot = i; } } } } cout << tot << "\n"; } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys from math import inf input = sys.stdin.readline def prog(): for _ in range(int(input())): h,c,t = map(int,input().split()) lowest = (h+c)/2 if t <= lowest: print(2) elif abs(t-(2*h+c)/3) >= abs(t-h): print(1) else: closest = (h-c)//(t-lowest) closest -= (closest % 2) if closest % 4 == 0: closest -= 2 closest = int(closest) first = abs(t - lowest - (h-c)/closest) second = abs(t - lowest - (h-c)/(closest+4)) if first <= second: print(closest//2) else: print(closest//2 + 2) prog() ''' import sys from math import inf import math from decimal import Decimal input = sys.stdin.readline def prog(): for _ in range(int(input())): h,c,t = map(int,input().split()) lowest = (h+c)/2 if t <= lowest: print(2) elif abs(t-(2*h+c)/3) >= abs(t-h): print(1) else: closest = (h-c)//(t-lowest) closest -= (closest % 2) closest = int(closest) if closest % 4 == 0: closest -= 2 a = Decimal(lowest) + Decimal((h-c))/Decimal(closest) b = Decimal(lowest) + Decimal((h-c))/Decimal(closest+4) first = a-t second = t-b print(first) print(second) if first <= second: print(int(closest//2)) else: print(int(closest//2) + 2) '''
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h,c,t=[int(c) for c in input().split()] if t <= (h + c)/2: print(2) else: ##finding k a=h-t b=2*t - h-c k=2*(a//b) +1 # k = ( t - h) // (h+c - 2*t) ##comparing k and k + 1 a =abs((k//2)*c + ((k+1)//2)*h - t*k) # a=abs(a) b= abs(((k+2)//2)*c + ((k+3)//2)*h - t*(k+2)) if a*(k+2) <= b*(k) : ans=k else: ans=k + 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
for test_ in range(int(input())): h, c, t = map(int, input().split()) if t <= (h + c) // 2: print(2) else: t -= (h + c) / 2 t1 = ((h - c) / 2) / t x1, x2 = int(((t1 - 1) // 2) * 2 + 1), int(((t1 + 1) // 2) * 2 + 1) if (h - c) / (x1 * 2) - t <= t - (h - c) / (x2 * 2): print(x1) else: print(x2)
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
# https://codeforces.com/problemset/problem/1359/C import sys import os import heapq import math try: path = "./file/input.txt" if os.path.exists(path): sys.stdin = open(path, 'r') # sys.stdout = open(r"./file/output.txt", 'w') except: pass T = int(input()) def printd(value): # print(value) pass for _ in range(T): arr = list(map(int, input().split(" "))) h, c, t = arr[0], arr[1], arr[2] result = 1 if t == h: result = 1 elif t * 2 <= (h + c): result = 2 elif t * 3 >= (h * 2 + c): result = 1 if abs(h * 3 - t * 3) <= abs((h * 2 + c) - t * 3) else 3 else: x = (t - c) / (2 * t - h - c) x = int(x) start = max(2, x - 5) top = 99999999999 bottom = 1 for i in range(start, start + 15): bottomi = abs(i * 2 - 1) topi = abs((h * i + c * (i - 1)) - bottomi * t) if top * bottomi > topi * bottom: top = topi bottom = bottomi result = i * 2 - 1 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
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; import java.io.*; public class C_Mixing_Water{ public static void main(String[] args) { FastReader sc=new FastReader(); PrintWriter out = new PrintWriter(System.out); int test = sc.nextInt(); for(int t=0;t<test;t++){ double a = sc.nextInt(); double b = sc.nextInt(); double c = sc.nextInt(); if(c>=a)out.println(1); else{ // double diff = Math.abs(a-c); // double currentDiff = Double.MAX_VALUE; // int n = 1; // int m = 0; // boolean hot = false; // while(true){ // if(hot){ // n++; // hot = false; // double temp = ((n*a)+((m*b)))/(n+m); // currentDiff = Math.abs(temp-c); // if(currentDiff>=diff){ // n--; // break; // } // else { // diff = currentDiff; // } // } // else{ // m++; // hot = true; // double temp = ((n*a)+((m*b)))/(n+m); // currentDiff = Math.abs(temp-c); // if(currentDiff>=diff){ // m--; // break; // } // else { // diff = currentDiff; // } // } // } // out.println(n+m); double diff = Math.abs(a-c); int ans = 1; double avg = (double)(a+b)/(double)2; if(diff>Math.abs(avg-c)){ diff = Math.abs(avg-c); ans = 2; } // n n-1 double num = b-c; double dem = a+b-(2*c); double div = num/dem; int n = (int)Math.round(div); double temp = (double)((n*a)+((n-1)*b))/(double)(2*n-1); double temp1 = Math.abs(temp-c); if(diff>temp1){ diff = temp1; ans = n+n-1; } n++; temp = (double)((n*a)+((n-1)*b))/(double)(2*n-1); temp1 = Math.abs(temp-c); if(diff>temp1){ diff = temp1; ans = n+n-1; } // n+1 n // num = c-a; // dem = a+b-(2*c); // div = num/dem; // n = (int)Math.round(div); // temp = (double)((n*b)+((n+1)*a))/(double)(2*n+1); // temp1 = Math.abs(temp-c); // if(diff>temp1){ // diff = temp1; // ans = n+n+1; // } // n++; // temp = (double)((n*b)+((n+1)*a))/(double)(2*n+1); // temp1 = Math.abs(temp-c); // if(diff>temp1){ // diff = temp1; // ans = n+n+1; // } if(ans<0){ ans = 2; } out.println(ans); } } out.close(); } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys from array import array # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') tc = int(input()) ans = [0] * tc for ti in range(tc): h, c, t = map(int, input().split()) if abs(3 * t - (h + c + h)) >= abs(3 * t - 3 * h): ans[ti] = 1 continue if h + c >= 2 * t: ans[ti] = 2 continue ok, ng = 1, 10**9 while abs(ok - ng) > 1: mid = (ok + ng) >> 1 if h * (mid + 1) + c * mid >= t * (2 * mid + 1): ok = mid else: ng = mid x = ok if ( abs((4 * x**2 + 8 * x + 3) * t - (2 * x + 3) * (h * (x + 1) + c * x)) <= abs((4 * x**2 + 8 * x + 3) * t - (2 * x + 1) * (h * (x + 2) + c * (x + 1))) ): ans[ti] = 2 * x + 1 else: ans[ti] = 2 * x + 3 sys.stdout.buffer.write('\n'.join(map(str, ans)).encode('utf-8'))
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.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; import java.util.Stack; import static java.lang.Math.abs; import static java.lang.Math.ceil; /* tutorial solution much better*/ public class Main { public static int dirr[][] = {{1,0},{0,1},{0,-1},{-1,0}}; public static boolean possible; public static void main(String[] args) { // write your code here // q1365D(); // q1354D(); // q1363C(); // q545C(); // q1361A(); q1359C(); } public static void q1359C(){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++){ double h = sc.nextInt(); double c = sc.nextInt(); double temp = sc.nextInt(); if((h+c)==2*temp){ System.out.println("2"); } else { Double k = (h-temp)/(2*temp-h-c); if(k<0){ System.out.println("2"); continue; } Integer k1 = k.intValue()+1; // System.out.println(k); double p = abs((k.intValue()*(h+c)+h)-temp*(2*k.intValue()+1))*(2*k.intValue()+3); double q = abs(((k.intValue()+1)*(h+c)+h)-temp*(2*k.intValue()+3))*(2*k.intValue()+1); // System.out.println(p+" "+q); if(p<=q){ System.out.println(k.intValue()*2+1); } else { System.out.println(k1.intValue()*2+1); } } } } public static void q1361A(){ Scanner sc = new Scanner(System.in); ArrayList<ArrayList<Integer>> adj = new ArrayList<>(); ArrayList<ArrayList<Integer>> occur = new ArrayList<>(); for(int i=0;i<=1000000;i++){ adj.add(new ArrayList<>()); occur.add(new ArrayList<>()); } int[] color = new int[1000000]; int n = sc.nextInt(); int m = sc.nextInt(); for(int i=0;i<m;i++){ int c = sc.nextInt(); int d = sc.nextInt(); adj.get(c).add(d); adj.get(d).add(c); } for(int i=0;i<n;i++){ int temp = sc.nextInt(); occur.get(temp).add(i+1); color[i+1] = temp; if(temp > n){ System.out.println("-1"); return; } } ArrayList<Integer> ans = new ArrayList<>(); int[] temp = new int[n+1]; for(int i=1;i<=n;i++){ for(Integer p:occur.get(i)){ int cnt =i-1; for(Integer q: adj.get(p)){ if(color[q]==i){ System.out.println("-1"); return; } else if(color[q]<i){ if(temp[color[q]]!=p) { temp[color[q]] = p; cnt--; } } } if(cnt!=0){ System.out.println("-1"); return; } //System.out.print(" "+p); ans.add(p); } } for(int i=0;i<ans.size()-1;i++){ System.out.print(ans.get(i)+" "); } System.out.print(ans.get(ans.size()-1)); } public static void q545C(){ Scanner sc = new Scanner(System.in); List<Corrd> arr = new ArrayList<>(); int n = sc.nextInt(); int ans=n-2<=0?n:2; for(int j=0;j<n;j++){ arr.add(new Corrd(sc.nextInt(),sc.nextInt())); } int closest = arr.get(0).x; for(int j=1;j<arr.size()-1;j++){ Corrd tree = arr.get(j); int farthest = arr.get(j+1).x; if(closest<tree.x-tree.y){ ans++; closest=tree.x; } else if(farthest>tree.x+tree.y){ ans++; closest=tree.x+tree.y; } else{ closest= tree.x; } } System.out.println(ans); } public static void q1363C(){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++){ int deg =0; int n = sc.nextInt(); int x = sc.nextInt(); for(int j=0;j<n-1;j++){ int p = sc.nextInt(); int q = sc.nextInt(); if(p==x || q==x){ deg++; } } if(deg<=1){ System.out.println("Ayush"); } else{ if(n%2==0){ System.out.println("Ayush"); } else{ System.out.println("Ashish"); } } } } public static void q1354D(){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int q = sc.nextInt(); int[] cnt= new int[n+1]; for(int i=0;i<n;i++){ cnt[sc.nextInt()]++; } for(int i=0;i<q;i++){ int k = sc.nextInt(); if(k>0){ cnt[k]++; } else{ k=k*(-1); } } } public static void q1365D(){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++){ possible = true; int n = sc.nextInt(); int m = sc.nextInt(); Integer[][] graph = new Integer[n+1][m+1]; Corrd[][] corrds = new Corrd[n+1][m+1]; for(int j=0;j<n;j++){ String str = sc.next(); for(int k=0;k<m;k++){ graph[j][k] = (int)str.charAt(k); // System.out.println(graph[j][k]); corrds[j][k] = new Corrd(j,k); } } dfs(n-1,m-1,n,m,graph,corrds); if(!possible){ System.out.println("NO"); continue; } dfs2(n-1,m-1,n,m,graph,corrds); for(int j=0;j<n;j++){ for(int k=0;k<m;k++){ // System.out.print((char)((int)graph[j][k])); if(graph[j][k]=='G'){ // System.out.print(corrds[j][k].color); if(corrds[j][k].color<2){ // System.out.print("Mee"+corrds[j][k].color); possible = false; break; } } } // System.out.println(); } System.out.println(possible?"YES":"NO"); } } public static void dfs(int x,int y,int maxx,int maxy,Integer[][] graph,Corrd[][] corrds) { Stack<Corrd> st = new Stack<Corrd>(); if (graph[x][y] == '#') { return; } st.push(corrds[x][y]); corrds[x][y].color++; while (!st.empty()) { Corrd p = st.pop(); // System.out.println(p.x + " " + p.y); for (int i = 0; i < 4; i++) { int nx = p.x + dirr[i][0]; int ny = p.y + dirr[i][1]; if (nx >= 0 && nx < maxx && ny >= 0 && ny < maxy && corrds[nx][ny].color == 0) { // System.out.println("x" + nx + " " + ny); if (!(graph[nx][ny] == '#')) { if (graph[nx][ny] == 'B' && (graph[p.x][p.y] == '.')) { graph[p.x][p.y] = (int) '#'; break; } else { if (graph[nx][ny] == 'B' && graph[p.x][p.y] == 'G') { possible = false; break; } else { corrds[nx][ny].color++; st.push(corrds[nx][ny]); } } } } } } } public static void dfs2(int x,int y,int maxx,int maxy,Integer[][] graph,Corrd[][] corrds){ Stack<Corrd> st = new Stack<Corrd>(); if(graph[x][y]=='#'){ return; } st.push(corrds[x][y]); corrds[x][y].color++; while(!st.empty()){ Corrd p = st.pop(); // System.out.println(p.x+" "+p.y); for(int i=0;i<4;i++){ int nx = p.x+dirr[i][0]; int ny = p.y+dirr[i][1]; if(nx>=0 && nx<maxx && ny>=0 && ny<maxy && corrds[nx][ny].color==1){ if(!(graph[nx][ny]=='#')){ corrds[nx][ny].color++; st.push(corrds[nx][ny]); } } } } } public static class Corrd{ int x; int y; int color; Corrd(int x,int y){ this.x = x; this.y = y; this.color =0; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
# -*- coding: utf-8 -*- """ Created on Fri May 29 13:34:56 2020 @author: default """ #%% def count(h, c, t): if h+c >= 2*t: ans = 2 else: k = int((h - t) / (2*t - h - c)) if 4*k*(k+1)*(h+c)+(6*k+5)*h+c*(2*k+1) <= 2*t*(2*k+1)*(2*k+3): ans = 2*k + 1 else: ans = 2*k + 3 return ans #%% t = int(input()) for _ in range(t): h, c, t = map(int, input().split()) print(count(h, c, t))
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 { // HashMap<> map=new HashMap<>(); // TreeMap<> map=new TreeMap<>(); // map.put(p,map.getOrDefault(p,0)+1); // for(Map.Entry<> mx:map.entrySet()){ // int v=mx.getValue(),k=mx.getKey(); // } // ArrayList<Pair<Character,Integer>> l=new ArrayList<>(); // ArrayList<> l=new ArrayList<>(); // HashSet<> has=new HashSet<>(); PrintWriter out; FastReader sc; public void sol(){ int te=ni(); while(te-->0){ int h=ni(),c=ni(); int t=ni(); if(h==t)pl("1"); else if((h+c)/2>=t)pl("2"); else{ int p=h+c-2*t; int q=t-h; int f=((q)/(p)); int g=f+1; long x=(abs((((f*1l)+1l)*(h*1l)+(c*1l)*f)-t*(2l*f+1l)))*(2l*g+1l); long y=(abs((((g*1l)+1l)*(h*1l)+(c*1l)*g)-t*(2l*g+1l)))*(2l*f+1l); // pl(x+" "+y); // long r=abs(t-x),s=abs(t-y); // pl(r+" "+s); if(x>y){ pl(2*g+1); }else pl(2*f+1); } } } public static void main(String[] args) { Main g=new Main(); g.out=new PrintWriter(System.out); g.sc=new FastReader(); g.sol(); g.out.flush(); } 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; } } public int ni(){ return sc.nextInt(); }public long nl(){ return sc.nextLong(); }public double nd(){ return sc.nextDouble(); }public String rl(){ return sc.nextLine(); }public void pl(Object s){ out.println(s); }public void ex(){ out.println(); } public void pr(Object s){ out.print(s); }public String next(){ return sc.next(); }public long abs(long x){ return Math.abs(x); } public int abs(int x){ return Math.abs(x); } public double abs(double x){ return Math.abs(x); } public long pow(long x,long y){ return (long)Math.pow(x,y); } public int pow(int x,int y){ return (int)Math.pow(x,y); } public double pow(double x,double y){ return Math.pow(x,y); }public long min(long x,long y){ return (long)Math.min(x,y); } public int min(int x,int y){ return (int)Math.min(x,y); } public double min(double x,double y){ return Math.min(x,y); }public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); }static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort(double[] a) { ArrayList<Double> l = new ArrayList<>(); for (double i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }int swap(int a,int b){ return a; }long swap(long a,long b){ return a; }double swap(double a,double b){ return a; } boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0); }boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); }public long max(long x,long y){ return (long)Math.max(x,y); } public int max(int x,int y){ return (int)Math.max(x,y); } public double max(double x,double y){ return Math.max(x,y); }long sqrt(long x){ return (long)Math.sqrt(x); }int sqrt(int x){ return (int)Math.sqrt(x); }void input(int[] ar,int n){ for(int i=0;i<n;i++)ar[i]=ni(); }void input(long[] ar,int n){ for(int i=0;i<n;i++)ar[i]=nl(); }int maxint(){ return Integer.MAX_VALUE; }int minint(){ return Integer.MIN_VALUE; }long maxlong(){ return Long.MAX_VALUE; }long minlong(){ return Long.MIN_VALUE; } public static class pair implements Comparable<pair> { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + "," + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Double(x).hashCode() * 31 + new Double(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≤ T ≤ 3 ⋅ 10^4) — the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≤ c < h ≤ 10^6; c ≤ t ≤ h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from fractions import Fraction t = int(input()) for testCase in range(t): h, c, t = [int(_) for _ in input().split()] if h == t: print(1) elif t <= (h+c)/2: print(2) else: sumTemp = h+c minGuess = 0 maxGuess = 555555 while maxGuess-minGuess > 1: guess = (maxGuess + minGuess) // 2 guessTemp = Fraction((h+sumTemp*guess), (2*guess+1)) if guessTemp < t: maxGuess = guess else: minGuess = guess diffLow = abs(Fraction((h+sumTemp*minGuess), (2*minGuess+1)) - t) diffHi = abs(Fraction((h+sumTemp*maxGuess), (2*maxGuess+1)) - t) if diffLow <= diffHi: print(minGuess * 2 + 1) else: print(maxGuess * 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
def solve(): [hi, lo, barrel] = map(int, input().split()) if 2 * barrel <= hi + lo: return 2 items = (hi - barrel) // (2 * barrel - hi - lo) temp = lambda t: (((t + 1) * hi + t * lo) - (2 * t + 1) * barrel) / (2 * t + 1) if abs(temp(items)) > abs(temp(items + 1)): items += 1 return 2 * items + 1 if __name__=='__main__': for _ in range(int(input())): print(solve())
PYTHON3