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
FAST_IO = 0 if FAST_IO: import io, sys, atexit rr = iter(sys.stdin.read().splitlines()).next sys.stdout = _OUTPUT_BUFFER = io.BytesIO() @atexit.register def write(): sys.__stdout__.write(_OUTPUT_BUFFER.getvalue()) else: rr = raw_input rri = lambda: int(rr()) rrm = lambda: map(int, rr().split()) #### from fractions import Fraction as Fra def solve(H, C, T): if H+C == 2 * T: if H == T: return 1 return 2 else: r = int((T-H) / (H+C-2*T)) ansv = ansi = float('inf') #print('breaking', r) for v in (2*r - 5, 2*r-3, 2*r-1, 2*r+1, 2*r+3, 2*r+5): if v<0: continue temp = H + (v // 2) * (H + C) - T * v temp = Fra(abs(temp), v) if (temp) < ansv: ansv = (temp) ansi = v elif (temp) == ansv and v < ansi: ansi = v t2 = Fra(abs(H+C - 2*T), 2) if ansv > t2: return 2 if ansv == t2: return min(ansi, 2) return ansi for tc in xrange(rri()): print solve(*rrm())
PYTHON
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.Scanner; public class er88c3 { 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
n = int(input()) for i in range(n): read = input().split(' ') a = float(read[0]) b = float(read[1]) c = float(read[2]) if c == a: print(1) elif abs(c - (a + b)/2) == 0: print(2) else: k = abs(c-a)/abs(a+b-2*c) k = int(2*k + 1) # print(k) avr = abs(c-a) res = 1 if(abs(c - (a+b)/2) < avr): avr = abs(c - (a+b)/2) res = 2 Min = k Max = min(1000000,k+2) # print(Min,Max) for j in range(Min,Max+1): if(j%2): # print(j) h1 = int(j/2) + 1 c1 = int(j/2) current = abs(c - ((a*h1 + b*c1)/j)) # print(current, avr) if(current < avr): avr = current res = j print(res)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.StringTokenizer; public class P1359C { 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(); boolean debug = !Boolean.parseBoolean(System.getProperty("ONLINE_JUDGE")); solver.solve(in, out, debug); out.close(); } static class Task { public void solve(InputReader in, PrintWriter out, boolean debug) { int _t = in.nextInt(); for (int i = 0; i < _t; i++) { long h = in.nextInt(), c = in.nextInt(), t = in.nextInt(); if (t == h) { out.println(1); } else if (2 * t == h + c) { out.println(2); } else { // n = (t-h)/(h+t-2*c) var num = BigDecimal.valueOf(t - h); var den = BigDecimal.valueOf(h + c - 2 * t); long n1 = num.divide(den, RoundingMode.CEILING).longValue(); long n2 = num.divide(den, RoundingMode.FLOOR).longValue(); long[] ns = new long[]{n1, n2}; final var mc = new MathContext(100); var minErr = den.divide(BigDecimal.valueOf(2), mc).abs(); long minN = 2; for (long n : ns) { if (2 * n + 1 <= 0) { continue; } var f = BigDecimal.valueOf((n + 1) * h + n * c) .divide(BigDecimal.valueOf(2 * n + 1), mc); var err = BigDecimal.valueOf(t).subtract(f).abs(); if (err.compareTo(minErr) < 0 || (err.compareTo( minErr) == 0 && 2 * n + 1 < minN)) { minErr = err; minN = 2 * n + 1; } } out.println(minN); } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.math.BigDecimal; import java.math.MathContext; import java.util.*; public class A implements Runnable { FastReader sc; PrintWriter out; void solve() { int tc = sc.ni(); outer: while(tc-- > 0) { long h = sc.nl(); long c = sc.nl(); long t = sc.nl(); double a = 0.0d; int sum = 0; /* for(int i = 1; i <= 20; i++) { if(i%2 == 1) { sum += h; } else sum += c; a = sum/(double)i; System.out.println(a); } */ double mi = (h+c)/(double)2; //System.out.println(mi); if(t == h) { out.println(1); continue outer; } else if(((h+c)%2 == 0) && (t <= mi)) { out.println(2); continue outer; } else if(((h+c)%2 != 0) && (t < mi)) { out.println(2); continue outer; } long l = 1; long u = (long)1e10; BigDecimal mind = new BigDecimal(Double.MAX_VALUE); BigDecimal tb = new BigDecimal(t); long ans = -1; while(l <= u) { long m = l+(u-l)/2; long num = m*h + (m-1)*c; long den = 2*m-1; //double cur = (m*h + (m-1)*c)/(double)(2*m-1); BigDecimal numb = new BigDecimal(num); BigDecimal denb = new BigDecimal(den); BigDecimal curb = numb.divide(denb,MathContext.DECIMAL128); //System.out.println(m*h + (m-1)*c); //System.out.println(2*m-1); //System.out.println(l+" "+u+" "+m+" "+curb+" "+(2*m-1)); BigDecimal cd = curb.subtract(tb).abs(); //System.out.println(cd); if(cd.compareTo(mind) < 0 || (cd.compareTo(mind) == 0 && (2*m-1) < ans)) { ans = 2*m-1; mind = cd; } if(curb.compareTo(tb) > 0) { l = m+1; } else if(curb.compareTo(tb) < 0) { u = m-1; } else { break; } } out.println(ans); } } class Pair implements Comparable<Pair> { int x; int y; Pair(int x,int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { if(x != o.x) { if(x > o.x) return 1; //increasing order else return -1; } else { if(y > o.y) return 1; //increasing order else return -1; } } } public void run() { out = new PrintWriter(System.out); sc = new FastReader(); solve(); out.flush(); } public static void main(String[] args) { new Thread(null, new A(), "Main", 1 << 28).start(); } 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 ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nln() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nia(int n) { int[] ar = new int[n]; for (int i = 0; i < n; i++) ar[i] = ni(); return ar; } long[] nla(int n) { long[] ar = new long[n]; for (int i = 0; i < n; i++) ar[i] = nl(); return ar; } int[][] nim(int n, int m) { int[][] ar = new int[n][]; for (int i = 0; i < n; i++) { ar[i] = nia(m); } return ar; } long[][] nlm(int n, int m) { long[][] ar = new long[n][]; for (int i = 0; i < n; i++) { ar[i] = nla(m); } return ar; } String reverse(String s) { StringBuilder r = new StringBuilder(s); return r.reverse().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 java.util.Scanner; /** * @εˆ›ε»ΊδΊΊ YDL * @εˆ›ε»Ίζ—Άι—΄ 2020/5/9 22:34 * @描述 */ public class Main { private static manyFunc<Integer,Long> f= (n,h,l)->((long)(h+l)*n-l); public static void main(String[] args) { Scanner sc = new Scanner(System.in); int len = sc.nextInt(); while(len-->0) { int h = sc.nextInt(); int c = sc.nextInt(); int t = sc.nextInt(); if(t<<1<=h+c||t<<1<c<<1){ System.out.println(2); }else if(t>=h){ System.out.println(1); }else { int n = (h - t) / ((t<<1) - h - c); if (Math.abs(n * (h + c) + h - ((n<<1) + 1) * t) * (2 * n + 3) <= Math.abs((n + 1) * (h + c) + h - (2 * n + 3) * t) * (2 * n + 1)) { System.out.println(1+(n<<1)); } else { System.out.println(3+(n<<1)); } } } } @FunctionalInterface interface manyFunc<A,F>{ F solve(A a,A b,A c); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> #pragma warning(disable : 4996) const int maxn = 1e5 + 5; const double Inf = 10000.0; const double PI = acos(-1.0); using namespace std; double h, c, t; double a; double f1(int x) { return ((x)*h + (x - 1) * c) / (1.0 * (2 * x - 1)); } double f2(int x) { return ((x - 1) * h + (x)*c) / (1.0 * (2 * x - 1)); } int b_1() { int l = 1, r = 0x3f3f3f3f; while (l < r) { int mid = (l + r) / 2; if (f1(mid) > t) l = mid + 1; else r = mid; } double del1 = fabs(t - f1(l)); double del2; l == 1 ? del2 = 0x3f3f3f3f : del2 = fabs(t - f1(l - 1)); double del3 = fabs(t - a); if (del3 <= del1 && del3 <= del2) return 2; if (del1 < del2) return 2 * l - 1; else return 2 * l - 3; } int b_2() { int l = 1, r = 0x3f3f3f3f; while (l < r) { int mid = (l + r + 1) / 2; if (f2(mid) < t) l = mid; else r = mid - 1; } double del1 = fabs(t - f1(l)); double del2; del2 = fabs(t - f1(l + 1)); double del3 = fabs(t - a); if (del3 <= del1 && del3 <= del2) return 2; if (del1 < del2) return 2 * l - 1; else return 2 * l + 1; } int main() { int T; scanf("%d", &T); while (T--) { scanf("%lf%lf%lf", &h, &c, &t); a = (h + c) / 2; if ((fabs(a - t) < 1e-8) && (fabs(h - t) < 1e-8)) puts("1"); else if ((fabs(a - t) < 1e-8)) puts("2"); else if (t > a) { printf("%d\n", b_1()); } else printf("%d\n", b_2()); } 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.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class C_Mixing_Water { public static void main(String[] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int T = sc.nextInt(); while(T-- > 0) { double a = sc.nextInt(); double b = sc.nextInt(); long desiredTemp = sc.nextInt(); long lo = 0; long hi = 10000000; //bin search on the number of b cups while(lo + 1 < hi) { int mid = (int) (lo + (hi - lo)/2); double temp = currTemp(a, b, mid); if(temp <= desiredTemp) { hi = mid; }else { lo = mid; } } long a_l = (long) a; long b_l = (long) b; long v1 = Math.abs((lo*(a_l + b_l) + a_l) - desiredTemp*(2*lo + 1))*(2*hi + 1); long v2 = Math.abs((hi*(a_l + b_l) + a_l) - desiredTemp*(2*hi + 1))*(2*lo + 1); //delta(hi*(a_l + b_l) + a_l, desiredTemp*(2*hi + 1)) * (2*hi + 1); int binVal = v1 <= v2 ? (int) lo : (int) hi; if(Math.abs((a/2 + b/2) - desiredTemp) <= Math.abs(currTemp(a, b, binVal) - desiredTemp)) { out.println(2); }else { out.println(binVal + binVal + 1); } } out.close(); } public static double currTemp(double a, double b, int count) { return (a*(count + 1) + b*(count))/(2*count + 1); } private static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math for i in range(int(input())): h, c, t = map(int, input().split()) if (h + c) / 2 >= t: print(2) continue if h <= t: print(1) continue left = 1 right = 1000000000 while right - left > 1: n = (right + left) // 2 temp_1 = (n * h + (n - 1) * c) / (2 * n - 1) if temp_1 <= t: right = n else: left = n if abs((left * h + (left - 1) * c) * (2 * right - 1) - t * (2 * left - 1) * (2 * right - 1)) <= abs((right * h + (right - 1) * c) * (2 * left - 1) - t * (2 * right - 1) * (2 * left - 1)): print(left * 2 - 1) else: print(right * 2 - 1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; long double h, c, t; vector<pair<double, int> > pot; void bs() { long long lo = 0, hi = 1e9, mid; long long bestN = -1; long double bestDif = 1e9; while (lo <= hi) { mid = (lo + hi) / 2; long double tmp = ((mid + 1) * h + mid * c) / (2 * (mid + 1) - 1); long double error = abs(tmp - t); if (tmp > t) lo = mid + 1; else hi = mid - 1; if (error < bestDif) { pot.clear(); bestN = mid; bestDif = error; pot.push_back({bestDif, (2 * (bestN + 1)) - 1}); } else if (error == bestDif) pot.push_back({error, (2 * (mid + 1)) - 1}); } } long long solve() { pot.clear(); bs(); double avg = (h + c) / 2; pot.push_back({abs(avg - t), 2}); pot.push_back({abs(h - t), 1}); sort(pot.begin(), pot.end()); return pot[0].second; } int main() { cin.sync_with_stdio(0); cin.tie(0); int TC; cin >> TC; while (TC--) { cin >> h >> c >> t; long long ans = solve(); cout << ans << "\n"; } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; vector<long long int> pr; void SieveOfEratosthenes() { long long int n = 1000000; bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (long long int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (long long int i = p * p; i <= n; i += p) prime[i] = false; } } for (long long int p = 2; p <= n; p++) if (prime[p]) pr.push_back(p); } void solve() { long double h, c, t; cin >> h >> c >> t; if (t == h) { cout << "1" << endl; return; } long double ans = (long double)(h + c) / (2.0); if (ans >= t) { cout << "2" << endl; return; } long double xx = (long double)((long double)(c - t) / (h + c - 2 * t)); long double xx1 = floor(xx); long double xx2 = ceil(xx); long double a1, a2; a1 = (long double)((h * ceil(xx1)) + (c * (ceil(xx1) - 1))) / (long double)(2 * ceil(xx1) - 1); a2 = (long double)((h * ceil(xx2)) + (c * (ceil(xx2) - 1))) / (long double)(2 * ceil(xx2) - 1); if (abs(a1 - t) <= abs(ans - t)) { if (abs(a1 - t) <= abs(a2 - t)) cout << 2 * ceil(xx1) - 1 << endl; else cout << 2 * ceil(xx2) - 1 << endl; } else if (abs(a2 - t) > abs(ans - t)) cout << "2" << endl; else cout << 2 * ceil(xx2) - 1 << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; cin >> t; while (t) { solve(); t--; } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; long double h, c, t; long long bs() { long long lo = 0, hi = 1e17, mid; long long bestN = -1; long double bestDif = 1e9; while (lo <= hi) { mid = (lo + hi) / 2; long double tmp = ((mid + 1) * h + mid * c) / (2 * (mid + 1) - 1); long double error = abs(tmp - t); if (tmp > t) { lo = mid + 1; } else { hi = mid - 1; } if (error < bestDif) { bestN = mid; bestDif = error; } else if (error == bestDif) { if (mid < bestN) { bestN = mid; } } } long long ans = (2 * (bestN + 1)) - 1; double avg = (h + c) / 2; if (abs(avg - t) <= bestDif && abs(h - t) > bestDif) { return 2; } if (abs(h - t) <= bestDif) { return 1; } return ans; } long long solve() { if (t >= h) return 1; double avg = (h + c) / 2; if (t == avg) return 2; if (t < avg) { return 2; } return bs(); } int main() { int TC; cin >> TC; while (TC--) { cin >> h >> c >> t; long long ans = solve(); cout << ans << "\n"; } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
'''Author- Akshit Monga''' from fractions import Fraction q=int(input()) for _ in range(q): h,c,t=map(int,input().split()) if 2*t<=(h+c): print(2) else: x1 = (c - t) // (h + c - 2 * t) x2=x1+1 if abs(t-Fraction((x1*h+(x1-1)*c),(2*x1-1))) <= abs(t-Fraction((x2*h+(x2-1)*c),(2*x2-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 solve(th, tc, t): ans = 0 if th == t: ans = 1 elif 2*t <= th + tc: ans = 2 else: # halfsum = (th + tc)/2 # halfdelta = (th - tc)/2 # k = halfdelta/(t - halfsum) k = (th - tc)/(2*t - th - tc) if (th - tc)%(2*t - th - tc) == 0: if k%2 == 0: ans = int(k)+1 else:#k%2 == 1 ans = int(k) else: k_int = int(k) if k_int%2 == 1: i_candidate = k_int else: i_candidate = k_int - 1 if (1/k - 1/(i_candidate + 2) + 0.000000000000001) > (1/i_candidate - 1/k): ans = i_candidate else: ans = i_candidate + 2 # j = 1 # i = 2*j + 1 # current_t = halfsum + halfdelta/i # prev_t = th # while current_t > t: # prev_t = current_t # j += 1 # i = 2*j + 1 # current_t = halfsum + halfdelta/i # if abs(current_t - t) + 0.000000000000001 > abs(prev_t - t): # ans = i-2 # else: # ans = i return ans # fin = open('input.txt', 'r') # fout = open('output.txt', 'w') AA = int(input()) # AA = 1 # AA = int(fin.readline()) for ___ in range(AA): # th, tc, t = (int(k) for k in fin.readline().split(' ')) th, tc, t = (int(k) for k in input().split(' ')) answer = solve(th, tc, t) # fout.write(str(answer) + '\n') print(answer) # fin.close() # fout.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
#include <bits/stdc++.h> using namespace std; int checktemp(double temp1, double temp2, double t) { if (abs(temp1 - t) > abs(temp2 - t)) return 1; else return 0; return 0; } int main() { int w; cin >> w; while (w--) { float h, c, t; cin >> h >> c >> t; if (t <= (h + c) / 2) { cout << 2 << endl; } else { float n = (t - c) / (2 * t - h - c); int ans = floor(n); if (abs((h * ans + c * (ans - 1)) / (2 * ans - 1) - t) > abs((h * (ans + 1) + c * (ans)) / (2 * ans + 1) - t)) ans++; cout << (2 * ans - 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
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class c { 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; } } // long start = System.currentTimeMillis(); // long end = System.currentTimeMillis(); // System.out.println((end - start) + "ms"); public static void main(String[] args) { FastReader s=new FastReader(); int t = s.nextInt(); for(int tt=0;tt<t;tt++){ int h = s.nextInt(); int c = s.nextInt(); int temp = s.nextInt(); System.out.println(min_steps(h,c,temp)); } } public static int min_steps(double h,double c,double t){ if(h+c==2*t){ return 2; } if(h==c){ return 1; } else { double mean =(Math.floor((t-h)*1.0/(h+c-2*t))); // System.out.println(mean); if(mean>=0){ double max1 = (double) ((double)(h*(mean+1)+c*(mean))*1.0); // System.out.println(max1); double max2 = (double) ((double) (h*(mean+2)+c*(mean+1))); // System.out.println(max2); if(Math.abs(max1-t*(2*mean+1))*(double) (2*mean+3)<=(Math.abs(max2-t*(2*mean+3)))*(double) (2*mean+1) ){ // System.out.println(Math.abs(max1-t)); // System.out.println(Math.abs(max2-t)); return (int) (2*mean+1); } return (int) (2*mean+3); } else { return 2; } } } public static void debug(long[][] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.println(Arrays.toString(arr[i])); } } public static void debug(int[][] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.println(Arrays.toString(arr[i])); } } public static void debug(String[] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.println(arr[i]); } } public static void print(int[] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.print(arr[i]+" "); } System.out.print('\n'); } public static void print(String[] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.print(arr[i]+" "); } System.out.print('\n'); } public static void print(long[] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.print(arr[i]+" "); } System.out.print('\n'); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.*; import java.io.*; public class Main { static FastReader in = new FastReader(); public static void main(String[] args) { int t = in.nextInt(); while (t-- > 0) solve(); } static void solve() { int h = in.nextInt(), c = in.nextInt(), t = in.nextInt(), k; if (h == t) System.out.println(1); else if (h + c >= 2 * t) System.out.println(2); else { k = (h - t) / (2 * t - h - c); long l = Math.abs((k * (h + c) + h) - t * (2 * k + 1)); long ld = 2 * k + 1; long rd = 2 * k + 3; long r = Math.abs(((k + 1) * (h + c) + h) - t * (2 * k + 3)); if (l * rd <= r * ld) { System.out.println(2 * k + 1); } else { System.out.println(2 * k + 3); } } } static 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()); } } }
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.IOException; import java.io.InputStream; import java.util.*; public class Solution { static long h,c,te; static long binary(){ long l=1,r=(long)1e7; while(l<=r){ if(r-l<5){ double min=h-te; long ans=1; for(long i=l;i<=r;i++){ double z=Math.abs(calc(i)); if(z/(2*i+1)<min){ min=z/(2*i+1); ans=2*i+1; } } return ans; } long m=(l+r)/2; if(calc(m)>0) r=m; else l=m; } return 1/0; } static long calc(long k){ long time=2*k+1; long temp=h*(k+1) + c*k; //System.out.println(k+" "+temp); return te*time-temp; } public static void main(String args[]) throws IOException { FastReader in = new FastReader(System.in); StringBuilder sb = new StringBuilder(); int i, j; boolean testcase=true; int t=testcase?in.nextInt():1; while(t-->0){ h=in.nextInt(); c=in.nextInt(); te=in.nextInt(); if(te==h){ sb.append("1\n"); } else if(te<=(h+c)/2) sb.append("2\n"); else{ sb.append(binary()).append("\n"); } } System.out.print(sb); } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; void mycode(); const long long int mod = 998244353; long long int ceil(long long int a, long long int b) { return (a + b - 1) / b; } long long int min(long long int a, long long int b) { if (a > b) return b; else return a; } bool bit_check(long long int a, int i) { if ((a & ((long long)1 << (i - 1)))) return 1; return 0; } long long int bit_toggle(long long int a, int i) { return (a ^ ((long long)1 << (i - 1))); } long long int bit_sum(long long int a, int i) { return a + ((long long)1 << (i - 1)); } long long int bit_sub(long long int a, int i) { return a - ((long long)1 << (i - 1)); } long long int mod_power(long long int x, long long int y) { long long int p = mod; long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int power_of(long long int a, int b) { if (a == 0) return -1; return 1 + power_of(a / b, b); } long long int power(long long int a, int b) { long long int c = 1; if (b > 0) while (b--) c = c * a; return c; } double T, c, h; double fx(long long int x) { if (x & 1) { } else return 999999999; double xx = (x - 1.0) / 2.0; return abs(T - (h + c * xx + h * xx) / (2.0 * xx + 1.0)); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; mycode(); return 0; } void mycode() { int t, f = 0; cin >> t; bool flag = 0; while (t--) { f++; cin >> h >> c >> T; double value = (1LL << 60) + 5; long long int mi, k = 30; long long int left = 1, right = (1LL << 31), shift = (1LL << 30); upside: for (long long int i = left; i <= right; i += shift) { if (value > fx(i)) { mi = i; value = fx(i); } else if (i < mi && value + 0.0000001 > fx(i)) { mi = i; value = fx(i); } } if (shift != 1) { left = max(mi - shift, left); right = min(mi + shift, right); shift = (1LL << k); k--; goto upside; } if (abs(T - (h + c + 0.0) / 2.0) < value) { mi = 2; } cout << mi << '\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
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)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; long long dx[] = {0, -1, 0, 1, 0, 0}, dy[] = {-1, 0, 1, 0, 0, 0}, dz[] = {0, 0, 0, 0, -1, 1}; const long long N = 1e5 + 7; long long n, m, p; inline long long read() { long long first = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { first = (first << 1) + (first << 3) + (ch ^ 48); ch = getchar(); } return first * f; } long long h, c, t; inline double sum(long long first) { return fabs(t - (h * 1.000 * (first / 2 + 1) + c * 1.000 * (first / 2)) / first * 1.000); } inline void solve() { cin >> h >> c >> t; if (h == t) cout << 1 << endl; else if ((h + c) / 2 >= t) cout << 2 << endl; else if (t % ((h + c) / 2) == 0) cout << t / (h + c) << endl; else { long long num = (c - h) / (h + c - 2 * t); long long ans = 0; double mi = 0x3f3f3f3f; for (long long i = num - 10; i <= num + 10; i++) { if (i % 2 == 0 || i <= 0) continue; double temp = sum(i); if (mi > temp) mi = temp, ans = i; } cout << ans << endl; } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long _; cin >> _; while (_--) 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.InputStreamReader; import java.util.StringTokenizer; public class Prob3 { public static void main(String[] args) { FastReader fr = new FastReader(); int q = fr.nextInt(); for (int o=0; o<q; o++) { int t1 = fr.nextInt(); int t2 = fr.nextInt(); int t = fr.nextInt(); if (t1 <= t) { System.out.println(1); continue; } if (t1 + t2 >= 2*t) { System.out.println(2); continue; } int x1 = (int)(t - (float)t1)/(t1 + t2 - 2*t); int x = x1; float curMad = Math.abs((x * (t1 + t2) + t1 - t*(2*x+1) )*(2*x +3)); float nextMad = Math.abs(((x+1) * (t1 + t2) + t1 - t*(2*x+3))*(2*x+1)); if (nextMad < curMad) { x++; } System.out.println((2*x + 1)); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import java.util.HashSet; import java.util.Scanner; import java.util.Set; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.text.DecimalFormat; import java.lang.Math; import java.util.Iterator; public class C25{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while(n-->0){ int h = sc.nextInt(); int c = sc.nextInt(); int t = sc.nextInt(); if(t<=(h+c)/2){ System.out.println(2); continue; } int k = (int)Math.floor((double)(h-t)/(2*t-h-c)); long v1 = Math.abs((k*(h+c)+h)-t*(2*k+1))*(2*k+3); long v2 = Math.abs(((k+1)*(h+c)+h)-t*(2*k+3))*(2*k+1); if(v1 <= v2){ System.out.println(2*k+1); } else{ System.out.println(2*(k+1)+1); } } } //////////////////////////////////////////////////////////////////////////////// static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub if(this.x==o.x){ return this.y-o.y; } return this.x-o.x; } } public static boolean isPrime(int n){ if(n < 2){ return false; } if(n%2==0){ return n==2; } if(n%3==0){ return n==3; } int i = 5; int h = (int)Math.floor(Math.sqrt(n)+1); while(i <= h){ if(n%i==0){ return false; } if(n%(i+2)==0){ return false; } i += 6; } return true; } public static long gcd(long a, long b){ return b==0? a:gcd(b, a%b); } public static long bSearch(int n,ArrayList<Long> A){ int s = 0; int e = A.size()-1; while(s<=e){ int m = s+(e-s)/2; if(A.get(m)==(long)n){ return A.get(m); } else if(A.get(m)>(long)n){ e = m-1; } else{ s = m+1; } } return A.get(s); } static class Point implements Comparable<Point>{ int x; int y; public Point(int x, int y){ this.x = x; this.y = y; } @Override public int compareTo(Point o) { // TODO Auto-generated method stub if(this.x==o.x){ return this.y-o.y; } return this.x-o.x; } } public static long[] fact; public static final long modulo = 1000000007; public static long modinv(long x){ return modpow(x, modulo-2); } public static long modpow(long a, long b){ if(b==0){ return 1; } long x = modpow(a, b/2); x = (x*x)%modulo; if(b%2==1){ return (x*a)%modulo; } return 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
#include <bits/stdc++.h> using namespace std; void solve() { long long h, c, t; cin >> h >> c >> t; if (h == t) { cout << 1 << '\n'; return; } if (h + c >= t * 2) { cout << 2 << '\n'; return; } long long lo = 0, hi = 1e7; while (lo < hi) { int m = (lo + hi + 1) / 2; if (m * (h + c) + h >= (2 * m + 1) * t) { lo = m; } else { hi = m - 1; } } hi = lo + 1; if ((lo * (h + c) + h - (2 * lo + 1) * t) * (2 * hi + 1) > ((2 * hi + 1) * t - (hi * (h + c) + h)) * (2 * lo + 1)) { cout << 2 * hi + 1 << '\n'; } else { cout << 2 * lo + 1 << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int _n; cin >> _n; while (_n--) { 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 sys import stdin input=lambda : stdin.readline().strip() from math import ceil,sqrt,factorial,gcd for _ in range(int(input())): h,c,t=map(int,input().split()) m=abs(t-(h+c)/2) ans=2 if m>abs(t-0): ans=0 if 2*t-h-c!=0: z=(h-t)/(2*t-h-c) b=abs(t-(ceil(z)*(h+c)+h)/(2*ceil(z)+1)) a=abs(t-(int(z)*(h+c)+h)/(2*int(z)+1)) if a==b: if m>a: if 0.5>z-int(z): ans=2*(int(z))+1 else: ans=2*ceil(z)+1 else: s=min(a,b) if m>a: ans=2*(int(z))+1 m=min(a,m) if m>b: ans=2*ceil(z)+1 if ans<0: 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
#include <bits/stdc++.h> using namespace std; double mi, kq, h, c, t, q; void xuly(long long x) { if (x < 0) return; double tg = abs(t - (x * (h + c) + h) * 1.0 / (2 * x + 1)); if (mi > tg || (mi == tg && kq > x * 2 + 1)) { mi = tg; kq = x * 2 + 1; } } int main() { cin >> q; while (q--) { cin >> h >> c >> t; mi = abs(t - (h + c) * 1.0 / 2); kq = 2; long long l = 0, r = round(1e6); while (l < r) { long long mid = (l + r) >> 1; if ((mid * (h + c) + h) <= (2 * mid + 1) * t) r = mid; else l = mid + 1; } xuly(l); xuly(l - 1); cout << kq << '\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 tt = int(input()) for test in range(tt): h,c,t = map(int,input().split()) if t<=(h+c)/2: print(2) else: k = (h-t)//(2*t-h-c) if abs((k*(h+c)+h)-t*(2*k+1))*(2*k+3)<=abs(((k+1)*(h+c)+h)-t*(2*k+3))*(2*k+1): print(2*k+1) else: print(2*k+3)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys import math import heapq import collections def inputnum(): return(int(input())) def inputnums(): return(map(int,input().split())) def inputlist(): return(list(map(int,input().split()))) def inputstring(): return([x for x in input()]) def inputmatrix(rows): arr2d = [[j for j in input().strip()] for i in range(rows)] return arr2d t=int(input()) for q in range(t): h, c, t = inputnums() if t == h: print(1) elif t <= (h+c)/2: print(2) else: x = h - (h+c)/2 y = t - (h+c)/2 a = math.ceil(x/y) b = math.floor(x/y); if a%2 == 0: a += 1 if b%2 == 0: b -= 1 if abs(x/a - y) < abs(x/b - y): print(a) else: print(b)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys from collections import defaultdict import math input = sys.stdin.readline def print_result(l): return print(' '.join(map(str, l))) def timer(function): import time def wrapper(*args, **kwargs): t1 = time.time() val = function() t2 = time.time() print("Executed \{}\ in {:.2f} seconds.".format(function.__name__, t2-t1)) return val return wrapper def string_to_arr(s): return [x for x in s] # @timer def solve(): h, c, t = map(int, input().split(' ')) if 2 * t <= h + c: return '2' x = int((t - h)/(h + c - 2 * t)) a, b = x, x + 1 tt = t * (2 * a + 1) * ( 2 * b + 1) aa = abs(tt - (a * (c + h) + h) * (2 * b + 1)) bb = abs(tt - (b * (c + h) + h) * (2 * a + 1)) if aa <= bb: return str(2 * a + 1) return str(2 * b + 1) if __name__ == '__main__': t = int(input()) l = [] for _ in range(t): l.append(solve()) print('\n'.join(l))
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t 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 = list(map(int, input().split())) av = (h+c)/2 if h == c: print(1) elif t <= av: print(2) else: d = (t-av)/(h-av) # sk = 1 # while 1/sk >= d: # sk += 2 # print("sk is {}".format(sk)) k = -(-(h-av)//(t-av)) if k == (h-av)/(t-av): k += 1 if k % 2 == 0: k += 1 k = int(k) # print("s is {}".format(k)) av_max_k = (h-av)/k av_min_k = (h-av)/(k-2) d_max_k = abs(t-av-av_max_k) d_min_k = abs(t-av-av_min_k) # print("av_max_k is {} av_min_k is {}".format(av_max_k, av_min_k)) # print("d_max_k is {} d_min_k is {}".format(d_max_k, d_min_k)) res = k-2 if d_min_k <= d_max_k else k print(res)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math t = int(input()) for x in range(t): [h,c,t]=list(map(int,input().split())) if t <= (h+c)/2: print(2) else: y = (t-c)/(2*t-h-c) #print(y) y1 = int(y) y2 = y1 + 1 #print(2*y1-1) #print(2*y2-1) z1 = abs((h*y1+c*(y1-1))*(2*y2-1)-t*(2*y1-1)*(2*y2-1)) z2 = abs((h*y2+c*(y2-1))*(2*y1-1)-t*(2*y1-1)*(2*y2-1)) #print(z1, z2) #print((h*y1+c*(y1-1))*(2*y2-1)-(h*y2+c*(y2-1))*(2*y1-1)) if z1 > z2: print(2*y2-1) else: print(2*y1-1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys import math import collections def set_debug(debug_mode=False): if debug_mode: fin = open('input.txt', 'r') sys.stdin = fin def bs(m): return -t * (2 * m - 1) + (m * h + (m-1) * c) if __name__ == '__main__': # set_debug(True) t = int(input()) for ti in range(1, t + 1): h, c, t = list(map(int, input().split())) if t >= h: print(1) elif t <= (h + c) / 2: print(2) else: l, r = 2, 10 ** 12 find = False while l < r: m = (l + r) // 2 b = bs(m) if b > 0: l = m + 1 elif b < 0: r = m elif b == 0: print(2 * m - 1) find = True break if find: continue a1 = bs(l) / (2 * l - 1) a2 = bs(l-1) / (2 * l - 3) if abs(a2) <= abs(a1): print((l - 1) * 2 - 1) else: print(l * 2 - 1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.File; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.FileReader; 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; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); CMixingWater solver = new CMixingWater(); solver.solve(1, in, out); out.close(); } static class CMixingWater { public void solve(int testNumber, FastScanner in, PrintWriter out) { int T = in.nextInt(); while (T-- > 0) { int h = in.nextInt(); int c = in.nextInt(); int t = in.nextInt(); if (h == t) { out.println(1); continue; } if (c + h == 2 * t) { out.println(2); continue; } long cups = (t - h) / (c + h - 2 * t) + 2; long ans = 2; long oldNum = h + c; long oldDen = 2; int i = 0; while (i < 10 && cups > 0) { i++; long newDen = 2 * cups - 1; long newNum = cups * h + (cups - 1) * c; long partA = Math.abs(t * oldDen - oldNum) * newDen; long partB = Math.abs(t * newDen - newNum) * oldDen; if (partA > partB || (partA == partB && cups < ans)) { ans = cups + cups - 1; oldDen = newDen; oldNum = newNum; } cups--; } out.println(ans); } } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } public String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.StringTokenizer; public class Main { public static Scanner cin = new Scanner(System.in); public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st = null; // List<List<Long>> list; long MOD = 998244353L; static long MAX = (long)1e12; int n, m; int a[], b[]; List<AC> ans = null; int c; StringBuffer sb = new StringBuffer(); long ax, ay; class AC { int x, y; public AC(int i, int j) { x = i; y = j; } }; public int wf(int m) { int ans = 0; int k = 0; for (int i = 0; i < n; i++) { if (a[i] > m) { k = 0; continue; } if (k < 0) k = 0; k += a[i]; if (k > ans) ans = k; } return ans - m; } public void testShow() { for (int i = 1; i <= n; i++) { System.out.printf("%d ", a[i]); } System.out.println(); } public void solve() throws Exception { long h = cin.nextLong(); long c = cin.nextLong(); long t = cin.nextLong(); // (h+c)/2 = t // (ah+ac+h)/(2a+1) = t // ah+ac+h=2at+t // a(h+c-2t)=t-h // a = (t - h) / (h+c-2t) long ans = 2; ax = (h + c - 2 * t); if (ax < 0) ax *= -1; ay = 2; if (ax == 0) { System.out.println(2); return; } if (2 * (h - t) < ax) { ax = h - t; ay = 1; ans = 1; } long a = (t - h) / (h + c - 2 * t); if (a < 0) { a = 1; } long x = (a * h + a * c + h - (2 * a + 1) * t); long y = (2 * a + 1); if (x < 0) x *= -1; if (ax * y > ay * x) { ax = x; ay = y; ans = 2 * a + 1; } a++; x = (a * h + a * c + h - (2 * a + 1) * t); y = (2 * a + 1); if (x < 0) x *= -1; if (ax * y > ay * x) { ax = x; ay = y; ans = 2 * a + 1; } System.out.println(ans); } class GCDOBJ { long x, y; } int gcd(int x, int y) { if (x % y == 0) { return y; } return gcd(y, x % y); } long extended_gcd(long a, long b, GCDOBJ obj) { long ret, tmp; if (b == 0) { obj.x = 1; obj.y = 0; return a; } ret = extended_gcd(b, a % b, obj); tmp = obj.x; obj.x = obj.y; obj.y = tmp - a / b * obj.y; return ret; } long getRs(long a, long b, long n) { GCDOBJ obj = new GCDOBJ(); long d = extended_gcd(a, n, obj); long x0 = obj.x * (b / d) % n; if (x0 < 0) { x0 += n; } return x0; } public static void main(String [] args) { Main t = new Main(); int tc = cin.nextInt(); try { while (tc-- > 0) { // while (cin.hasNext()) { t.solve(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } t.pw.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; const long long mod = 1e9 + 7; long long dx[] = {-1, 0, 1, 0}; long long dy[] = {0, -1, 0, 1}; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long q; cin >> q; while (q--) { long long h, c, t; cin >> h >> c >> t; if (t == h) { cout << 1 << "\n"; continue; } else if (t * 2.0 <= (h + c)) { cout << 2 << "\n"; continue; } long long s = 0, e = 1e6, ans = 1; long double val = 1000000000; while (s != e) { long long mid = (s + e + 1) / 2; long long x = h * (mid + 1) + c * mid; long double p = x; p /= (2 * mid + 1); if (p <= t) { e = mid - 1; } else { s = mid; } } for (long long i = s; i < s + 3; i++) { long long x = h * (i + 1) + c * i; long double p = x; p /= (2 * i + 1); if (abs(p - t) < val) { val = abs(p - t); ans = i; } } cout << 2 * ans + 1 << "\n"; } }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys,math,random input=sys.stdin.readline import sys,math,random input=sys.stdin.readline T=int(input()) for _ in range(T): n,m,t=map(int,input().split()) diff=abs(t-((n+m)/2)) fv=2 if (t==n): print(1) elif (2*t==(n+m)): print(2) else: val=(n-t)/((2*t)-(n+m)) if (val<0): print(2) else: x=math.floor(val) y=math.ceil(val) if (abs((n+x*(n+m))-(t*((2*x)+1)))*((2*y)+1)>abs((n+y*(n+m))-(t*((2*y)+1)))*((2*x)+1)): fv=y else: fv=x print((2*fv)+1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h,c,t = map(int,input().split()) if h+c-2*t >= 0: print(2) else: a = h - t b = 2*t - c - h k = 2 * (a // b) + 1 val1 = abs(k // 2 * 1 * c + (k + 1) // 2 * 1 * h - t * 1 * k) val2 = abs((k + 2) // 2 * 1 * c + (k + 3) // 2 * 1 * h - t * 1 * (k + 2)) if val1 * (k + 2) <= val2 * k: print(k) else: print(k + 2)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; long long h, c, t, lo, hi; long double cal(long long mid) { long double a = 1.0 * h * (mid + 1) + 1.0 * c * mid; a /= 2.0 * mid + 1; return a; } int main() { int tc; cin >> tc; while (tc--) { cin >> h >> c >> t; if (h == t) cout << 1 << '\n'; else if (2 * t <= h + c) cout << 2 << '\n'; else { lo = 0; hi = 1e12 + 1; while (lo + 1 < hi) { long long mid = (lo + hi) / 2; if (1.0 * t > cal(mid)) hi = mid; else lo = mid; } if (cal(lo) - 1.0 * t > 1.0 * t - cal(hi)) { cout << 2 * hi + 1 << '\n'; } else cout << 2 * lo + 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 fractions f=fractions.Fraction tests=int(input()) for _ in range(tests): h,c,t=map(int,input().split()) av1=(h+c)/2 if (h+c)/2 == t: print(2) else: x=(t-h)/(h+c-2*t) if x<0: if abs(h-t)<=abs(t-av1): print(1) continue else: print(2) continue x=int(x) av2=f((x+1)*h+x*c,(2*x+1)) av3=f(((x+2)*h+(x+1)*c),(2*(x+1)+1)) if abs(av3-t)<abs(av2 -t): x=x+1 av2=av3 if abs(t-av1) > abs(t-av2): print(2*x+1) elif abs(t-av1) == abs(t-av2): print(min(2,2*x+1)) else: print(2)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import io import os import math def solve(H, C, T): # Need big decimal for more floating point resolution but TLEs assert C < H assert C <= T <= H if H == T: return 1 # After even (2 * i) cups, average is always (H + C) / 2 # After odd (2 * i + 1) cups, average is ((i + 1) * H + i * C) / (2 * i + 1) # This is monotonically decreasing from H inclusive to (H + C) / 2 exclusive # So if T is less than or equal to (H + C) / 2 it's definitely 2 cups if H + C >= 2 * T: return 2 # Odd case # Solve the odd case for floating `i` we get index = (T - H) / (C + H - 2 * T) # The floor and ceil of index should be the closest temp i = int(index) j = i + 1 if False: def temp(i): # Temperature after 2 * i + 1 cups return ((i + 1) * H + i * C) / (2 * i + 1) assert temp(math.floor(index)) >= T >= temp(math.ceil(index)) # Get closest diff1Num = ((i + 1) * H + i * C) - (2 * i + 1) * T diff1Denom = 2 * i + 1 diff2Num = ((j + 1) * H + j * C) - (2 * j + 1) * T diff2Num *= -1 diff2Denom = 2 * j + 1 assert diff1Num >= 0 assert diff2Num >= 0 if diff1Num * diff2Denom <= diff2Num * diff1Denom: return 2 * i + 1 else: return 2 * j + 1 if __name__ == "__main__": input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline TC = int(input()) for tc in range(1, TC + 1): H, C, T = [int(x) for x in input().split()] ans = solve(H, C, T) print(ans)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=input.nextInt(); while(T-->0) { long h,c,t; h=input.nextInt(); c=input.nextInt(); t=input.nextInt(); long m=(h+c)/2; if(t<=m) { out.println(2); } else { long x=(t-c)/(2*t-h-c); long y=x+1; if(Math.abs(((h*x)+c*(x-1)-t*(2*x-1))*(2*y-1))<=Math.abs(((h*y)+c*(y-1)-t*(2*y-1))*(2*x-1))) { out.println(2*x-1); } else { out.println(2*y-1); } } } 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
T = int(input()) for _ in range(T): h, c, t = map(int, input().split()) if h <= t: print(1) elif t <= (h + c) // 2: print(2) else: i1 = (t - h) // (h + c - 2 * t) i2 = i1 + 1 if ((i1+1) * h + i1 * c) * (2*i2 + 1) + ((i2+1) * h + i2 * c) * (2*i1 + 1) <= 2 * t * (2*i1 + 1) * (2*i2 + 1): ans = 2 * i1 + 1 else: ans = 2 * i2 + 1 print(ans)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); double t, c, h; long long x, y; cin >> y; for (long long i = 0; i < y; i++) { cin >> h >> c >> t; x = 1 / (1 - 2 * (t - h) / (c - h)); if (x < 0 || (c + h) == 2 * t) { cout << 2 << endl; } else { double m, n; if (x % 2 == 0) { m = x - 1; n = x + 1; } else { m = x; n = x + 2; } double p, q, r; if ((h + c) / 2 > t) { r = (h + c) / 2 - t; } else { r = t - (h + c) / 2; } p = ((h + (c - h) * (1 - 1 / m) / 2) - t); q = (t - (h + (c - h) * (1 - 1 / n) / 2)); if (p > q) { cout << n << endl; } else { cout << m << endl; } } } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CF1359C { public static void main(String[] args) { FastReader input = new FastReader(); int t = input.nextInt(); while (t > 0){ long hot = input.nextLong(); long cold = input.nextLong(); long desired = input.nextLong(); if(hot == desired){ System.out.println(1); } else if((hot + cold) >= 2 * desired){ System.out.println(2); } else{ long x = ((desired - cold) / ((2 * desired) - hot - cold)); long y = x + 1; double lower = ((x * hot) + (x - 1) * cold) / (1.0 * (2 * x - 1)); double upper = ((y * hot) + (y - 1) * cold) / (1.0 * (2 * y - 1)); double d1 = Math.abs(desired * 1L * (2 * x - 1) - (x * (cold + hot) - cold)) / (2 * x - 1.0); double d2 = Math.abs(desired * 1L * (2 * y - 1) - (y * (cold + hot) - cold)) / (2 * y - 1.0); if(d1 <= d2){ System.out.println(2 * x - 1); } else { System.out.println(2 * y - 1); } } t--; } } 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.io.*; import java.util.*; public class Main extends PrintWriter { private void solve() { int T = sc.nextInt(); for(int tt = 1; tt <= T; tt++) { long h = sc.nextLong(); long c = sc.nextLong(); long t = sc.nextLong(); if(h+c >= 2L*t) { println(2L); continue; } long num0 = (h+c); long den0 = 2L; long x0 = 2L; long t1 = (t-h)/(c+h-2L*t); long t2 = t1+1L; long x1 = 2L*t1+1L; long x2 = 2L*t2+1L; long num1 = ((t1+1)*h + t1*c); long den1 = (2L*t1+1L); long num2 = ((t2+1)*h + t2*c); long den2 = (2L*t2+1L); long num = num0; long den = den0; long x = x0; if(x1 > 0) { long comp = compare(t, num1, den1, num, den); if(comp < 0L || comp == 0L && x1 <= x) { num = num1; den = den1; x = x1; } } if(x2 > 0) { long comp = compare(t, num2, den2, num, den); if(comp < 0L || comp == 0L && x2 <= x) { num = num2; den = den2; x = x2; } } println(x);flush(); } } long compare(long t, long num1, long den1, long num2, long den2) { long t_ = den1*den2*t; return Math.abs(t_ - den2 * num1 ) - Math.abs(t_ - den1 * num2 ); } // Main() throws FileNotFoundException { super(new File("output.txt")); } // InputReader sc = new InputReader(new FileInputStream("test_input.txt")); Main() { super(System.out); } InputReader sc = new InputReader(System.in); static class InputReader { InputReader(InputStream in) { this.in = in; } InputStream in; private byte[] buf = new byte[16384]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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 int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] $) { new Thread(null, new Runnable() { public void run() { long start = System.nanoTime(); try {Main solution = new Main(); solution.solve(); solution.close();} catch (Exception e) {e.printStackTrace(); System.exit(1);} System.err.println((System.nanoTime()-start)/1E9); } }, "1", 1 << 27).start(); } }
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()) def solve(h, c, t, x): if (h*x + c*(x - 1)) > t * (2 * x - 1): return True else: return False for _ in range(T): h, c, t = [int(x) for x in input().split()] left = 1 right = 10**20 while right - left > 1: mid = (right + left) // 2 if solve(h, c, t, mid): left = mid else: right = mid # print(right, left) l = (h*left + c*(left - 1)) * (2 * right - 1) * 2 r = (h*right + c*(right - 1)) * (2 * left - 1) * 2 m = (h*1 + c*1) * (2 * left - 1) * (2 * right - 1) t *= 2 * (2*left - 1) * (2*right - 1) a = [abs(l - t), abs(r - t), abs(m - t)] #print(a) #print(l, r, m) #print(left, right) if min(a) == abs(t - m): print(2) elif min(a) == abs(l - t): print(2*left - 1) elif min(a) == abs(t - r): print(2*right - 1) 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
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.InputMismatchException; public class C1359 { static class Solver { long h, c, t; void solve(int testNumber, FastScanner s, PrintWriter out) { h = s.nextLong(); c = s.nextLong(); t = s.nextLong(); // At or below midpoint if(t <= (h + c) / 2) { out.println(2); return; } // One hot cup if(t == h) { out.println(1); return; } // We'll do 2k + 1 iterations // gives (h * (k + 1) + c * k) / (2k + 1) int lo = 1, hi = 2_000_000, m, f = -1; while(lo <= hi) { m = lo + hi >> 1; // if we use 2m + 1 pours, are we at or below t? long a = h * (m + 1) + c * m, b = 2 * m + 1; if(a <= b * t) { f = m; hi = m - 1; // try using fewer } else { lo = m + 1; // we need more iterations } } long an = t * (2 * f + 1) - h * (f + 1) - c * f; long ad = 2 * f + 1; long bn = h * f + c * (f - 1) - t * (2 * f - 1); long bd = 2 * f - 1; if(an * bd < bn * ad) out.println(2 * f + 1); else out.println(2 * f - 1); } } final static boolean cases = true; public static void main(String[] args) { FastScanner s = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); Solver solver = new Solver(); for (int t = 1, T = cases ? s.nextInt() : 1; t <= T; t++) solver.solve(t, s, out); out.close(); } static int min(int a, int b) { return a < b ? a : b; } static int max(int a, int b) { return a > b ? a : b; } static long min(long a, long b) { return a < b ? a : b; } static long max(long a, long b) { return a > b ? a : b; } static int swap(int a, int b) { return a; } static Object swap(Object a, Object b) { return a; } static String ts(Object... o) { return Arrays.deepToString(o); } static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } public FastScanner(File f) throws FileNotFoundException { this(new FileInputStream(f)); } public FastScanner(String s) { this.stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)); } 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++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } // Jacob Garbage public int[] nextIntArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = this.nextInt(); return ret; } public int[][] next2DIntArray(int N, int M) { int[][] ret = new int[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextIntArray(M); return ret; } public long[] nextLongArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = this.nextLong(); return ret; } public long[][] next2DLongArray(int N, int M) { long[][] ret = new long[N][]; for (int i = 0; i < N; i++) ret[i] = nextLongArray(M); return ret; } public double[] nextDoubleArray(int N) { double[] ret = new double[N]; for (int i = 0; i < N; i++) ret[i] = this.nextDouble(); return ret; } public double[][] next2DDoubleArray(int N, int M) { double[][] ret = new double[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextDoubleArray(M); return ret; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Random; import java.util.StringTokenizer; public class MixingWater { static final int MAXN = 1000_006; static final long MOD = (long) 1e9 + 7; public static void main(String[] args) throws IOException { MyScanner s = new MyScanner(); Print p = new Print(); int d = s.nextInt(); while (d-- > 0) { long h = s.nextLong(); long c = s.nextLong(); long t = s.nextLong(); int cups = 1; if (h + c - 2 * t >= 0) { p.println("2"); } else { long a = h - t; long b = 2 * t - c - h; long 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)); p.println(val1 * (k + 2) <= val2 * k ? k : k + 2); } // double tmp = h + c; // tmp /= (double)2; // double diff = Math.abs(d - tmp); // int ans = 2; // int st = 0; // int en = 1000000; // while (st <= en) { // int mid = st + (en - st) / 2; // int cups = 2 * mid + 1; // tmp = (mid + 1) * h + mid * c; // tmp /= (double) cups; // if (Math.abs(tmp - d) < diff) { // diff = Math.abs(tmp - d); // ans = cups; // } // if (tmp > d) { // st = mid + 1; // } else if (tmp < d) { // en = mid - 1; // } else { // ans = cups; // diff = 0; // en = mid - 1; // } // } // p.println(ans); } p.close(); } public static class Pair implements Comparable<Pair> { int first; int second; public Pair(int a, int b) { this.first = a; this.second = b; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + first; result = prime * result + second; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pair other = (Pair) obj; if (first != other.first) return false; if (second != other.second) return false; return true; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return o.first - first; } } public static class Helper { long MOD = (long) 1e9 + 7; int MAXN = 1000_006;; Random rnd; public Helper(long mod, int maxn) { MOD = mod; MAXN = maxn; rnd = new Random(); } public Helper() { } public static int[] sieve; public static ArrayList<Integer> primes; public void setSieve() { primes = new ArrayList<>(); sieve = new int[MAXN]; int i, j; for (i = 2; i < MAXN; ++i) if (sieve[i] == 0) { primes.add(i); for (j = i; j < MAXN; j += i) { sieve[j] = i; } } } public static long[] factorial; public void setFactorial() { factorial = new long[MAXN]; factorial[0] = 1; for (int i = 1; i < MAXN; ++i) factorial[i] = factorial[i - 1] * i % MOD; } public long getFactorial(int n) { if (factorial == null) setFactorial(); return factorial[n]; } public long ncr(int n, int r) { if (r > n) return 0; if (factorial == null) setFactorial(); long numerator = factorial[n]; long denominator = factorial[r] * factorial[n - r] % MOD; return numerator * pow(denominator, MOD - 2, MOD) % MOD; } public long[] getLongArray(int size, MyScanner s) throws Exception { long[] ar = new long[size]; for (int i = 0; i < size; ++i) ar[i] = s.nextLong(); return ar; } public int[] getIntArray(int size, MyScanner s) throws Exception { int[] ar = new int[size]; for (int i = 0; i < size; ++i) ar[i] = s.nextInt(); return ar; } public int[] getIntArray(String s) throws Exception { s = s.trim().replaceAll("\\s+", " "); String[] strs = s.split(" "); int[] arr = new int[strs.length]; for (int i = 0; i < strs.length; i++) { arr[i] = Integer.parseInt(strs[i]); } return arr; } public long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public long max(long[] ar) { long ret = ar[0]; for (long itr : ar) ret = Math.max(ret, itr); return ret; } public int max(int[] ar) { int ret = ar[0]; for (int itr : ar) ret = Math.max(ret, itr); return ret; } public long min(long[] ar) { long ret = ar[0]; for (long itr : ar) ret = Math.min(ret, itr); return ret; } public int min(int[] ar) { int ret = ar[0]; for (int itr : ar) ret = Math.min(ret, itr); return ret; } public long sum(long[] ar) { long sum = 0; for (long itr : ar) sum += itr; return sum; } public long sum(int[] ar) { long sum = 0; for (int itr : ar) sum += itr; return sum; } public long pow(long base, long exp, long MOD) { base %= MOD; long ret = 1; while (exp > 0) { if ((exp & 1) == 1) ret = ret * base % MOD; base = base * base % MOD; exp >>= 1; } return ret; } } static class Print { private BufferedWriter bw; public Print() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
for _ in range(int(input())): h,c,t = map(int,input().split()) if t<=(h+c)/2: print(2) else: x = (t-c)//(2*t-h-c) v1 = ((x)*h + (x-1)*c) v2 = ((x+1)*h + (x)*c) if abs(v1-(2*x-1)*t)/(2*x-1)<=abs(t*(2*x+1)-v2)/(2*x+1): print(2*x-1) else: print(2*x+1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.util.StringTokenizer; public class Main { FastScanner in; PrintWriter out; public static void main(String[] args) throws Exception { (new Main()).run(); } private void run() throws Exception { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } private void solve() throws Exception { int T = in.nextInt(); while (T-- > 0) { long h = in.nextInt(); long c = in.nextInt(); long t = in.nextInt(); long middle = h + c; h *= 2; c *= 2; t *= 2; if (t <= middle) { out.println(2); continue; } long d = h - middle; int l = 0; int r = 100_000_000; while (l + 1 < r) { int mid = (l + r) / 2; if ((t - middle) * (2 * mid + 1) <= d) { l = mid; } else { r = mid; } } out.println( d * (2 * r + 2 * l + 2) > 2 * (t - middle) * (2 * l + 1) * (2 * r + 1) ? 2 * r + 1 : 2 * l + 1 ); } } private class FastScanner { BufferedReader bufferedReader; StringTokenizer stringTokenizer; public FastScanner(InputStream inputStream) { this.bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); } public String next() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { String line = bufferedReader.readLine(); if (line == null) { throw new EOFException("End of input stream is reached."); } stringTokenizer = new StringTokenizer(line); } return stringTokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class SolutionD extends Thread { 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; } } private static final FastReader scanner = new FastReader(); private static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { new Thread(null, new SolutionD(), "Main", 1 << 26).start(); } public void run() { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { solve(); } out.close(); } private static void solve() { long h = scanner.nextInt(); long c = scanner.nextInt(); int t = scanner.nextInt(); int low = 0; int high = 1_000_000_001; int closestAmount = 2; double closestTempDiff = Integer.MAX_VALUE; if (t <= (c + h) / 2) { out.println(2); return; } while (high - low > 1) { int mid = (low + high) / 2; int amountHot = mid+1; int amountCold = mid; double temperature = (amountHot * h + amountCold * c) * 1.0 / (amountHot + amountCold); if (temperature < t) { high = mid; } else { low = mid; } } if (Math.abs(low * h + h + low * c - t * (long) (low * 2 + 1)) * ((low+1) * 2 + 1) <= Math.abs((low + 1) * h + h + (low+1) * c - t * (long) ((low+1) * 2 + 1)) * (low * 2 + 1)) { out.println(low * 2 + 1); } else { out.println((low + 1) * 2 + 1); } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from decimal import * getcontext().prec = 100 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
import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Scanner; public class cf1359_Div2_C { public static double solve(double mid, int h, int c) { return ((mid * h) + (mid - 1) * c) / (2 * mid - 1); } public static void main(String args[]) { Scanner in = new Scanner(System.in); int tc = in.nextInt(); for ( ; tc > 0; tc--) { int h = in.nextInt(); int c = in.nextInt(); int t = in.nextInt(); if (t >= h) System.out.println(1); else if (t <= (c + h) / 2) System.out.println(2); else { int l = 1; int hi = (int)(1e9); while (l < hi) { int mid = l + (hi - l + 1) / 2; double calc = solve(mid, h, c); // System.out.println(l + " " + hi + " " + calc + " " + mid); if (calc >= t) l = mid; else hi = mid - 1; } //System.out.println(l); int start = l; int num = Math.abs((((start * h) + (start - 1) * c) - (t * (2 * start - 1)))); int den = 2 * start - 1; for (int i = start; i <= start + 100; i++) { int numCalc = Math.abs(((i * h) + (i - 1) * c) - (t * (2 * i - 1))); int denCalc = (2 * i - 1); //System.out.println(i + " " +denCalc * num + " " + numCalc * den); //System.out.println(num + " " + den + " " + numCalc + " " + denCalc); if (denCalc * num - numCalc * den > 0) { l = i; num = numCalc; den = denCalc; } } // System.out.println(l); System.out.println(2 * l - 1); } } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; long long int MOD = 998244353; long long int mod = 1e9 + 7; long long int INF = 1e18; long long int dx[] = {1, -1, 0, 0}; long long int dy[] = {0, 0, 1, -1}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { double h, c, t; cin >> h >> c >> t; double av = (h + c) / 2; if (av >= t) cout << 2; else if (h <= t) cout << 1; else { double a = h - t; double b = 2 * t - (h + c); long long int d = a / b; long long int e = d + 1; long double ans1 = ((h + c) * d + h) / (2 * d + 1); long double ans2 = ((h + c) * e + h) / (2 * e + 1); long double error1 = abs(ans1 - t); long double error2 = abs(ans2 - t); if (error1 <= error2) cout << 2 * d + 1; else cout << 2 * e + 1; } cout << 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
cases = int(input()) for _ in range(cases) : hot, cold, target = map(int, input().split()) if 2 * target <= hot + cold : print(2) elif target >= hot : print(1) else : lo, hi = 1, 10**20 while lo < hi : mid = (lo+1+hi) // 2 if mid * hot + (mid-1) * cold >= (2*mid-1) * target : lo = mid else : hi = mid - 1 upper_bound = (lo * hot + (lo-1) * cold, lo + lo - 1) hi = lo + 1 lower_bound = (hi * hot + (hi-1) * cold, hi + hi - 1) ans = lo if abs( (upper_bound[0] - upper_bound[1] * target) * lower_bound[1] ) >\ abs( (lower_bound[0] - lower_bound[1] * target) * upper_bound[1] ) : ans = hi print(2 * ans - 1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys, math input = sys.stdin.readline def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input().strip() def listStr(): return list(input().strip()) import collections as col import math def solve(): H,C,T = getInts() if H in (C,T): return 1 if 2*T <= H+C: return 2 if T >= H: return 1 #so we know that T lies strictly between (H+C)/2 and (2*H+C)/3, and therefore at least 3 cups are required left = 1 right = 10**18-1 while right-left > 2: middle = (right+left)//2 if middle % 2 == 0: middle -= 1 curr_temp = (middle//2+1)*H+(middle//2)*C if middle*T == curr_temp: return middle if middle*T < curr_temp: left = middle else: right = middle #we now know that left cups is hotter, right cups is cooler #print(right,left) right_temp = (right//2+1)*H+(right//2)*C left_temp = (left//2+1)*H+(left//2)*C #print(left_temp-T*left) if abs(left*(right_temp-right*T)) < abs(right*(left_temp-left*T)): return right return left for _ in range(getInt()): print(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
for _ in range(int(input())): a,b,c=map(int,input().split()) mid=(a+b)/2 if c==a: print(1) elif c<=mid: print(2) elif c>=(mid+(a-b)/6) and c<a: d=(mid+(a-b)/6) if abs(d-c)<abs(a-c): print(3) else: print(1) else: c-=mid d=[] x=(a-b)//c for i in range(int(x)-4,int(x)+5): if i%2==0 and (i//2)%2!=0: d.append([abs((a-b)/i-c),i]) d.sort() print(d[0][1]//2)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
def cck(val, h, c, t): return (val+1)*h+val*c < t*(2*val+1) def cnt(val, h, c): return (val+1)*h+val*c ts = int(raw_input()) for cs in range(ts): h, c, t = tuple(map(int, raw_input().split())) if(2*t <= h+c): print 2 continue st, ed = 0, 1000000000000 while st+1 < ed: mid = (st+ed)/2 if cck(mid, h, c, t): ed = mid else: st = mid baam, daan = 0, 0 baam += cnt(ed-1, h, c)*(2*ed+1) daan += cnt(ed, h, c)*(2*ed-1) if baam+daan <= 2*t*(4*ed*ed-1): ed -= 1 print 2*ed+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
/* 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); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.util.*; import java.lang.*; public class Rextester{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int z = Integer.parseInt(br.readLine()); StringBuffer sb = new StringBuffer(); for(int i=1;i<=z;i++){ StringTokenizer st = new StringTokenizer(br.readLine()); long h = Long.parseLong(st.nextToken()); long c = Long.parseLong(st.nextToken()); long t = Long.parseLong(st.nextToken()); if(t<=(h+c)/2){ sb.append("2\n"); continue; } long d = (h-t)/(2*t-h-c); long e = d+1; long p = Math.abs(d*c + (d+1)*h - (2*d+1)*t)*(2*d+3); long q = Math.abs((d+1)*c + (d+2)*h - (2*d+3)*t)*(2*d+1); if(p<=q){ sb.append(2*d+1).append("\n"); } else{ sb.append(2*d+3).append("\n"); } } br.close(); System.out.println(sb); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.util.*; public class Main { static Set<Long> set = new TreeSet<>(); public static void main(String[] args) { Scanner s = new Scanner(System.in); int TC = s.nextInt(); tc: for (int tc = 0; tc < TC; tc++) { int h = s.nextInt(); int c = s.nextInt(); int t = s.nextInt(); double first = (h+c) / 2.0; if (t <= first ) { System.out.println(2); continue; } if (t == h) { System.out.println(1); continue ; } double u2 = first+ getTemp(2, h,c); if (u2 < t) { double gap1 = Math.abs(t-u2); double gap2 = Math.abs(t-h); if (gap2 <= gap1) { System.out.println(1); continue ; } else { System.out.println(3); continue ; } } double n = (((h-c) / (2* (t-first) ))+ 1)/2; int n1 = (int) Math.ceil(n); int n2 = (int) Math.floor(n); double gap1 = Math.abs((t- first) - getTemp(n1, h,c)); double gap2 = Math.abs((t-first) - getTemp(n2, h,c)); if (gap2 <= gap1) { System.out.println(2*n2 - 1); } else { System.out.println(2*n1-1); } } } public static double getTemp(long n, int h , int c) { double devider = (4*n - 2); return (h-c) / devider; } }
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+c>=2*t: print(2) elif t>=h: print(1) else: x=2*t-h-c k=(h-c+x)//(2*x) test1=abs((8*k**2-2)*t-(h*k+c*k-c)*(4*k+2)) test2=abs((8*k**2-2)*t-(h*(k+1)+c*(k+1)-c)*(4*k-2)) test3=abs((8*k**2-2)*t-(h+c)*(4*k**2-1)) if k==1: if min(test1,test2,test3)==test1: print(2*k-1) elif min(test1,test2,test3)==test3: print(2) else: print(2*k+1) else: if min(test1,test2,test3)==test3: print(2) elif min(test1,test2,test3)==test1: 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.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.List; import java.util.*; public class realfast implements Runnable { private static final int INF = (int) 1e9; long in= (long)Math.pow(10,9)+7; long fac[]= new long[3000]; public void solve() throws IOException { int t1 = readInt(); for(int f =0;f<t1;f++) { long h = readLong(); long c = readLong(); long t = readLong(); if(t>=h) out.println(1); else if(2*t<=(h+c)) { out.println(2); } else { long left =1; long right = 1000000000; long ans =1; long diff = Math.abs(h-t); long diff2 = Math.abs(h+c-2*t); if(diff2<2*diff) { diff2= h+c; ans=2; } while(left<=right) { long mid = left + (right-left)/2; long kl = mid*h+ (mid-1)*c; kl= kl - (2*mid-1)*t; kl= Math.abs(kl); long gal = kl *ans ; diff=Math.abs(diff); long pal = diff*(2*mid-1); if(gal<pal) { ans=2*mid-1; diff=kl; } else if(gal==pal) { if(2*mid-1<ans) { ans=2*mid-1; diff=kl; } } if(mid*h+(mid-1)*c<=(2*mid-1)*t) { right =mid-1; } else left = mid+1; //out.println(mid); } out.println(ans); } } } public int gcd(int a , int b ) { if(a<b) { int t =a; a=b; b=t; } if(a%b==0) return b ; return gcd(b,a%b); } public long pow(long n , long p,long m) { if(p==0) return 1; long val = pow(n,p/2,m);; val= (val*val)%m; if(p%2==0) return val; else return (val*n)%m; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { new Thread(null, new realfast(), "", 128 * (1L << 20)).start(); } private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; private BufferedReader reader; private StringTokenizer tokenizer; private PrintWriter out; @Override public void run() { try { if (ONLINE_JUDGE || !new File("input.txt").exists()) { reader = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { reader = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } solve(); } catch (IOException e) { throw new RuntimeException(e); } finally { try { reader.close(); } catch (IOException e) { // nothing } out.close(); } } private String readString() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } @SuppressWarnings("unused") private int readInt() throws IOException { return Integer.parseInt(readString()); } @SuppressWarnings("unused") private long readLong() throws IOException { return Long.parseLong(readString()); } @SuppressWarnings("unused") private double readDouble() throws IOException { return Double.parseDouble(readString()); } } class edge implements Comparable<edge>{ int val ; int color; edge(int u, int v) { this.val=u; this.color=v; } public int compareTo(edge e) { return this.val-e.val; } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
# https://codeforces.com/contest/1359/problem/C def calc(a, b, t, k): num1, num2 = abs(k*(a+b)+a - (2*k + 1)*t), abs((k+1)*(a+b) + a - (2*k + 3)*t) den1, den2=(2*k + 1), (2*k + 3) if num1*den2 <= num2*den1: return 2*k + 1 return 2*k + 3 t=int(input()) for _ in range(t): a, b, t=map(int, input().split()) if t <= (a+b)/2: print(2) continue k=(a - t)//(2*t - a - b) print(calc(a, b, t, k))
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
tt = int(input()) for _ in range(tt): h, c, t = list(map(int, input().split(' '))) if t>=h: print(1) elif t <= (c+h)/2: print(2) else: n = (t-c)/(2*t-h-c) m = int(n)+1 nn = int(n) e = (nn*h+(nn-1)*c) - t*(2*nn-1) ee = t*(2*m-1) - (m*h+(m-1)*c) if e*(2*m-1) > ee*(2*nn-1): print(2*m-1) else: print(2*nn-1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from fractions import Fraction def temp(hot, cold, step): return Fraction((hot * step + cold * (step - 1)), (2 * step - 1)) tests = int(input()) for i in range(tests): numbers = input().split(" ") h = int(numbers[0]) c = int(numbers[1]) t = int(numbers[2]) if t >= h: print(1) elif t <= (h + c) / 2: print(2) else: step0 = (((h - c) / (t - (h + c) / 2) + 2) / 4) step = int((((h - c) / (t - (h + c) / 2) + 2) / 4)) t_0 = temp(h, c, step) t_1 = temp(h, c, step + 1) if abs(t_0 - t) <= abs(t_1 - t): print(step * 2 - 1) else: print((step + 1) * 2 - 1)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from math import ceil, floor from decimal import Decimal def calculate(x, y, n): return Decimal(x * n + y * (n - 1)) / Decimal(2 * n - 1) def ca(x, y): return (x + y) / 2 for _ in range(int(input())): x, y, c = map(int, input().split()) ans1 = 2 if (x + y) / 2 >= c: print(ans1) continue ans2 = (floor((y - c) / (x + y - 2 * c))) ans3 = (ceil((y - c) / (x + y - 2 * c))) if ans2 == 0: ans2 = ans3 else: if abs(c - calculate(x, y, ans2)) > abs(c - calculate(x, y, ans3)): ans2 = ans3 if abs(ca(x, y) - c) > abs(c - calculate(x, y, ans2)): print(ans2 * 2 - 1) elif abs(calculate(x, y, ans2) - c) > abs(c - ca(x, y)): print(ans1) else: print(min(ans1, ans2 * 2 - 1))
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): h,c,temp = map(int,input().split()) ave_temp = (h+c)/2 if temp <= ave_temp: print(2) elif h == temp: print(1) else: u = (h-c)/((temp-ave_temp)*2) u_l = int(u) if int(u) % 2 == 1: u1 = u_l u2 = u_l + 2 else: u1 = u_l - 1 u2 = u_l + 1 if abs((temp - ave_temp) - (h - c) / (u1 * 2)) <= abs((temp - ave_temp) - (h - c) / (u2 * 2)): print(u1) else: print(u2)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t 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.BigInteger; import java.util.*; public class Main { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); static int MOD = 1000000007; public static void main(String[] args) throws IOException { Main m = new Main(); m.solve(); m.close(); } void close() throws IOException { pw.flush(); pw.close(); br.close(); } int readInt() throws IOException { return Integer.parseInt(br.readLine()); } long readLong() throws IOException { return Long.parseLong(br.readLine()); } int[] readIntLine() throws IOException { String[] tokens = br.readLine().split(" "); int[] A = new int[tokens.length]; for (int i = 0; i < A.length; i++) A[i] = Integer.parseInt(tokens[i]); return A; } long[] readLongLine() throws IOException { String[] tokens = br.readLine().split(" "); long[] A = new long[tokens.length]; for (int i = 0; i < A.length; i++) A[i] = Long.parseLong(tokens[i]); return A; } void solve() throws IOException { int T = readInt(); for (int Ti = 0; Ti < T; Ti++) { int[] hct = readIntLine(); int h = hct[0]; int c = hct[1]; int t = hct[2]; if (t == h) { pw.println("1"); continue; } if (2 * t <= c + h) { pw.println("2"); continue; } int X = (t - c) / (2 * t - c - h); Fraction bestDiff = new Fraction(Math.abs(h + c - 2 * t), 2); int cupsOfBest = 2; for (int x = Math.max(1, X-1); x <= X+1; x++) { Fraction diff = new Fraction(Math.abs(h * x + c * (x-1) - (2*x-1) * t), 2*x-1); if (diff.compareTo(bestDiff) < 0) { bestDiff = diff; cupsOfBest = 2*x-1; } } pw.println(cupsOfBest); } } } class Fraction implements Comparable<Fraction> { long n; long d; public Fraction(long n, long d) { if (n == 0) { this.d = 1; return; } int sign = n * d < 0 ? -1 : 1; n = Math.abs(n); d = Math.abs(d); long g = gcd(n, d); this.n = sign * n / g; this.d = d / g; } static Fraction add(Fraction f1, Fraction f2) { return new Fraction(f1.n*f2.d+f1.d*f2.n, f1.d*f2.d); } static Fraction subtract(Fraction f1, Fraction f2) { return new Fraction(f1.n*f2.d-f1.d*f2.n, f1.d*f2.d); } static Fraction multiply(Fraction f1, Fraction f2) { return new Fraction(f1.n*f2.n, f1.d*f2.d); } static Fraction divide(Fraction f1, Fraction f2) { return new Fraction(f1.n*f2.d, f1.d*f2.n); } public boolean equals(Object o) { if (!(o instanceof Fraction)) return false; Fraction f = (Fraction) o; return n == f.n && d == f.d; } public int compareTo(Fraction f) { return Long.compare(n*f.d, f.n*d); } public String toString() { return n + "/" + d; } private static long gcd(long a, long b) { return a == 0 ? b : gcd(b % a, a); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
def main(): from decimal import Decimal import sys input = sys.stdin.readline sys.setrecursionlimit(10**6) T = int(input()) for _ in range(T): h, c, t = [Decimal(x) for x in input().strip().split()] avg = (h + c) / 2 if h == t: print(1) continue elif avg >= t: print(2) continue l, r = Decimal(1), Decimal(10 ** 9) def calc(m): return Decimal((m * h + (m - 1) * c) / (2 * m - 1)) while l + 1 < r: m = Decimal((l + r ) // 2) if calc(m) >= t: l = m else: r = m if abs(calc(l) - t) <= abs(calc(r) - t): print(2 * l - 1) else: print(2 * r - 1) if __name__ == '__main__': main()
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; 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)); long long val1 = abs(k / 2 * c + (k + 1) / 2 * h - t * k); if (val1 * (k + 2) <= val2 * k) { cout << k << '\n'; } else cout << k + 2 << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) solve(); }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.util.*; import java.math.*; /** * Built using CHelper plug-in * Actual solution is at the top */ public class MixingWater { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(in.nextInt(), in, out); out.close(); } static class TaskA { long mod = (long)(1000000007); public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException { while(testNumber-->0){ long a = in.nextLong(); long b = in.nextLong(); long t = in.nextLong(); if((double)(a+b)/2 >= (double)t){ out.println(2); continue; } long[] n = find(a-t,2*t-a-b); // out.println(n); long x1 = (long)Math.floor((double)n[0]/n[1]); long x2 = (long)Math.ceil((double)n[0]/n[1]); long x = ans(t , a , b , x1 , x2 , out); // if(x1-1>=0){ // x = ans(t , a , b , x1-1 , x , out); // } out.println(2*x+1); } } //438837 375205 410506 public long ans(long t , long a , long b , long x1 , long x2 , PrintWriter out){ long fr1[] = find((x1+1)*a + x1*b , 2*x1+1); long fr2[] = find((x2+1)*a + x2*b , 2*x2+1); long fFr1[] = find(Math.abs(t*fr1[1]-fr1[0]) , fr1[1]); long fFr2[] = find(Math.abs(t*fr2[1]-fr2[0]) , fr2[1]); // System.out.println(x1 + " " + x2); // print1d(fFr1 , out); // print1d(fFr2 , out); long left = (long)fFr1[0]*fFr2[1]; long right = (long)fFr2[0]*fFr1[1]; // out.print if(left<=right) return x1; return x2; } public long[] find(long num , long deno){ long gcd = gcd(num , deno); num /= gcd; deno /= gcd; return new long[]{num , deno}; } public void factorise(int a[] , HashMap<Integer , Integer> m[]){ for(int i=0;i<a.length;i++){ m[i] = new HashMap<>(); int x = a[i]; for(int j=2;j*j<=x;j++){ if(x%j == 0){ int count = 0; while(x%j == 0){ x/=j; count++; } m[i].put(j , count); } } if(x!=1) m[i].put(x , 1); } } public void print1d(long a[] , PrintWriter out){ for(int i=0;i<a.length;i++) out.print(a[i] + " "); out.println(); } public void print2d(int a[][] , PrintWriter out){ for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++) out.print(a[i][j] + " "); out.println(); } out.println(); } public void sieve(int a[]){ a[0] = a[1] = 1; int i; for(i=2;i*i<=a.length;i++){ if(a[i] != 0) continue; a[i] = i; for(int k = (i)*(i);k<a.length;k+=i){ if(a[k] != 0) continue; a[k] = i; } } } public int [][] matrixExpo(int c[][] , int n){ int a[][] = new int[c.length][c[0].length]; int b[][] = new int[a.length][a[0].length]; for(int i=0;i<c.length;i++) for(int j=0;j<c[0].length;j++) a[i][j] = c[i][j]; for(int i=0;i<a.length;i++) b[i][i] = 1; while(n!=1){ if(n%2 == 1){ b = matrixMultiply(a , a); n--; } a = matrixMultiply(a , a); n/=2; } return matrixMultiply(a , b); } public int [][] matrixMultiply(int a[][] , int b[][]){ int r1 = a.length; int c1 = a[0].length; int c2 = b[0].length; int c[][] = new int[r1][c2]; for(int i=0;i<r1;i++){ for(int j=0;j<c2;j++){ for(int k=0;k<c1;k++) c[i][j] += a[i][k]*b[k][j]; } } return c; } public long nCrPFermet(int n , int r , long p){ if(r==0) return 1l; long fact[] = new long[n+1]; fact[0] = 1; for(int i=1;i<=n;i++) fact[i] = (i*fact[i-1])%p; long modInverseR = pow(fact[r] , p-2 , p); long modInverseNR = pow(fact[n-r] , p-2 , p); long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p; return w; } public long pow(long a, long b, long m) { a %= m; long res = 1; while (b > 0) { long x = b&1; if (x == 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } public long pow(long a, long b) { long res = 1; while (b > 0) { long x = b&1; if (x == 1) res = res * a; a = a * a; b >>= 1; } return res; } public void swap(int a[] , int p1 , int p2){ int x = a[p1]; a[p1] = a[p2]; a[p2] = x; } public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) { if (start > end) { return; } int mid = (start + end) / 2; a.add(mid); sortedArrayToBST(a, start, mid - 1); sortedArrayToBST(a, mid + 1, end); } class Combine{ int value; int delete; Combine(int val , int delete){ this.value = val; this.delete = delete; } } class Sort2 implements Comparator<Combine>{ public int compare(Combine a , Combine b){ if(a.value > b.value) return 1; else if(a.value == b.value && a.delete>b.delete) return 1; else if(a.value == b.value && a.delete == b.delete) return 0; return -1; } } public int lowerLastBound(ArrayList<Integer> a , int x){ int l = 0; int r = a.size()-1; if(a.get(l)>=x) return -1; if(a.get(r)<x) return r; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a.get(mid) == x && a.get(mid-1)<x) return mid-1; else if(a.get(mid)>=x) r = mid-1; else if(a.get(mid)<x && a.get(mid+1)>=x) return mid; else if(a.get(mid)<x && a.get(mid+1)<x) l = mid+1; } return mid; } public int upperFirstBound(ArrayList<Integer> a , Integer x){ int l = 0; int r = a.size()-1; if(a.get(l)>x) return l; if(a.get(r)<=x) return r+1; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a.get(mid) == x && a.get(mid+1)>x) return mid+1; else if(a.get(mid)<=x) l = mid+1; else if(a.get(mid)>x && a.get(mid-1)<=x) return mid; else if(a.get(mid)>x && a.get(mid-1)>x) r = mid-1; } return mid; } public int lowerLastBound(int a[] , int x){ int l = 0; int r = a.length-1; if(a[l]>=x) return -1; if(a[r]<x) return r; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a[mid] == x && a[mid-1]<x) return mid-1; else if(a[mid]>=x) r = mid-1; else if(a[mid]<x && a[mid+1]>=x) return mid; else if(a[mid]<x && a[mid+1]<x) l = mid+1; } return mid; } public int upperFirstBound(long a[] , long x){ int l = 0; int r = a.length-1; if(a[l]>x) return l; if(a[r]<=x) return r+1; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a[mid] == x && a[mid+1]>x) return mid+1; else if(a[mid]<=x) l = mid+1; else if(a[mid]>x && a[mid-1]<=x) return mid; else if(a[mid]>x && a[mid-1]>x) r = mid-1; } return mid; } public long log(float number , int base){ return (long) Math.floor((Math.log(number) / Math.log(base))); } public long gcd(long a , long b){ if(a<b){ long c = b; b = a; a = c; } while(b!=0){ long c = a; a = b; b = c%a; } return a; } public long[] gcdEx(long p, long q) { if (q == 0) return new long[] { p, 1, 0 }; long[] vals = gcdEx(q, p % q); long d = vals[0]; long a = vals[2]; long b = vals[1] - (p / q) * vals[2]; // 0->gcd 1->xValue 2->yValue return new long[] { d, a, b }; } public void sievePhi(int a[]){ a[0] = 0; a[1] = 1; for(int i=2;i<a.length;i++) a[i] = i-1; for(int i=2;i<a.length;i++) for(int j = 2*i;j<a.length;j+=i) a[j] -= a[i]; } public void lcmSum(long a[]){ int sievePhi[] = new int[(int)1e6 + 1]; sievePhi(sievePhi); a[0] = 0; for(int i=1;i<a.length;i++) for(int j = i;j<a.length;j+=i) a[j] += (long)i*sievePhi[i]; } static class AVLTree{ Node root; public AVLTree(){ this.root = null; } public int height(Node n){ return (n == null ? 0 : n.height); } public int getBalance(Node n){ return (n == null ? 0 : height(n.left) - height(n.right)); } public Node rotateRight(Node a){ Node b = a.left; Node br = b.right; a.lSum -= b.lSum; a.lCount -= b.lCount; b.rSum += a.rSum; b.rCount += a.rCount; b.right = a; a.left = br; a.height = 1 + Math.max(height(a.left) , height(a.right)); b.height = 1 + Math.max(height(b.left) , height(b.right)); return b; } public Node rotateLeft(Node a){ Node b = a.right; Node bl = b.left; a.rSum -= b.rSum; a.rCount -= b.rCount; b.lSum += a.lSum; b.lCount += a.lCount; b.left = a; a.right = bl; a.height = 1 + Math.max(height(a.left) , height(a.right)); b.height = 1 + Math.max(height(b.left) , height(b.right)); return b; } public Node insert(Node root , long value){ if(root == null){ return new Node(value); } if(value<=root.value){ root.lCount++; root.lSum += value; root.left = insert(root.left , value); } if(value>root.value){ root.rCount++; root.rSum += value; root.right = insert(root.right , value); } // updating the height of the root root.height = 1 + Math.max(height(root.left) , height(root.right)); int balance = getBalance(root); //ll if(balance>1 && value<=root.left.value) return rotateRight(root); //rr if(balance<-1 && value>root.right.value) return rotateLeft(root); //lr if(balance>1 && value>root.left.value){ root.left = rotateLeft(root.left); return rotateRight(root); } //rl if(balance<-1 && value<=root.right.value){ root.right = rotateRight(root.right); return rotateLeft(root); } return root; } public void insertElement(long value){ this.root = insert(root , value); } public int getElementLessThanK(long k){ int count = 0; Node temp = root; while(temp!=null){ if(temp.value == k){ if(temp.left == null || temp.left.value<k){ count += temp.lCount; return count-1; } else temp = temp.left; } else if(temp.value>k){ temp = temp.left; } else{ count += temp.lCount; temp = temp.right; } } return count; } public void inorder(Node root , PrintWriter out){ Node temp = root; if(temp!=null){ inorder(temp.left , out); out.println(temp.value + " " + temp.lCount + " " + temp.rCount); inorder(temp.right , out); } } } static class Node{ long value; long lCount , rCount; long lSum , rSum; Node left , right; int height; public Node(long value){ this.value = value; left = null; right = null; lCount = 1; rCount = 1; lSum = value; rSum = value; height = 1; } } } 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
# Nilfer 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 java.util.*; import java.io.*; public class MixingWater { public static void main(String[] args) { Scanner in = new Scanner(System.in); OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); int T; T=in.nextInt(); while((T--)>0) { //code comes here double hot=in.nextInt()+0.0; double cold=in.nextInt()+0.0; double req=in.nextInt()+0.0; double av=(hot+cold)/2; if(req<=av) { out.println(2); } else { if(req==hot) out.println(1); else { long low=0; long high=Integer.MAX_VALUE-1; while (low<high) { long mid=(low+high+1)/2; double val=((double)((hot+cold)*mid + hot))/((double) (2*mid+1)); if(val>=req) { low=mid; } else { high=mid-1; } //out.println(low+" "+ high+" "+mid); //out.println(val); } long ans=low; //double val1=((double)(((hot+cold)*low + hot))/(double)(2*low+1)); //double val2=((double)(((hot+cold)*(low+1) + hot))/(double)(2*(low+1)+1)); //double diff1=Math.abs(val1-req); //double diff2=Math.abs(val2-req); //if(diff2<diff1) //{ // ans=low+1L; //} double n1=(hot+cold)*low + hot; double n2=(hot+cold)*(low+1) + hot; double d1=2*low+1; double d2=2*(low+1)+1; double val1=Math.abs((req*d1-n1))*d2; double val2=Math.abs((req*d2-n2))*d1; //out.println(val1+" "+val2); if(val1>val2) ans=low+1; out.println( (2L*ans)+1L ); } } } out.flush(); in.close(); out.close(); } static void printSDA(int arr[]) { int l=arr.length; for(int i=0;i<l;i++) { System.out.print(arr[i]+" "); } } }
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() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long i, T, n; cin >> T; while (T--) { double h, c, t, ans; cin >> h >> c >> t; double res = h, freq = 1.0, diff = fabs(((h + c) / 2) - t); if (h == t) { cout << 1 << "\n"; continue; } if ((h + c) / 2 >= t) { cout << 2 << "\n"; continue; } freq = 1 + 2 * ceil((t - h) / (h + c - 2 * t)); double freq2 = freq - 2; if (fabs((h + (h + c) * ((freq - 1) / 2)) / (freq)-t) < fabs((h + (h + c) * ((freq2 - 1) / 2)) / (freq2)-t)) { if (diff < fabs((h + (h + c) * (freq / 2)) / (1 + freq) - t)) { cout << 2 << "\n"; } else { cout << freq << "\n"; } } else { if (diff < fabs((h + (h + c) * (freq2 / 2)) / (1 + freq2) - t)) { cout << 2 << "\n"; } else { cout << freq2 << "\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 import sys from fractions import Fraction input = sys.stdin.readline ts = int(input()) # (h + c) * n / 2n = (h + c) / 2 # ((h + c) * n + h) / (2n + 1) = (h + c) * n/(2n + 1) + h/(2n + 1) res = [] for cs in range(ts): h, c, t = map(int, input().split()) if 2 * t <= (h + c): res.append(2) else: l, r = 0, int(1e9) while l < r: mid = (l + r) // 2 if (h + c) * mid + h <= t * (mid + mid + 1): r = mid else: l = mid + 1 opt = l init = Fraction(h - c) ans = 2 for i in range(opt - 1, opt + 1): if i < 0: continue if Fraction(t) < Fraction((h + c) * i + h, 2*i + 1): dist = Fraction((h + c) * i + h, 2*i + 1) - Fraction(t) else: dist = Fraction(t) - Fraction((h + c) * i + h, 2*i + 1) if dist < init: init = dist ans = 2 * i + 1 res.append(ans) print('\n'.join(map(str, res)))
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import math from sys import stdin,stdout import sys import fractions mod=1000000007 F=fractions.Fraction t=int(stdin.readline()) while t>0: h,c,tt=list(map(int,stdin.readline().split())) mini=sys.maxsize ans=0 xx=[] if(2*tt>h+c): xx.append((h-tt)//((2*tt)-(h+c))) xx.append((h-tt)//((2*tt)-(h+c))+1) for x in xx: delta=F(h*(x+1)+c*x,2*x+1) if(mini>abs(tt-delta)): mini=abs(tt-delta) ans=2*x+1 else: ans=2 stdout.write(f"{ans}\n") t-=1
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
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); double d2 = Math.abs(temp * 1L * (2 * n + 1) - (n * (cold + hot) + hot)) / (2 * n + 1.0); if (d1 <= d2) { out.println(2 * n - 1); } else { 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
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; template <typename T> using v2 = vector<vector<T>>; template <typename T> inline v2<T> fill(int r, int c, T t) { return v2<T>(r, vector<T>(c, t)); } int h, c, t; ll calc(int n) { return (ll)h * (n + 1) + (ll)c * n; } void solve() { cin >> h >> c >> t; if (t >= h) { cout << "1\n"; } else if (2 * t <= h + c) { cout << "2\n"; } else { int lo = 0; int hi = 1e9; int ans = -1; while (lo <= hi) { int mid = (lo + hi) / 2; if ((ll)(mid + 1) * h + (ll)mid * c >= (ll)t * (2 * mid + 1)) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } int a = 2 * ans + 1; int b = 2 * (ans + 1) + 1; ll e = abs((ll)t * a - calc(ans)); ll f = abs((ll)t * b - calc(ans + 1)); if (e * b <= f * a) { cout << a << '\n'; } else { cout << b << '\n'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; for (int i = 0; i < t; i++) { solve(); } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; template <typename X> ostream& operator<<(ostream& x, const vector<X>& v) { for (long long i = 0; i < v.size(); ++i) x << v[i] << " "; return x; } template <typename X> ostream& operator<<(ostream& x, const set<X>& v) { for (auto it : v) x << it << " "; return x; } template <typename X, typename Y> ostream& operator<<(ostream& x, const pair<X, Y>& v) { x << v.first << " " << v.second; return x; } template <typename T, typename S> ostream& operator<<(ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; } double h, c, t; double f(long long x) { double res = (((h + c) * x + h) / (2.0 * x + 1)) - t; return fabs(res); } long long ternary() { long long l = 0, r = 2e9; while (l <= r - 5) { long long m1 = l + (r - l) / 3; long long m2 = r - (r - l) / 3; if (f(m1) < f(m2)) { r = m2; } else { l = m1; } } for (long long i = l; i <= r; i++) { if (f(i) <= f(i + 1)) { return i; } } return l; } void solve() { cout << setprecision(20); cin >> h >> c >> t; double diff = fabs((h + c) / 2.0 - t); long long res = ternary(); double diff2 = f(res); double diff3 = abs(h - t); 42; double mn = min({diff, diff2, diff3}); for (long long j = 0; j <= 30; j++) { 42; } if (mn == diff3) { cout << 1 << endl; return; } if (mn == diff) { cout << 2 << endl; } else if (mn == diff2) { cout << 2 * res + 1 << endl; } else { cout << 1 << endl; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { solve(); } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.util.*; public class F { public static void main(String[] args) throws Throwable { Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); int T = sc.nextInt(); while (T-- > 0) { h = sc.nextInt(); c = sc.nextInt(); int t = sc.nextInt(); Fraction minDif = new Fraction(Math.abs(t - h), 1); long ans = 1; Fraction avg = getTemp(1, 1); if (avg.subtract(t).compareTo(minDif) < 0) { minDif = avg.subtract(t); ans = 2; } if (2 * t != c + h) { int x = (t - h) / (c + h - 2 * t); for (int cold = Math.max(1, x - 2); cold <= Math.max(1, x + 2); cold++) { Fraction tmp = getTemp(cold + 1, cold); if (tmp.subtract(t).compareTo(minDif) < 0) { minDif = tmp.subtract(t); ans = 2 * cold + 1; } } } pw.println(ans); } pw.close(); } static int h, c; static Fraction getTemp(long hots, long colds) { return new Fraction(hots * h + colds * c, hots + colds); } static class Fraction implements Comparable<Fraction> { long x, y; Fraction(long a, long b) { x = a; y = b; } public int compareTo(Fraction f) { return Long.compare(Math.abs(x) * Math.abs(f.y), Math.abs(y) * Math.abs(f.x)); } public Fraction subtract(int n) { return new Fraction(x - n * y, y); } } static class Scanner { StringTokenizer st; BufferedReader br; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String s) throws Throwable { br = new BufferedReader(new FileReader(new File(s))); } String next() throws Throwable { if (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws Throwable { return Integer.parseInt(next()); } long nextLong() throws Throwable { 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
# -*- coding: utf-8 -*- import sys # from collections import defaultdict, deque # from math import log10, sqrt, ceil # from pprint import pprint def input(): return sys.stdin.readline()[:-1] # warning not \n # def input(): return sys.stdin.buffer.readline()[:-1] from functools import lru_cache from itertools import product from fractions import Fraction as F # sys.setrecursionlimit(int(1e8)) def f(h, c, m, t): return h * (m + 1) + c * m def solve(): h, c, t = [int(x) for x in input().split()] h *= 10**9 c *= 10**9 t *= 10**9 if (h + c) >= t * 2: print(2) return delta = F(abs(t - h)) ans = 1 l = 0 r = 10 ** 18 while l + 1 < r: m = (l+r) // 2 if f(h,c,m,t) > t * (2*m+1): l = m else: r = m # print(l, r) for l in range(l, r+3): if abs(F(t)-F(f(h,c,l,t),(2*l+1))) < delta: delta = abs(F(t)-F(f(h,c,l,t),(2*l+1))) ans = (2*l+1) print(ans) t = 1 t = int(input()) for case in range(1,t+1): ans = solve() """ """
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from sys import stdin input=stdin.readline import bisect import math #i = bisect.bisect_left(a, k) #list=input().split(maxsplit=1) def sol(x,y,z): p=(x+y)/2 p=p-z p*=2 kk=(y-x)/p return kk for xoxo in range(1): #a=[] for _ in range (int(input())): #n=int(input()) ans=[[],[]] h,c,t=map(int, input().split()) ans[0].append(abs(h-t)) ans[1].append(1) if h==t: print('1') continue ans[0].append(abs( t-((h+c)/2) ) ) ans[1].append(2) if (h+c)%2==0 and (h+c)//2==t: print('2') continue x=math.ceil(sol(h,c,t)) y=x if x%2==0 and x>1: x-=1 y+=1 if x>0: ans[0].append(abs(t-((h+c)/2)+((c-h)/(2*x)))) ans[1].append(x) if y>0: ans[0].append(abs(t-((h+c)/2)+((c-h)/(2*y)))) ans[1].append(y) o=min(ans[0]) for i in range(len(ans[0])): if ans[0][i]==o: print(ans[1][i]) break #a=list(map(int, input().split())) #ans=0
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t 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 { void solve() throws IOException { in = new InputReader("__std"); out = new OutputWriter("__std"); int testCount = in.readInt(); for (int test = 1; test <= testCount; ++test) { long h = in.readInt(); long c = in.readInt(); long t = in.readInt(); if (t * 2 <= h + c) { out.println(2); } else { long k = (t - c) / (t * 2 - h - c); long l = k * 2 - 1; long r = k * 2 + 1; t *= l * r; long diff = (h * k + c * (k - 1)) * r + (h * (k + 1) + c * k) * l - t * 2; out.println(diff <= 0 ? l : r); } } exit(); } void exit() { out.close(); //System.err.println((System.currentTimeMillis() - startTime) + " ms"); System.exit(0); } InputReader in; OutputWriter out; //long startTime = System.currentTimeMillis(); public static void main(String[] args) throws IOException { new C().solve(); } class InputReader { private InputStream stream; private byte[] buffer = new byte[1024]; private int pos, len; private int cur; private StringBuilder sb = new StringBuilder(32); InputReader(String name) throws IOException { if (name.equals("__std")) { stream = System.in; } else { stream = new FileInputStream(name); } cur = read(); } private int read() throws IOException { if (len == -1) { throw new EOFException(); } if (pos >= len) { pos = 0; len = stream.read(buffer); if (len == -1) return -1; } return buffer[pos++] & 0xff; } boolean whitespace() { return cur == ' ' || cur == '\t' || cur == '\r' || cur == '\n' || cur == -1; } boolean eol() { return cur == '\r' || cur == '\n' || cur == -1; } boolean eof() { return cur == -1; } byte readByte() throws IOException { if (eof()) { throw new EOFException(); } byte res = (byte) cur; cur = read(); return res; } char readChar() throws IOException { if (eof()) { throw new EOFException(); } char res = (char) cur; cur = read(); return res; } int readInt() throws IOException { if (eof()) { throw new EOFException(); } while (whitespace()) { cur = read(); } if (eof()) { throw new EOFException(); } int sign = 1; if (cur == '-') { sign = -1; cur = read(); if (cur < '0' || cur > '9') { throw new NumberFormatException(); } } int res = 0; while (!whitespace()) { if (cur < '0' || cur > '9') { throw new NumberFormatException(); } res *= 10; res += cur - '0'; cur = read(); } return res * sign; } long readLong() throws IOException { if (eof()) { throw new EOFException(); } while (whitespace()) { cur = read(); } if (eof()) { throw new EOFException(); } int sign = 1; if (cur == '-') { sign = -1; cur = read(); if (cur < '0' || cur > '9') { throw new NumberFormatException(); } } long res = 0; while (!whitespace()) { if (cur < '0' || cur > '9') { throw new NumberFormatException(); } res *= 10; res += cur - '0'; cur = read(); } return res * sign; } double readDouble() throws IOException { return Double.parseDouble(readToken()); } String readToken() throws IOException { if (eof()) { throw new EOFException(); } while (whitespace()) { cur = read(); } if (eof()) { throw new EOFException(); } sb.setLength(0); while (!whitespace()) { sb.append((char) cur); cur = read(); } return sb.toString(); } String readLine() throws IOException { if (eof()) { throw new EOFException(); } sb.setLength(0); while (!eol()) { sb.append((char) cur); cur = read(); } if (cur == '\r') { cur = read(); } if (cur == '\n') { cur = read(); } return sb.toString(); } void skipLine() throws IOException { if (eof()) { throw new EOFException(); } while (!eol()) { cur = read(); } if (cur == '\r') { cur = read(); } if (cur == '\n') { cur = read(); } } } class OutputWriter { private PrintWriter writer; OutputWriter(String name) throws IOException { if (name.equals("__std")) { writer = new PrintWriter(System.out); } else { writer = new PrintWriter(name); } } void print(String format, Object ... args) { writer.print(new Formatter(Locale.US).format(format, args)); } void println(String format, Object ... args) { writer.println(new Formatter(Locale.US).format(format, args)); } void print(Object value) { writer.print(value); } void println(Object value) { writer.println(value); } void println() { writer.println(); } void printArray(int[] a) { boolean first = true; for (int v : a) { if (!first) { writer.print(' '); } else { first = false; } writer.print(v); } writer.println(); } void printArray(int[][] a) { for (int[] v : a) { printArray(v); } } void flush() { writer.flush(); } void close() { writer.close(); } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import os import sys from io import BytesIO, IOBase import math def main(): pass # 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") import os import sys from io import BytesIO, IOBase def main(): pass # 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") for xyz in range(0,int(input())): h,c,t=map(int,input().split()) if (2*t-h-c<=0): print(2) elif t>=h: print(1) else: k=(h-t)//(2*t-h-c) term1=k*(h+c)+h term1-=(t*(2*k+1)) term2=(k+1)*(h+c) term2+=h term2-=t*(2*k+3) if(abs(term1*(2*k+3))<=abs(term2*(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.BufferedReader; import java.io.InputStreamReader; import java.math.*; public class C { public static void main(String []args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String input; String []splited; int q,n,m,t; int []arr,arr1; int[][]mat; input=br.readLine(); splited=input.split(" "); t=Integer.parseInt(splited[0]); long a,b,c,d; int a1,b1,c1,d1;long x,y,z; int u,v,k; int yy=0; String []sa; String st; for(int i=0;i<t;i++) { input=br.readLine(); splited=input.split(" "); double hot=Integer.parseInt(splited[0]); double cold=Integer.parseInt(splited[1]); double tem=Integer.parseInt(splited[2]); if(hot==tem) { System.out.println(1); } else if((hot+cold)/2.0==tem) { System.out.println(2); } else if(hot+cold-2*tem>=0) { System.out.println(2); } else { //long s1=tem-hot; //long s2=hot+cold-2*tem; //BigDecimal big=new BigDecimal(""+s1); //BigDecimal big1=new BigDecimal(""+s2); //System.out.println(big.divide(big1,RoundingMode.CEILING)); double q1=-3.0; q1=(double)(tem-hot)/(hot+cold-2*tem); //System.out.println(q1); double a11=Math.floor(q1); double a12=Math.ceil(q1); double p1=(a11+1)*(hot)+a11*cold; double p2=(2*a11+1); double p3=(a12+1)*(hot)+a12*cold; double p4=(2*a12+1); //System.out.println(b11+" "+b12); //System.out.println(a11+" "+a12); if(Math.abs(p2*tem-p1)*p4<=Math.abs(p4*tem-p3)*p2) { long ans=((long)a11)*2+1; System.out.println(ans); } else { long ans=((long)a12)*2+1; System.out.println(ans); } } } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
from decimal import Decimal t1 = int(input()) while t1: t1 += -1 h, c, t = map(int, input().split()) if h == t: print(1) elif h + c >= 2 * t: print(2) else: x = (c - h) // (h + c - 2 * t) mn = 9999999999999999999 ans = 0 for i in range(max(1, x - 10), x + 10): a2 = Decimal(i // 2) a1 = Decimal(i - a2) v1 = Decimal((a1 * h + a2 * c) / i) q1 = Decimal(abs(v1 - t)) if q1 < mn: mn = q1 ans = i q3 = abs(((h + c) / 2) - t) if q3 <= mn: mn = q3 ans = 2 if abs(t - h) <= mn: ans = 1 print(ans)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long h, c, t; cin >> h >> c >> t; if (t == h) { cout << "1" << '\n'; continue; } if (2 * t <= h + c) { cout << "2" << '\n'; continue; } if (2 * t > h + c) { long long ic = 0; ic = ceil(1.0 * (h - t) / (2 * t - h - c)); long double x = 1.0 * ((ic - 1) * (h + c) + h) / (2 * (ic - 1) + 1) - t; long double y = 1.0 * t - 1.0 * (ic * (h + c) + h) / (2 * (ic) + 1); cout << (x <= y ? 2 * (ic - 1) + 1 : 2 * (ic) + 1); } cout << '\n'; } cerr << endl << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.BufferedWriter; import java.io.OutputStreamWriter; public class MixingWater { public static void main(String[] args) { FastReader reader = new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); try { int T = reader.nextInt(); for (int i = 0; i < T; i++) { int h, c, t; h = reader.nextInt(); c = reader.nextInt(); t = reader.nextInt(); if (t == h) { log.write("1\n"); log.flush(); continue; } int total = (h + c) / 2; if (t <= total) { log.write("2\n"); log.flush(); continue; } double x = (double) h - (double) t; x = x / (((double) 2 * (double) t) - (double) h - (double) c ); long ceil = (long) Math.ceil(x); long floor = (long) Math.floor(x); //System.out.println(ceil + " " + floor); if (ceil == floor) { long ans = 2 * ceil + 1; log.write(ans + "\n"); log.flush(); continue; } long answer1 = ((long) t * ((2 * ceil) + 1)) - ((ceil + 1) * (long) h + ceil * (long) c); long answer2 = ((long) t * ((2 * floor) + 1)) - ((floor + 1) * (long) h + floor * (long) c); //System.out.println(answer1 + " " + answer2); answer1 *= (2 * floor + 1); answer2 *= (2 * ceil + 1); answer1 = Math.abs(answer1); answer2 = Math.abs(answer2); //System.out.println(answer1 + " " + answer2); long answer; if (answer1 < answer2) { answer = 2 * ceil + 1; } else if (answer2 < answer1) { answer = 2 * floor + 1; } else { answer = 2 * floor + 1; } log.write(answer + "\n"); log.flush(); // double ans1 = (((double) (ceil + 1)) * (double) h) + ((double) ceil * (double) c); // ans1 /= (((double) 2 * (double) ceil) + 1.0); // // double ans2 = (((double) (floor + 1)) * (double) h) + ((double) floor * (double) c); // ans2 /= (((double) 2 * (double) floor) + 1.0); // // //System.out.println(ans1 + " " + ans2); // // double diff1 = (double) t - ans1; // if (Double.compare(diff1, 0.0) < 0) { // diff1 = -1.0 * diff1; // } // // double diff2 = (double) t - ans2; // if (Double.compare(diff2, 0.0) < 0) { // diff2 = -1.0 * diff2; // } // // //System.out.println(diff1 + " " + diff2); // // long ans; // // if (Double.compare(diff1, diff2) < 0) { // ans = 2 * ceil + 1; // } else if (Double.compare(diff2, diff1) < 0) { // ans = 2 * floor + 1; // } else { // System.out.println("Equal"); // ans = 2 * floor + 1; // } // // log.write(ans + "\n"); // log.flush(); } log.close(); } catch (Exception e) { e.printStackTrace(); } } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception 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 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') class Q: def __init__(self, p, q=1): if q == 0: raise ZeroDivisionError self.p = p self.q = q self.reduce() def __repr__(self): return "{}/{}".format(self.p, self.q) def __eq__(self, other): if type(other) == int: return self.p == self.q * other else: return self.p * other.q == self.q * other.p def __ne__(self, other): if type(other) == int: return self.p != self.q * other else: return self.p * other.q != self.q * other.p def __lt__(self, other): if type(other) == int: return self.p < self.q * other else: return self.p * other.q < self.q * other.p def __le__(self, other): if type(other) == int: return self.p <= self.q * other else: return self.p * other.q <= self.q * other.p def __gt__(self, other): if type(other) == int: return self.p > self.q * other else: return self.p * other.q > self.q * other.p def __ge__(self, other): if type(other) == int: return self.p >= self.q * other else: return self.p * other.q >= self.q * other.p def __neg__(self): return Q(-self.p, self.q) def __invert__(self): return Q(self.q, self.p) def __hash__(self): if self.q == 1: return hash(self.p) else: return hash((self.p, self.q)) def __abs__(self): return Q(abs(self.p), abs(self.q)) def __add__(self, other): if type(other) is int: return Q(self.p + other * self.q, self.q) else: return Q(self.p * other.q + self.q * other.p, self.q * other.q) def __radd__(self, other): return self + other def __iadd__(self, other): if type(other) is int: self.p += other * self.q else: self.p = self.p * other.q + self.q * other.p self.q *= other.q self.reduce() return self def __sub__(self, other): return self + (-other) def __rsub__(self, other): return other + (-self) def __isub__(self, other): self += -other return self def __mul__(self, other): if type(other) is int: return Q(self.p * other, self.q) else: return Q(self.p * other.p, self.q * other.q) def __rmul__(self, other): return self * other def __imul__(self, other): if type(other) is int: self.p *= other else: self.p *= other.p self.q *= other.q self.reduce() return self def __truediv__(self, other): if type(other) is int: return Q(self.p, self.q * other) else: return Q(self.p * other.q, self.q * other.p) def __rtruediv__(self, other): if type(other) is int: return Q(other * self.q, self.p) else: return Q(other.p * self.q, other.q * self.p) def __itruediv__(self, other): if type(other) is int: self.q *= other else: self.p *= other.q self.q *= other.p self.reduce() return self def __floordiv__(self, other): if type(other) is int: return self.p // (self.q * other) else: return (self.p * other.q) // (self.q * other.p) def __rfloordiv__(self, other): if type(other) is int: return other * self.q // self.p else: return (other.p * self.q) // (other.q * self.p) def __ifloordiv__(self, other): if type(other) is int: self.p //= self.q * other else: self.p = (self.p * other.q) // (self.q * other.p) self.q = 1 return self def __mod__(self, other): return self - (self // other) * other def __rmod__(self, other): return other - (other // self) * self def __imod__(self, other): self -= (self // other) * other self.reduce() return self def __pow__(self, other): if type(other) is not int: raise EnvironmentError return Q(self.p**other, self.q**other) def __ipow__(self, other): if type(other) is not int: raise TypeError self.p **= other self.q **= other return self def GCD(self): a, b = abs(self.p), abs(self.q) while b > 0: a, b = b, a % b return a def reduce(self): r = self.GCD() self.p //= r self.q //= r if self.q < 0: self.p *= -1 self.q *= -1 return self def floor(self): return self.p // self.q def to_decimal(self): return self.p / self.q def inverse(self): return Q(self.q, self.p) def calc(x, t, h, c): return abs(t - (x * h + (x-1) * c) / (x * 2 - 1)) def solve(): h, c, t = nm() if t * 2 <= h + c: print(2) return x = (t - c) // (2 * t - h - c) h, c, t = Q(h), Q(c), Q(t) cmax = abs(t - (h+c)/2) cidx = -1 for i in (x+1, x): cn = calc(i, t, h, c) if cmax >= cn: cmax = cn cidx = i print(cidx*2 - 1 if cidx >= 0 else 2) return # solve() T = ni() for _ in range(T): solve()
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> struct Drob { long long a, b; Drob(long long a = 0, long long b = 0) : a(a), b(b) { ; } bool operator<(const Drob& other) const { return (a * other.b) < (b * other.a); } Drob operator-(const Drob& other) const { return Drob(a * other.b - other.a * b, b * other.b); } }; Drob ggg(Drob a, Drob b) { return (std::max(a, b) - std::min(a, b)); } void solve() { signed cc, hh, tt; std::cin >> hh >> cc >> tt; long long c(cc), h(hh), t(tt); std::pair<Drob, signed> ans{Drob(1e9, 1), -1}; auto get = [&](long long i) { return std::pair<Drob, signed>{ ggg(Drob(c * (i / 2) + h * (i - (i / 2)), i), Drob(t, 1)), i}; }; auto get2 = [&](long long i) { return Drob(c * (i / 2) + h * (i - (i / 2)), i); }; ans = std::min(get(1), ans); ans = std::min(get(2), ans); long long beg = 1, end = 1e8; while ((beg + 1) < end) { long long mid = (beg + end) / 2; if (get2(mid * 2 + 1) < Drob(t, 1)) { end = mid; } else { beg = mid; } } for (long long i = std::max(1ll, beg - 100); i < (end + 100); i++) { ans = std::min(ans, get(2 * i + 1)); } std::cout << ans.second << '\n'; } signed main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); signed t(1); std::cin >> t; while (t--) { solve(); } return 0; }
CPP
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long mx = 2e6 + 10; int posx[] = {1, -1, 0, 0}; int posy[] = {0, 0, 1, -1}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1, n, k, m, a, b, c, d, h; cin >> t; while (t--) { cin >> h >> c >> k; if (h + c >= 2 * k) { cout << 2 << endl; continue; } long long x = (c - k) / (h + c - (2 * k)); long long y = x + 1; long double xx = ((h * x) + (c * (x - 1))) / ((1.0) * 2 * x - 1); long double yy = ((h * y) + (c * (y - 1))) / ((1.0) * 2 * y - 1); if (abs(xx - k) <= abs(yy - k)) 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
def rs(): return input().strip() def ri(): return int(input()) def ria(): return list(map(int, input().split())) def ia_to_s(a): return ' '.join([str(s) for s in a]) def temp_at_2k_1(h, c, k): return k * h + (k - 1) * c, 2 * k - 1 def solve(h, c, t): if t <= (h+c)//2: return 2 k_left = 1 k_right = 10**19 while k_right - k_left > 1: k_mid = (k_left + k_right) // 2 t_mid_n, t_mid_d = temp_at_2k_1(h, c, k_mid) if t_mid_n > t * t_mid_d: k_left = k_mid else: k_right = k_mid abs_min = (abs(2*t-(h+c)), 2) ans = 2 test = [x for x in [k_left+2, k_left+1, k_left, k_left-1, k_left-2] if x > 0] for at in test: t_at_n, t_at_d = temp_at_2k_1(h, c, at) if abs_min[1] * abs(t_at_d * t - t_at_n) <= abs_min[0] * t_at_d: abs_min = (abs(t_at_d * t - t_at_n), t_at_d) ans = 2*at-1 return ans def main(): for _ in range(ri()): h, c, t = ria() print(solve(h, c, t)) if __name__ == '__main__': main()
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; public class Main { private static final String NO = "NO"; private static final String YES = "YES"; InputStream is; PrintWriter out; String INPUT = ""; private static final long MOD = 1000000007L; void solve() { int T = ni(); while (T-- > 0) { long h = ni(); long c = ni(); long t = ni(); // h (h+c)/2 // (a+1)*h+a*c = a*(h+c)+h // x/(2*a+1) = t if (2 * t <= h + c) out.println(2); else if (t == h) out.println(1); else { long l = 0; long r = 100000000; while (l < r - 1) { long a = (l + r) / 2; long d = get(h, c, t, a); if (d > 0) // too hot l = a; else r = a; // tr(l, r, a, d); } int d = get2(h, c, t, r, l); out.println(d < 0 ? 2 * r + 1 : 2 * l + 1); } } } private long get(long h, long c, long t, long a) { return (a * (h + c) + h) - (2 * a + 1) * t; } private int get2(long h, long c, long t, long a, long b) { long cmp = Math.abs((a * (h + c) + h) - t * (2 * a + 1)) * (2 * b + 1) - Math.abs((b * (h + c) + h) - t * (2 * b + 1)) * (2 * a + 1); return Long.signum(cmp); } long power(long a, long b) { long x = 1, y = a; while (b > 0) { if (b % 2 != 0) { x = (x * y) % MOD; } y = (y * y) % MOD; b /= 2; } return x % MOD; } private long gcd(long a, long b) { while (a != 0) { long tmp = b % a; b = a; a = tmp; } return b; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private boolean vis[]; 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) { if (!(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 List<Integer> na2(int n) { List<Integer> a = new ArrayList<Integer>(); for (int i = 0; i < n; i++) a.add(ni()); return a; } private int[][] na(int n, int m) { int[][] a = new int[n][]; for (int i = 0; i < n; i++) a[i] = na(m); 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(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private long[][] nl(int n, int m) { long[][] a = new long[n][]; for (int i = 0; i < n; i++) a[i] = nl(m); return a; } 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 static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } public class Pair<K, V> { /** * Key of this <code>Pair</code>. */ private K key; /** * Gets the key for this pair. * * @return key for this pair */ public K getKey() { return key; } /** * Value of this this <code>Pair</code>. */ private V value; /** * Gets the value for this pair. * * @return value for this pair */ public V getValue() { return value; } /** * Creates a new pair * * @param key The key for this pair * @param value The value to use for this pair */ public Pair(K key, V value) { this.key = key; this.value = value; } /** * <p> * <code>String</code> representation of this <code>Pair</code>. * </p> * * <p> * The default name/value delimiter '=' is always used. * </p> * * @return <code>String</code> representation of this <code>Pair</code> */ @Override public String toString() { return key + "=" + value; } /** * <p> * Generate a hash code for this <code>Pair</code>. * </p> * * <p> * The hash code is calculated using both the name and the value of the * <code>Pair</code>. * </p> * * @return hash code for this <code>Pair</code> */ @Override public int hashCode() { // name's hashCode is multiplied by an arbitrary prime number (13) // in order to make sure there is a difference in the hashCode between // these two parameters: // name: a value: aa // name: aa value: a return key.hashCode() * 13 + (value == null ? 0 : value.hashCode()); } /** * <p> * Test this <code>Pair</code> for equality with another <code>Object</code>. * </p> * * <p> * If the <code>Object</code> to be tested is not a <code>Pair</code> or is * <code>null</code>, then this method returns <code>false</code>. * </p> * * <p> * Two <code>Pair</code>s are considered equal if and only if both the names and * values are equal. * </p> * * @param o the <code>Object</code> to test for equality with this * <code>Pair</code> * @return <code>true</code> if the given <code>Object</code> is equal to this * <code>Pair</code> else <code>false</code> */ @Override public boolean equals(Object o) { if (this == o) return true; if (o instanceof Pair) { Pair pair = (Pair) o; if (key != null ? !key.equals(pair.key) : pair.key != null) return false; if (value != null ? !value.equals(pair.value) : pair.value != null) return false; return true; } return false; } } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import java.io.*; import java.util.*; public class MainClass { static ArrayList<Long> val = new ArrayList<>(); public static void main(String[] args)throws IOException { Reader in = new Reader(); StringBuilder stringBuilder = new StringBuilder(); int t = in.nextInt(); while (t-- > 0) { long h = in.nextLong(); long c = in.nextLong(); long tb = in.nextLong(); if (h == tb) { stringBuilder.append("1\n"); continue; } if ((h + c) == 2L * tb) { stringBuilder.append("2\n"); continue; } else { if (2 * tb > (h + c)) { if (tb >= h) { stringBuilder.append("1\n"); continue; } val.clear(); long yy = (h - c) / (2 * tb - h - c); if (yy % 2 == 0L) { if (yy - 1 > 0L) val.add(yy - 1L); if (yy - 3 > 0L) val.add(yy - 3L); val.add(yy + 1L); val.add(yy + 3L); } else { if (yy - 2 > 0L) val.add(yy - 2L); if (yy - 4 > 0L) val.add(yy - 4L); val.add(yy); val.add(yy + 2L); val.add(yy + 4L); } double min = Double.MAX_VALUE; long ans = -1; Collections.sort(val); for (long x: val) { double mm = f(x, h, c, tb); if (mm < min) { min = mm; ans = x; } } stringBuilder.append(ans).append("\n"); } else { stringBuilder.append("2\n"); continue; } } } System.out.println(stringBuilder); } public static double f(long x, long h, long c, long tb) { double mm1 = (double)tb - 0.5d * (h + c) - 0.5d * (h - c) / (double)x; return Math.abs(mm1); } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } }
JAVA
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
#include <bits/stdc++.h> using namespace std; using ii = pair<unsigned long long, unsigned long long>; using ll = long long; const double EPS = 1e-9; bool equals(double a, double b) { return fabs(a - b) < EPS; } int32_t main() { ios::sync_with_stdio(0); unsigned long long t; cin >> t; while (t--) { double h, c, t; cin >> h >> c >> t; double l = (h + c) / 2.0; double r = h; pair<double, unsigned long long> ans = make_pair(fabs(r - t), 1LLU); ans = min(ans, make_pair(fabs(l - t), 2LLU)); if (not equals(r, t)) { double k = (h - c) / ((2.0 * t) - (h + c)); unsigned long long k1 = k; if (k1 % 2 == 0) ++k1; unsigned long long k2 = k1 - 2; unsigned long long k3 = k1 + 2; double a, b, res; a = (k1 + 1.0) / 2.0; b = (k1 - 1.0) / 2.0; res = (a * h + b * c) / double(k1); ans = min(ans, make_pair(fabs(res - t), k1)); a = (k2 + 1.0) / 2.0; b = (k2 - 1.0) / 2.0; res = (a * h + b * c) / double(k2); ans = min(ans, make_pair(fabs(res - t), k2)); a = (k3 + 1.0) / 2.0; b = (k3 - 1.0) / 2.0; res = (a * h + b * c) / double(k3); ans = min(ans, make_pair(fabs(res - t), k3)); } cout << ans.second << 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; long long int MOD = 1000000007; long long int MX = 400000000000000000; long long int T, t, h, c; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> T; while (T--) { cin >> h >> c >> t; if (2 * t == h + c) { cout << 2 << endl; } else { long double hh = h; long double cc = c; long double tt = t; long double x = (hh - tt) / (2.0 * tt - hh - cc); long long int x1 = floor(x); long long int x2 = ceil(x); if (t >= h) cout << 1 << endl; else if (2 * t <= (h + c)) { cout << 2 << endl; } else { if (abs((2 * x2 + 1) * ((x1 + 1) * h + x1 * c - t * (2 * x1 + 1))) <= abs((2 * x1 + 1) * (t * (2 * x2 + 1) - (x2 + 1) * h - x2 * c))) { cout << 2 * x1 + 1 << endl; } else { cout << 2 * x2 + 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
import sys,math Tests=int(input()) for _ in range(Tests): h,c,t=map(int,sys.stdin.readline().split()) if h==t: print(1) else: x=(h+c)/2 if x>=t: print(2) else: diff=t-x some=int(abs((c-h)//(2*diff))) if(some%2==0): some+=1 if(diff-(h-c)/(2*some)<abs(diff-(h-c)/(2*(some-2)))): print(some) else: print(some-2)
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
T = int(input()) o = [] for i in range(T): h, c, t = input().split() h = int(h) c = int(c) t = int(t) s = (h + c) / 2 if t >= h: o.append(1) elif t <= s: o.append(2) else: i = (h - t) / (2 * t - (h + c)) #print(i) if i == int(i): i = int(i) o.append(2 * i + 1) else: i = int(i) aprev = abs((i * (h + c - 2 * t) + h - t) / (2 * i + 1)) i += 1 acur = abs((i * (h + c - 2 * t) + h - t) / (2 * i + 1)) #print(aprev, acur, t) if acur >= aprev: o.append((i - 1) * 2 + 1) else: o.append(i * 2 + 1) for i in range(T): print(o[ i ]) #'499981', found: '499979' # 999977 17 499998
PYTHON3
1359_C. Mixing Water
There are two infinite sources of water: * hot water of temperature h; * cold water of temperature c (c < h). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water ... 4. and so on ... Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer T (1 ≀ T ≀ 3 β‹… 10^4) β€” the number of testcases. Each of the next T lines contains three integers h, c and t (1 ≀ c < h ≀ 10^6; c ≀ t ≀ h) β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integer β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t. Example Input 3 30 10 20 41 15 30 18 13 18 Output 2 7 1 Note In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve. In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that. In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t.
2
9
import sys from decimal import * input=sys.stdin.buffer.readline def temp(nHot,h,c): #assuming that nCold=nHot-1 return (nHot*h+(nHot-1)*c)/(2*nHot-1) def tempDec(nHot,h,c): #for higher precision return Decimal(nHot*h+(nHot-1)*c)/(2*nHot-1) #for more precision nTests=int(input()) for _ in range(nTests): # print(_+1)######### h,c,t=[int(zz) for zz in input().split()] # print(_+1)######### #Observe that for every nHot==nCold (nCups//2==0), finalt=(h+c)/2. #if t<=(h+c)/2, ans=2 #else, perform all binary search for all nHot=nCold+1 to find where temp(nHot)>t and temp(nHot+1)<=t, #and return nHot+nCold #with algebra, 1<=nHot<=int((h/2-c/4+0.5))+1 (that's the nHot to get t=(h+c)/2+1) if t<=(h+c)/2: print(2) elif t==h: print(1) else: lo=1;hi=int((h/2-c/4+0.5))+1 while True: nHot=(lo+hi)//2 if temp(nHot,h,c)>t and temp(nHot+1,h,c)<=t: #either nHot or nHot+1 break elif temp(nHot,h,c)>t: lo=nHot+1 else: #temp(nHot,h,c)<=t hi=nHot-1 # print(nHot) # print(temp(nHot,h,c),temp(nHot+1,h,c)) if abs(temp(nHot,h,c)-t)<abs(temp(nHot+1,h,c)-t): #nHot print(nHot*2-1) elif abs(temp(nHot,h,c)-t)>abs(temp(nHot+1,h,c)-t): #nHot+1 print(nHot*2+1) else: #equal. check with Decimal a,b=abs(tempDec(nHot,h,c)-t),abs(tempDec(nHot+1,h,c)-t) if a<=b: print(nHot*2-1) else: print(nHot*2+1)
PYTHON3