Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from sys import stdin
for _ in range(int(stdin.readline())):
h,c,t = map(int,stdin.readline().split())
if t>=h:
print(1)
continue
if t <= (h+c)/2:
print(2)
continue
n = 1/(1-2*((h-t)/(h-c)))
x = round(n)
if x%2==0:
y = x-1
ex = 2*t*y*(y+2)-2*((y//2+1)*h+(y//2)*c)*(y+1)-(h+c)*y
if ex < 0:
print(y+2)
else:
print(y)
else:
print(x) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from sys import stdin
input = stdin.buffer.readline
for _ in range(int(input())):
h, c, t = map(int, input().split())
d = abs((t << 1) - h - c)
ans = 2
l, r = 0, 1000000
while r - l > 1:
mid = (l + r) >> 1
if (h + c) * mid + h < t * (mid << 1 | 1):
r = mid
else:
l = mid
tmp = abs(t * (r << 1 | 1) - (h + c) * r - h)
if (tmp << 1) < d * (r << 1 | 1) or (tmp << 1) == d * (r << 1 | 1) and (r << 1 | 1) < ans:
d = tmp
ans = r << 1 | 1
tmp = abs(t * (l << 1 | 1) - (h + c) * l - h)
if (tmp * ans) < d * (l << 1 | 1) or (tmp * ans) == d * (l << 1 | 1) and (l << 1 | 1) < ans:
d = tmp
ans = l << 1 | 1
print(ans)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 2e18;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int tc;
cin >> tc;
while (tc--) {
long long h, c, t;
cin >> h >> c >> t;
if (t >= h) {
cout << "1\n";
} else {
long double gap = fabs(t - (h + c) / 2.0);
int res = 2;
int n = (int)round((long double)(h - t) / (long double)(t + t - h - c));
for (int i = max(0, n - 100); i <= n + 100; ++i) {
int k = i + i + 1;
long double temp = (long double)(h * (i + 1) + c * i) / k;
long double tg = fabs(temp - t);
if (tg < gap) res = k, gap = tg;
}
cout << res << "\n";
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int round;
cin >> round;
for (; round; round--) {
int h, c, t;
cin >> h >> c >> t;
if (h <= t) {
cout << 1 << endl;
continue;
} else if (c >= t) {
cout << 2 << endl;
continue;
}
double tem = (double)(h + c) / 2.0;
if (tem >= t) {
cout << 2 << endl;
continue;
} else {
tem = (double)t - tem;
int k = (double)(h - c) / (2.0 * tem);
if (k % 2) {
double difference_1 = abs((double)(h - c) / (2.0 * k) - tem);
double difference_2 = abs(tem - (double)(h - c) / (2.0 * (k + 2)));
if (difference_1 <= difference_2) {
cout << k << endl;
} else {
cout << k + 2 << endl;
}
} else {
double difference_1 = abs((double)(h - c) / (2.0 * (k - 1)) - tem);
double difference_2 = abs(tem - (double)(h - c) / (2.0 * (k + 1)));
if (difference_1 <= difference_2) {
cout << k - 1 << endl;
} else {
cout << k + 1 << endl;
}
}
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
long long gcdf(long long a, long long b) {
while (a != 0 && b != 0) {
if (a > b) {
a %= b;
} else {
b %= a;
}
}
return max(a, b);
}
long long power(long long a, long long b, long long m) {
long long ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans;
}
long long mod_div(long long a, long long b) { return power(a, b - 2, b) % MOD; }
int main() {
long long test = 1, i = 1, j = 1;
cin >> test;
while (test--) {
double h, c, t;
cin >> h >> c >> t;
double mi = 0.5 * h + 0.5 * c;
if (mi >= t) {
cout << 2 << endl;
continue;
}
long long lo = 1, hi = 1e12, val = 2;
double ans = abs(t - mi);
while (lo <= hi) {
long long mid = (lo + hi) / 2;
if (mid % 2 == 0) mid++;
long long ho = mid / 2 + 1;
long long co = mid / 2;
double curr = ((double)ho * (double)h) / (double)mid +
((double)co * (double)c) / (double)mid;
double diff = abs((double)t - (double)curr);
if (ans >= diff) {
if (ans == diff)
val = min(val, mid);
else {
val = mid;
ans = diff;
}
}
if (t == curr || lo == hi) break;
if (t <= curr) {
lo = mid + 1;
} else
hi = mid - 1;
}
cout << val << endl;
}
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
static class Pair implements Comparable<Pair>{
int a;
int b;
public Pair(int x,int y){a=x;b=y;}
public Pair(){}
public int compareTo(Pair p){
return p.a - a;
}
}
static class cell implements Comparable<cell>{
int a,b;
long dis;
public cell(int x,int y,long z) {a=x;b=y;dis=z;}
public int compareTo(cell c) {
return Long.compare(dis,c.dis);
}
}
static class TrieNode{
TrieNode[]child;
int w;
boolean term;
TrieNode(){
child = new TrieNode[26];
}
}
public static long gcd(long a,long b)
{
if(a<b)
return gcd(b,a);
if(b==0)
return a;
return gcd(b,a%b);
}
static long lcm(long a,long b) {
return a*b / gcd(a,b);
}
//static long ans = 0;
static long mod = (long)(1e9+7);///998244353;
public static void main(String[] args) throws Exception {
new Thread(null, null, "Anshum Gupta", 99999999) {
public void run() {
try {
solve();
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
static long pow(long x,long y){
if(y == 0)return 1;
if(y==1)return x;
long a = pow(x,y/2);
a = (a*a)%mod;
if(y%2==0){
return a;
}
return (a*x)%mod;
}
static long mxx;
static int mxN = (int)(3e5+5);
static long[]fact,inv_fact;
static long my_inv(long a) {
return pow(a,mod-2);
}
static long bin(int a,int b) {
if(a < b || a<0 || b<0)return 0;
return ((fact[a]*inv_fact[a-b])%mod * inv_fact[b])%mod;
}
//static ArrayList<ArrayList<Integer>>adj;
//static boolean[]vis;
//static long[]dis;
//static long val;
//static void dfs(int sv,int cur) {
// if(vis[sv]) {
// // dis[sv] = cur;
// val = cur-dis[sv];
// return;
// }
// vis[sv] = true;
// dis[sv]=cur;
// for(int x:adj.get(sv)) {
//
// dfs(x,cur+1);
//
// }
//}
static int n,m;
static long[][]c;
static int[]x= {0,0,-1,1};
static int[]y= {-1,1,0,0};
public static void solve() throws Exception {
// solve the problem here
MyScanner s = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out), true);
int tc = s.nextInt();
mxx = (long)(1e18+5);
// fact=new long[mxN];
// inv_fact = new long[mxN];
// fact[0]=inv_fact[0]=1L;
// for(int i=1;i<mxN;i++) {
// fact[i] = (i*fact[i-1])%mod;
// inv_fact[i] = my_inv(fact[i]);
// }
while(tc-->0){
double h = s.nextDouble();
double c = s.nextDouble();
double t = s.nextDouble();
if(t <= (h+c)/2) {
out.println("2");
continue;
}
double k = (h-t)/(2*t-h-c);
double o1 = Math.floor(k);
//out.println(o1);
if( (2*o1+3)*Math.abs((o1*(h+c)+h)-t*(2*o1+1)) <= (2*o1+1)*Math.abs(((o1+1)*(h+c)+h)-t*(2*o1+3))) {
out.println((int)(2*o1+1));
}
else out.println((int)(2*o1+3));
}
out.flush();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int T;
long long sol, k1, k2;
double h, c, t, best, diff, k, t1, t2, pola;
int main() {
cin >> T;
while (T--) {
cin >> h >> c >> t;
pola = (h + c) / 2;
if (t >= h) {
cout << "1" << endl;
continue;
} else if (t <= pola) {
cout << "2" << endl;
continue;
}
sol = 1;
best = h;
diff = abs(h - t);
if (abs((h + c) / 2 - t) < diff) {
sol = 2;
best = (h + c) / 2;
diff = abs(best - t);
}
k = (t - h) / (h + c - 2 * t);
k1 = floor(k);
k2 = ceil(k);
t1 = ((k1 + 1) * h + k1 * c) / (2 * k1 + 1);
t2 = ((k2 + 1) * h + k2 * c) / (2 * k2 + 1);
if (abs(t1 - t) < diff) {
diff = abs(t1 - t);
sol = k1 * 2 + 1;
}
if (abs(t2 - t) < diff) {
diff = abs(t2 - t);
sol = k2 * 2 + 1;
}
cout << sol << 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 | // package Round_88;
import java.io.*;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.*;
public class Mixing_Water {
public static void main(String[] args) {
FastReader in = new FastReader();
int numTrials = in.nextInt();
while(numTrials --> 0) {
int hotTemp = in.nextInt(), coldTemp = in.nextInt(), finalTemp = in.nextInt();
double avg = ((double)hotTemp + coldTemp) / 2;
if(finalTemp <= avg) {
System.out.println(2);
}
// More Hot
else {
int low = 0, high = 5000001;
while(high - low > 1) {
int mid = (high + low) / 2;
double val = calcAvg(hotTemp, coldTemp, mid);
if(val > finalTemp) {
low = mid;
}
else {
high = mid;
}
}
double lowDist = Math.abs(finalTemp - calcAvg(hotTemp, coldTemp, low));
double highDist = Math.abs(finalTemp - calcAvg(hotTemp, coldTemp, high));
if(calcDistance(hotTemp, coldTemp, finalTemp, low).compareTo(calcDistance(hotTemp, coldTemp, finalTemp, high)) <= 0) {
System.out.println(2 * low + 1);
}
else {
System.out.println(2 * high + 1);
}
}
}
}
public static BigDecimal calcDistance(int hot, int cold, int finalTemp, int val) {
long avg = (long)val * (hot + cold) + hot;
BigDecimal calcAvg = BigDecimal.valueOf(avg);
MathContext mc = new MathContext(30);
calcAvg = calcAvg.divide(BigDecimal.valueOf(2 * val + 1), mc);
return BigDecimal.valueOf(finalTemp).subtract(calcAvg).abs();
}
public static double calcAvg(int hot, int cold, int val) {
long avg = (long)val * (hot + cold) + hot;
return (double)avg / (2 * val + 1);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from math import gcd
import sys
def input():
return sys.stdin.readline().rstrip()
dxy = ((-1, 0), (0, -1), (1, 0), (0, 1))
def slv():
#print("hello world")
h, c, t = map(int, input().split())
V = []
if t == h:
print(1)
return
if 2*t <= h + c:
print(2)
return
def f(k):
x = h * (k + 1) + c * k
y = 2*k + 1
return x, y
v = (h - t)//(2*t - h - c)
if 1 <= v - 1:
e = f(v - 1)
V.append((v - 1, e))
if 1 <= v:
e = f(v)
V.append((v, e))
if 1 <= v + 1:
e = f(v + 1)
V.append((v + 1, e))
V.append((0, f(0)))
V.append((1, f(1)))
ans = 0
dif = (1 << 64, 1)
def compare(e, f):
x, y = e
x1, y1 = f
if (y1)*abs(x - t*y) == y*abs(x1 - t*y1):
return 1
if (y1)*abs(x - t*y) < y*abs(x1 - t*y1):
return 0
else:
return 2
for e, tmp_t in V:
if compare(tmp_t, dif) == 1:
ans = min(ans, 2 * e + 1)
elif compare(tmp_t, dif) == 0:
dif = tmp_t
ans = 2 * e + 1
print(ans)
return
def main():
t = int(input())
for _ in range(t):
slv()
return
if __name__ == "__main__":
# in_txt = open("in.txt", "r")
# out_txt = open("out.txt", "w")
# sys.stdin = in_txt
# sys.stdout = out_txt
main()
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import io,os
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import sys
from math import floor,ceil
def solve(h,c,t):
if h==t:
return 1
if 2*t<=h+c:
return 2
if 6*t>=5*h+c:
return 1
if 3*t>=2*h+c:
return 3
if (h-t)%(2*t-(h+c))==0:
cnt=(h-t)//(2*t-(h+c))
return 2*cnt+1
k,l=floor((h-t)/(2*t-(h+c))),ceil((h-t)/(2*t-(h+c)))
#print(k,l)
t_k=abs((h*(k+1)+c*k)/(2*k+1)-t)
t_l=abs((h*(l+1)+c*l)/(2*l+1)-t)
#print(t_k,t_l)
#if t_k<=t_l:
if (2*l+1)*(h*(k+1)+c*k)+(2*k+1)*(h*(l+1)+c*l)<=2*t*(2*k+1)*(2*l+1):
return 2*k+1
return 2*l+1
def main():
T=int(input())
for _ in range(T):
h,c,t=map(int,input().split())
ans=solve(h,c,t)
sys.stdout.write(str(ans)+'\n')
if __name__=='__main__':
main() | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import math
def process_test_case(i_t):
h, c, t = map(int, input().split())
if 2 * t <= h + c:
print(2)
return None
if t == h:
print(1)
return None
t -= 0.5 * (h + c)
n1 = math.ceil(((h - c) / t - 2) / 4)
n2 = n1 - 1
delta1 = abs(t - (h - c) / (4 * n1 + 2))
delta2 = abs(t - (h - c) / (4 * n2 + 2))
if delta1 < delta2:
print(2 * n1 + 1)
else:
print(2 * n2 + 1)
return None
n_tests = int(input())
for i_test in range(1, n_tests + 1):
process_test_case(i_test)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t 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 temp(h, c, n):
return h * (n + 1) + c * n, 2 * n + 1
for _ in range(int(input())):
h, c, t = map(int, input().split())
if t * 2 <= (h + c):
print(2)
continue
n = (h - t) // (2 * t - h - c)
n1 = n + 1
a, b = temp(h, c, n)
a1, b1 = temp(h, c, n1)
d, dn = abs(a - b * t), b
d1, dn1 = abs(a1 - b1 * t), b1
if d * dn1 > d1 * dn:
ans = n1
else:
ans = n
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 | """
NTC here
"""
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
from decimal import Decimal, getcontext
getcontext().prec = 40
def iin(): return int(input())
def lin(): return list(map(Decimal, input().split()))
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
def main():
T = iin()
for _ in range(T):
h, c, t = lin()
ans = [[abs(h-t), 1]]
md = (h+c)/2
ans.append([abs(md-t), 2])
if md<t:
l, r = Decimal(1), Decimal(10**6)
ch = 0
while l<r:
if ch>5:break
x = (l+r)//2
x1 = 2*x -1
a1 = (x*h+(x-1)*c)/x1
ans.append([abs(a1-t), x1])
if a1<t:
r = x
elif a1>t:
l = x
else:
break
if r-l<=1:
ch += 1
ans.sort()
mn =ans[0]
# print(ans)
print(mn[1])
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
# sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main() | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import sys
# import math
# from collections import deque
# import heapq
# from math import inf
# from math import gcd
# print(help(deque))
# 26
pprint = lambda s: print(' '.join(map(str, s)))
input = lambda: sys.stdin.readline().strip()
ipnut = input
for i in range(int(input())):
# n = int(input())
h,c,t= map(int,input().split())
if t<=(h+c)/2:
print(2)
continue
# if t == h:
# print(1)
# continue
# if t==(h+c)//2:
# print(2)
# continue
l = 1
r = 1000000000000000000
# for m in range(20):
# m+=1
# print((m*(h+c)-c)/(m*2-1))
# print("?????????")
def f(m):
return m*(h+c)+h
l=(t-h)//(c+h-2*t)
r = l+1
ansl = (abs(t*(2*l+1)-f(l))*(r*2+1),2*l+1)
ansr = (abs(t*(r*2+1)-f(r))*(2*l+1),r*2+1)
# print(ansl,ansr)
print(min(ansl,ansr)[1])
# a = list(map(int,input().split()))
# s = list(input())
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | //package codeforces.educational_88;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.StringTokenizer;
public class _88_C2 {
public static void main (String[] args) throws Exception {
// String s = "1\n" +
// "973303 519683 866255";
// String s = "2\n" +
// "41 15 30\n" +
// "18 13 18";
String s = "1\n" +
"999977 17 499998";
// br = new BufferedReader(new StringReader(s));
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
long time = System.currentTimeMillis();
rl(); int T = nin();
for (int _T=0; _T<T; ++_T) {
rl(); long h=nlo(), c=nlo(), t=nlo();
double m = (c + h) / 2;
if (t <= m) {
bw.write(Long.toString(2));
bw.newLine();
} else {
double dc = ((t-h)/(h+c-2*t));
long counter2 = (long) ((t-h)/(h+c-2*t));
BigDecimal d2 = BigDecimal.valueOf(t);
BigDecimal d22 = BigDecimal.valueOf(h).multiply(BigDecimal.valueOf(counter2+1));
BigDecimal d23 = BigDecimal.valueOf(c).multiply(BigDecimal.valueOf(counter2));
BigDecimal d24 = d22.add(d23).divide(BigDecimal.valueOf(counter2*2+1), 100, RoundingMode.HALF_UP);
d2 = d2.subtract(d24).abs();
long counter3 = counter2+1;
BigDecimal d3 = BigDecimal.valueOf(t);
BigDecimal d32 = BigDecimal.valueOf(h).multiply(BigDecimal.valueOf(counter3+1));
BigDecimal d33 = BigDecimal.valueOf(c).multiply(BigDecimal.valueOf(counter3));
BigDecimal d34 = d32.add(d33).divide(BigDecimal.valueOf(counter3*2+1), 100, RoundingMode.HALF_UP);
d3 = d3.subtract(d34).abs();
long counter1 = counter2-1;
BigDecimal d1 = BigDecimal.valueOf(t);
BigDecimal d12 = BigDecimal.valueOf(h).multiply(BigDecimal.valueOf(counter1+1));
BigDecimal d13 = BigDecimal.valueOf(c).multiply(BigDecimal.valueOf(counter1));
BigDecimal d14 = d12.add(d13).divide(BigDecimal.valueOf(counter1*2+1), 100, RoundingMode.HALF_UP);
d1 = d1.subtract(d14).abs();
long counter = 0;
if (d1.compareTo(d2)<=0 && d1.compareTo(d3)<=0) {
counter = counter1;
} else if (d2.compareTo(d1)<=0 && d2.compareTo(d3)<=0) {
counter = counter2;
} else if (d3.compareTo(d1)<=0 && d3.compareTo(d2)<=0) {
counter = counter3;
} else {
counter = counter1;
}
counter=counter*2 +1;
bw.write(Long.toString(counter));
bw.newLine();
}
}
bw.flush();
}
static BufferedReader br;
static BufferedWriter bw;
static StringTokenizer st;
static void rl() throws Exception{
st = new StringTokenizer(br.readLine());
}
static long nlo(){
return Long.parseLong(st.nextToken());
}
static int nin(){
return Integer.parseInt(st.nextToken());
}
/*private static void te(){
}*/
static double ndo(){
return Double.parseDouble(st.nextToken());
}
static char[] nca(){
return st.nextToken().toCharArray();
}
} | 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.util.Scanner;
public final class C {
public static void main(String[] args) {
final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
final int t = in.nextInt();
for (int x = 0; x < t; x++) {
final long h = in.nextLong();
final long c = in.nextLong();
final long target = in.nextLong();
in.nextLine();
if (2 * target <= (h + c)) {
System.out.println(2);
continue;
}
if (target == h) {
System.out.println(1);
continue;
}
int lo = 1, hi = (int) 1e9, f = -1;
while (lo <= hi) {
final int mid = lo + hi >> 1;
final long a = h * (mid + 1) + c * mid;
final long b = 2 * mid + 1;
if (a <= b * target) {
f = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
final long an = target * (2 * f + 1) - h * (f + 1) - c * f;
final long ad = 2 * f + 1;
final long bn = h * f + c * (f - 1) - target * (2 * f - 1);
final long bd = 2 * f - 1;
if (an * bd < bn * ad) {
System.out.println(2 * f + 1);
} else {
System.out.println(2 * f - 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 fractions import Fraction
T = int(input())
def solve(h, c, t):
return Fraction((t - h), (h + c - 2 * t))
def with_a(a, h, c):
return Fraction(((a+1) * h + a * c), (2*a + 1))
def closest(t, poss):
best_delta = float('inf')
best = None
for steps, p in poss:
if abs(t - p) < best_delta:
best_delta = abs(t - p)
best = steps
return best
for case in range(T):
h, c, t = map(int, input().split())
poss = []
poss.append((1, h))
avg = Fraction((h + c), 2)
poss.append((2, avg))
try:
x = solve(h, c, t)
except ZeroDivisionError:
pass
else:
w = int(x) - 5
for a in range(w, w+10):
if a <= 0: continue
poss.append((2 * a + 1, with_a(a, h, c)))
poss.sort(key=lambda k: k[0])
print(closest(t, poss))
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int h, c, t;
double func(long long k) {
return abs((double)(k * h + (k - 1) * c) / (double)(2 * k - 1) - (double)t);
}
void ternary(int l, int r) {
int yaz;
while (r - l > 0) {
int m1 = l + (r - l) / 3;
int m2 = r - (r - l) / 3;
double f1 = func(m1);
double f2 = func(m2);
if (f1 > f2) {
l = m1 + 1;
yaz = m2 * 2 - 1;
} else {
r = m2 - 1;
yaz = m1 * 2 - 1;
}
}
cout << yaz << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int i, j, k, T;
cin >> T;
for (k = 1; k <= T; k++) {
cin >> h >> c >> t;
if ((2 * t) <= (h + c))
cout << '2' << endl;
else
ternary(1, 1000000000);
}
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
from decimal import *
from sys import stdin, stdout
T = int(input())
def calc(x, y, n, m):
n = Decimal(n)
m = Decimal(m)
# print(x, y, n, m, (x * n + y * m) / (n + m))
return (x * n + y * m) / (n + m)
def bs(x, y, t):
lo = 1
hi = 1000000000
# print(x, y, t)
while lo<=hi:
mid = int((lo + hi) / 2)
ret = calc(x, y, mid, mid-1)
if ret <= t:
hi = mid - 1
else:
lo = mid + 1
return hi
while T != 0:
h, c, t = stdin.readline().split(' ')
c = Decimal(c)
h = Decimal(h)
t = Decimal(t)
y2 = bs(h, c, t)
# print(y2)
d1 = abs(t - calc(h, c, 1, 0))
d2 = abs(t - calc(h, c, 1, 1))
d3 = abs(t - calc(h, c, y2, y2-1))
d4 = abs(t - calc(h, c, y2+1, y2))
# print(d1, d2, d3, d4)
mn = min(d1, d2, d3, d4)
# print(mn)
ans = -1
if mn==d1:
ans = 1
elif mn==d2:
ans = 2
elif mn==d3:
ans = 2*y2-1
else:
ans = 2*y2+1
stdout.write(str(ans)+'\n')
T = T - 1
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from decimal import Decimal
def solve(h, c, t):
if (h + c) / 2 >= t:
return 2
k = (t - h) // (h + c - 2 * t)
# k = Decimal(k)
# k = math.ceil(k)
delta = lambda k: abs(Decimal((k * (h + c)) + h) / (2 * k + 1) - t)
candidates = (k, delta(k)), (k + 1, delta(k + 1))
return sorted(candidates, key=lambda x: x[1])[0][0] * 2 + 1
for _ in range(int(input())):
h, c, t = map(int, input().split())
k = solve(h, c, t)
print(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 | import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.joining;
public class Main {
void solve() {
long h = in.nextInt();
long c = in.nextInt();
long t = in.nextInt();
if(t<=(0.0+h+c)/2){
out.println(2);
return;
}
long n = (h - t)/(2 * t - h - c);
long tStar = t * (2 * n + 1) * (2 * n + 3);
long tmp1 = (h*(n+1)+c*n)* (2 * n + 3);
long tmp2 = (h*(n+2)+c*(n+1))* (2 * n + 1);
if (Math.abs(tmp1-tStar)<=Math.abs((tmp2-tStar))){
out.println(2*n+1);
}else{
out.println(2*(n+1)+1);
}
}
static final boolean MULTI_TEST = true;
// --------------------------------------------------------------------------------------------------------------
// --------------------------------------------------HELPER------------------------------------------------------
// -------------------------------{-------------------------------------------------------------------------------
void solve2() {
// init();
if (MULTI_TEST) {
int t = in.nextInt();
for (int i = 0; i < t; i++) {
solve();
}
} else {
solve();
}
}
// --------------------ALGORITHM-------------------------
public void sort(int[] arr) {
List<Integer> tmp = Arrays.stream(arr).boxed().sorted().collect(Collectors.toList());
for (int i = 0; i < arr.length; i++) {
arr[i] = tmp.get(i);
}
}
// --------------------SCANNER-------------------------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner(boolean debug) {
if (debug) {
try {
br = new BufferedReader(new FileReader("input.txt"));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
} else {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextInts(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
try {
String line = br.readLine();
if (line == null) {
throw new RuntimeException("empty line");
}
return line;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
// --------------------WRITER-------------------------
public static class MyWriter extends PrintWriter {
public MyWriter(OutputStream out) {
super(out);
}
void println(int[] arr) {
String line = Arrays.stream(arr).mapToObj(String::valueOf).collect(joining(" "));
println(line);
}
}
// --------------------MAIN-------------------------
public MyScanner in;
public MyWriter out;
public static void main(String[] args) {
Main m = new Main();
m.in = new MyScanner(args.length > 0);
m.out = new MyWriter(new BufferedOutputStream(System.out));
m.solve2();
m.out.close();
}
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | for _ in range(int(input())):
h,c,t = map(int,input().split())
if h+c - 2*t>=0:
print(2)
continue
else:
a = h-t
b = 2*t - h - c
k = a//b
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 | T = int(input())
for i in range(T):
h, c, t = map(int, input().split())
avg = (h + c) / 2
if avg >= t:
print(2)
else:
n = ((h - c) / (2 * (t - avg)) + 1) / 2
if type(n) == int:
print(2 * n - 1)
else:
n_min = int(n)
n_max = n_min + 1
diff_n_min = abs(t - avg - (h - c) / (2 * (2 * n_min - 1)))
diff_n_max = abs(t - avg - (h - c) / (2 * (2 * n_max - 1)))
if diff_n_min <= diff_n_max:
print(2 * n_min - 1)
else:
print(2 * n_max - 1)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int tc;
scanf("%d", &tc);
for (int _ = 0; _ < int(tc); _++) {
int h, c, t;
scanf("%d%d%d", &h, &c, &t);
if (h + c - 2 * t >= 0)
puts("2");
else {
int a = h - t;
int b = 2 * t - c - h;
int k = 2 * (a / b) + 1;
long long val1 =
abs(k / 2 * 1ll * c + (k + 1) / 2 * 1ll * h - t * 1ll * k);
long long val2 = abs((k + 2) / 2 * 1ll * c + (k + 3) / 2 * 1ll * h -
t * 1ll * (k + 2));
printf("%d\n", val1 * (k + 2) <= val2 * k ? k : k + 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 | #include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y);
long long int gcd(long long int a, long long int b);
vector<long long int> ft = {1};
long long int modInv(long long int i) { return power(i, 1000000007 - 2); }
long long int ncr(long long int n, long long int r) {
return (n >= r ? (ft[n] * modInv(ft[r])) % 1000000007 * modInv(ft[n - r]) %
1000000007
: 0);
}
long long int power(long long int x, long long int y) {
long long int res = 1;
x;
while (y > 0) {
if (y & 1) res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int h, c, tb;
double cal1(long long int n) {
double cur = n * (h + c);
cur /= 2 * n;
return abs(cur - tb);
}
double cal2(long long int n) {
double cur = n * h + (n - 1) * c;
cur /= (2 * n - 1);
return abs(cur - tb);
}
long long int ternary1(long long int l, long long int r) {
while (l < r - 2) {
long long int mid1 = l + (r - l) / 3;
long long int mid2 = r - (r - l) / 3;
if (cal1(mid1) <= cal1(mid2))
r = mid2;
else
l = mid1;
}
double val = 1e9;
long long int ind = l;
for (long long int i = l; i < r + 1; i++) {
if (cal1(i) < val) {
ind = i;
val = cal1(i);
}
}
return ind;
}
long long int ternary2(long long int l, long long int r) {
while (l < r - 2) {
long long int mid1 = l + (r - l) / 3;
long long int mid2 = r - (r - l) / 3;
if (cal2(mid1) <= cal2(mid2))
r = mid2;
else
l = mid1;
}
double val = 1e9;
long long int ind = l;
for (long long int i = l; i < r + 1; i++) {
if (cal2(i) < val) {
ind = i;
val = cal2(i);
}
}
return ind;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
cin >> h >> c >> tb;
long long int cnt1 = ternary1(1, 1e12);
long long int cnt2 = ternary2(1, 1e12);
if (cal1(cnt1) < cal2(cnt2))
cout << 2 * cnt1 << "\n";
else
cout << 2 * cnt2 - 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 | #!/bin/python3
# Not sure why people get so many failed submissions. Numerical error?
from fractions import Fraction
for _ in range(int(input())):
hot, cold, temp=map(int, input().split())
if temp>=hot: print(1); continue
if temp*2<=hot+cold: print(2); continue
x=int(1/((temp-Fraction(hot+cold)/2)/(hot-cold))/2)
if x<1: x=1
if x%2==0: x-=1
print(min((x, x+2),
key=lambda x: abs(Fraction(hot+cold)/2+Fraction(hot-cold)/(2*x)-temp))
)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t 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
input = sys.stdin.readline
for f in range(int(input())):
h,c,t=map(int,input().split())
if t==h:
print(1)
else:
if t<=(h+c)/2:
print(2)
else:
diff=t-(h+c)/2
k1=math.floor(((h-c)/(2*diff)-1)/2)
k2=k1+1
d1=abs(diff-(h-c)/(4*k1+2))
d2=abs(diff-(h-c)/(4*k2+2))
if d2<d1:
print(2*k2+1)
else:
print(2*k1+1) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from math import floor, ceil
tests = int(input())
for _ in range(tests):
h,c,t = map(int,input().split())
if 2*t <= h + c:
print(2)
else:
m = (h-t)/(2*t-h-c)
if m == 0:
print(1)
else:
m1 = ceil(m)
m2 = floor(m)
if abs(((m2+1)*h+m2*c)-(2*m2+1)*t) < abs(((m1+1)*h+m1*c)-(2*m1+1)*t):
if abs(((m2+1)*h+m2*c)/(2*m2+1)-t) <= abs(((m1+1)*h+m1*c)/(2*m1+1)-t):
print(2*m2+1)
else:
print(2*m1+1)
else:
print(2*m1+1) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #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());
}
}
| 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
[t] = [int(i) for i in sys.stdin.readline().split()]
for _ in range(t):
[h, c, t] = [int(i) for i in sys.stdin.readline().split()]
h -= c
t -= c
# if t <= h/2:
if 2*t <= h:
print(2)
else:
# k = 1/(2-h/t)
# k = t/(2t-h)
k1 = t // (2*t-h)
k2 = k1 + 1
# under common denominator of (2k1-1)(2k2-1)
t1 = h * k1 * (2*k2-1)
diff1 = abs(t*(2*k1-1)*(2*k2-1) - t1)
t2 = h * k2 * (2*k1-1)
diff2 = abs(t*(2*k1-1)*(2*k2-1) - t2)
if diff1 <= diff2:
print(2*k1 - 1)
else:
print(2*k2 - 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
"""
infinite water:
h = hot water
c = cold water (c < h)
"""
from fractions import Fraction
from math import floor, ceil
Decimal = Fraction
def calc_temp(cups, h, c):
# cups = total cups
if cups <= 0:
return 0
if cups % 2 == 0:
return ((h * (cups/2) + c * (cups/2))) / cups
else:
return Decimal(((h * (cups//2 + 1) + c * (cups//2)))) / Decimal(cups)
def solve(h, c, t):
if t >= h:
return 1
elif t <= (h+c)/2:
return 2
else:
x = (c-t)/(h+c-2*t)
#print(x)
lower = max(floor(x), 1)
upper = ceil(x) + 2
best_abs = float("inf")
best_cups = None
t = Decimal(t)
for i in range(lower, upper):
t_b = calc_temp(2*i-1, h, c)
diff = abs(t_b-t)
if diff < best_abs:
best_abs = diff
best_cups = 2*i-1
return best_cups
res = []
t = int(input())
for _ in range(t):
h, c, t = map(int, input().split())
res.append(solve(h, c, t))
for r in res:
print(r)
"""
for i in range(1, 100):
print(calc_temp(i, 30, 20))
""" | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | //package main;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Submission {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter (System.out);
//BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
int q = Integer.parseInt(reader.readLine());
SortedSet<Fraction> s = new TreeSet<>();
HashMap<Fraction,HashSet<Integer>> map = new HashMap<>();
for (int i=0;i<q;i++) {
StringTokenizer st = new StringTokenizer(reader.readLine()," ");
int h = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
Fraction v = new Fraction((t-c),(h-c));
s.add(v);
if (!map.containsKey(v)) map.put(v,new HashSet<>());
map.get(v).add(i);
}
long[] answers = new long[q];
for (Fraction d : s.headSet(new Fraction(1,2))) {
for (int e : map.get(d)) answers[e]=2;
}
SortedSet<Fraction> w = new TreeSet<>(s.headSet(new Fraction(1,2)));
s.removeAll(w);
if (s.contains(new Fraction(1,2))) {
for (int e : map.get(new Fraction(1,2))) answers[e]=2;
s.remove(new Fraction(1,2));
}
long count=1;
while (!s.isEmpty()) {
SortedSet<Fraction> u = new TreeSet<>(s.tailSet(new Fraction(4*count*count+2*count-1,8*count*count-2)));
for (Fraction d : u) {
for (int e : map.get(d)) answers[e]=2*count-1;
}
s.removeAll(u);
count++;
}
for (int i=0;i<q;i++) out.println(answers[i]);
reader.close();
out.close();
}
}
class Fraction implements Comparable<Fraction> {
long num;
long den;
public Fraction (long a, long b) {
BigInteger numb= BigInteger.valueOf(a);
BigInteger denb=BigInteger.valueOf(b);
long x = Math.max(1,Math.abs(numb.gcd(denb).longValue()));
this.num=a/x;
this.den=b/x;
}
public int compareTo (Fraction f) {
long j = (long)num*(long)f.den-(long)den*(long)f.num;
if (j>0) return 1;
else if (j==0) return 0;
else return -1;
}
@Override
public boolean equals (Object f) {
if (!(f instanceof Fraction)) return false;
else {
Fraction g = (Fraction) f;
if (num==g.num&&den==g.den) return true;
else return false;
}
}
public String toString () {
return num+"/"+den;
}
@Override
public int hashCode() {
return (int)num*1000000+(int)den;
}
} | JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from sys import stdin
input=stdin.readline
from collections import deque
R=lambda:map(int,input().split())
I=lambda:int(input())
S=lambda:input().rstrip('\n')
for _ in range(I()):
h,c,t=R()
if (h+c)/2>=t:print(2);continue
k=int((t-h)/(h+c-2*t))
print(2*k+1 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) else 2*k+3) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void solve() {
double x, y, t;
cin >> x >> y >> t;
if (t == x) {
cout << 1 << endl;
return;
}
if ((x + y) >= 2 * t) {
cout << 2 << endl;
return;
}
int n = (t - y) / ((2.0) * t - x - y);
int n_ = n + 1;
double diff1 = abs(t - ((double)n * x + (double)(n - 1.0) * y) /
((double)(2 * n - 1.0)));
double diff2 = abs(t - ((double)n_ * x + (double)(n_ - 1.0) * y) /
((double)(2 * n_ - 1.0)));
if (diff1 <= diff2) {
cout << (2 * n - 1) << endl;
} else {
cout << (2 * n_ - 1) << endl;
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.*;
import java.io.*;
public class Main{
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
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;
}
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 double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
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 boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String[] args)
{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int te=sc.nextInt();
while(te-->0)
{
int h = sc.nextInt();
int c = sc.nextInt();
int t = sc.nextInt();
if((h+c)>=2*t) w.println(2);
else{
int n1 = (int)Math.floor((double)(t-c)/(double)(2*t-(h+c)));
int n2 = (int)Math.ceil((double)(t-c)/(double)(2*t-(h+c)));
if(n1<1&&n2<1){
System.out.println(2);
continue;
}
long num1 = Math.abs((t*(2*n1-1))-h*n1-c*(n1-1));
long den1 = (2*n1-1);
long num2 = Math.abs((t*(2*n2-1))-h*n2-c*(n2-1));
long den2 = (2*n2-1);
if(num1*den2<=num2*den1)
w.println(2*n1-1);
else
w.println(2*n2-1);
}
}
w.close();
}
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long logN = 20;
const long long M = 1000000007;
const long long INF = 1e18;
void solve() {
long long h, c, t;
cin >> h >> c >> t;
if (h == t)
cout << 1;
else if (2 * t <= (h + c))
cout << 2;
else {
long double p = (long long)(h - t) / (2 * t - h - c);
long double p1 = (long double)(h + (h + c) * p) / (2 * p + 1);
long long pp = p + 1;
long double p2 = (long double)(h + (h + c) * pp) / (2 * pp + 1);
if (abs((long double)t - p1) <= abs((long double)t - p2))
cout << 2 * p + 1;
else
cout << 2 * pp + 1;
}
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.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 | for _ in range(int(input())):
h,c,t=map(int,input().split())
if t-h==0:
print(1)
elif 2*t<=(h+c):
print(2)
else:
k=(h-t)//(2*t-(h+c))
if abs((k+1)*h+k*c-t*(2*k+1))*abs(2*k+3)<=abs((k+2)*h+(k+1)*c-t*(2*k+3))*abs(2*k+1):
print(2*k+1)
else:
print(2*k+3)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
double h, c, t;
double f(double i) { return (c * (i - 1) + h * (i)) / (2 * i - 1); }
void solve() {
cin >> h >> c >> t;
if (t >= h) {
cout << 1 << endl;
return;
}
if (t <= (h + c) / 2) {
cout << 2 << endl;
return;
}
ll low = 1;
ll len = 1ll << 38;
ll steps;
while (len >= 1) {
ll mid = low + len / 2;
double cavg = f(mid);
if (cavg >= t) {
steps = mid;
low = mid;
}
len /= 2;
}
ll ans = 2;
double avg = (h + c) / 2.0;
for (int i = 0; i <= 100; ++i) {
double cavg = f(steps + i);
if (fabs(t - cavg) < fabs(t - avg)) {
ans = steps + i;
avg = cavg;
}
}
cout << 2 * ans - 1 << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.io.*;
import java.util.*;
final public class C {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int tst = Integer.parseInt(br.readLine());
while(tst-->0){
String[] str = br.readLine().split(" ");
int h = Integer.parseInt(str[0]), c = Integer.parseInt(str[1]), t = Integer.parseInt(str[2]);
if((h+c)/2 >= t) sb.append(2).append('\n');
else{
//System.err.println(h+" "+c);
long k = (long)((double)(t-h)/(double)(h+c-2*t));
double tmp1 = ((k+1)*h+k*c)/(double)(2*k+1);
double tmp2 = ((k+2)*h+(k+1)*c)/(double)(2*(k+1)+1);
long ans;
if((2*k+3)*Math.abs((k+1)*h + k*c - (2*k+1)*t)<=(2*k+1)*Math.abs((k+2)*h + (k+1)*c - (2*k+3)*t)){
ans = k;
}
else ans = k+1;
sb.append(2*ans+1).append('\n');
}
}
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 sys
from collections import defaultdict as dd
from collections import deque
from fractions import Fraction as f
def eprint(*args):
print(*args, file=sys.stderr)
zz=1
from math import *
import copy
#sys.setrecursionlimit(10**6)
if zz:
input=sys.stdin.readline
else:
sys.stdin=open('input.txt', 'r')
sys.stdout=open('all.txt','w')
def li():
return [int(x) for x in input().split()]
def fi():
return int(input())
def si():
return list(input().rstrip())
def mi():
return map(int,input().split())
def bo(i):
return ord(i)-ord('a')
tt=fi()
while tt>0:
tt-=1
h,c,t=mi()
if t==h:
print(1)
continue
ans=1
if abs(h-t)<=abs((h+c)/2-t):
ans=1
res=f(h-t)
else:
ans=2
res=abs(f((h+c),2)-f(t))
#print(ans,res)
#print(ans)
if 1:
l=1
r=10**6+1
while l<=r:
mid=l+(r-l)//2
z=f(h*mid+c*(mid-1),2*mid-1)
if z<=t:
if abs(z-t)<res:
res=abs(z-t)
ans=2*mid-1
#print(z,res,t,2*mid+1)
elif abs(z-t)==res:
ans=min(ans,2*mid-1)
r=mid-1
else:
l=mid+1
if abs(z-t)<res:
#print(z,res,t,2*mid+1)
res=abs(z-t)
ans=2*mid-1
elif abs(z-t)==res:
ans=min(ans,2*mid-1)
#print(l,r,ans,res)
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;
long long int n, T, x, y, z;
int main() {
ios::sync_with_stdio(false);
cin >> T;
while (T--) {
cin >> x >> y >> z;
if (x <= z)
cout << 1 << endl;
else if ((float)(x + y) / 2 >= z)
cout << 2 << endl;
else {
long long int lb = 1, rb = 1000000000, m, X;
while (lb < rb) {
m = (lb + rb - 1) / 2;
if (z >= (float)(m * (x + y) + x) / (2 * m + 1))
rb = m;
else
lb = m + 1;
}
X = 2 * rb + 1;
float Y = ((float)rb * (x + y) + x) / (2 * rb + 1);
lb = 0, rb = 1000000000;
while (lb < rb) {
m = (lb + rb + 1) / 2;
if (z <= (float)(m * (x + y) + x) / (2 * m + 1))
lb = m;
else
rb = m - 1;
}
if (fabs((float)z - Y) <
fabs((float)z - (float)(rb * (x + y) + x) / (2 * rb + 1)))
cout << X << endl;
else
cout << 2 * rb + 1 << endl;
}
}
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from sys import stdin, stdout
import math
t = int(stdin.readline())
for _ in range(t):
h, c, t = map(int, stdin.readline().split())
if 3*(2*t-h) >= 2*h+c:
print(1)
continue
if 2*t <= (h+c):
print(2)
continue
n = (h-t)//(2*t-h-c)
a1, b1 = (h*(n+1)+c*n), 2*n+1
a2, b2 = (h*(n+2)+c*(n+1)), 2*n+3
if b2*abs(a1-b1*t) <= b1*abs(a2-b2*t):
print(2*n+1)
else:
print(2*n+3)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
bool equal(double a, double b) { return abs(a - b) < 1e-13; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int ntest;
cin >> ntest;
while (ntest--) {
long long c, h, x, y, t, ans = 2;
cin >> h >> c >> t;
long double minDif = abs((long double)(h + c) / 2 - t);
x = double(t - h) / (h + c - 2 * t);
y = (x <= 0 ? 0 : x) + 10;
for (long long i = max(0LL, x - 10); i <= y; i++) {
long double dif = abs((long double)(i * (h + c) + h) / (2 * i + 1) - t);
if (minDif >= dif) {
long long cup = i * 2 + 1;
if (minDif == dif)
ans = min(ans, cup);
else
ans = cup;
minDif = dif;
}
}
cout << ans << "\n";
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
def gift():
for _ in range(t):
h,c,tem = list(map(int,input().split()))
if h==tem:
yield 1
elif (h+c)/2>tem:
mid=(h+c)/2
edif=abs(h-c)
ldif=abs(mid-c)
if edif>=ldif:
yield 2
else:
yield 1
elif (h+c)/2==tem:
yield 2
else:
dif= tem-(h+c)/2
posans=((h-c)/2)//dif
bibi=((h-c)/2)%dif
if posans%2==0:
posans+=1
oposans=posans+2
pdif=abs(tem-((h+c)/2+(h-c)/2/posans))
edif=abs(tem-((h+c)/2+(h-c)/2/oposans))
if oposans>=3:
kposans=posans-2
kdif=abs(tem-((h+c)/2+(h-c)/2/kposans))
mindif=min(pdif,edif,kdif)
if mindif==pdif:
yield int(posans)
elif mindif==edif:
yield int(oposans)
else:
yield int(kposans)
else:
#print(pdif,edif,posans,oposans)
if pdif<edif:
yield int(posans)
else:
yield int(oposans)
if __name__ == '__main__':
t= int(input())
ans = gift()
print(*ans,sep='\n')
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long test;
cin >> test;
while (test--) {
long long h, c, t;
cin >> h >> c >> t;
long double mini = (h + c) / 2.0;
if (t <= mini) {
cout << 2 << '\n';
} else {
long long start = 1, end = 1000000000, mid;
long double res;
vector<long long> v;
std::vector<long double> v1;
while (start <= end) {
mid = (start + end) / 2;
long double cur =
(h * mid + c * (mid - 1)) / (long double)(2 * mid - 1);
res = cur;
v1.push_back(cur);
v.push_back(mid);
if (cur > t) {
start = mid + 1;
} else {
end = mid - 1;
}
}
long double diff = 10000000;
long long ans = 1000000000;
for (int i = 0; i < (long long)v.size(); ++i) {
if (abs(v1[i] - t) < diff) {
diff = abs(v1[i] - t);
ans = v[i];
} else if (abs(v1[i] - t) == diff) {
diff = abs(v1[i] - t);
ans = min(ans, v[i]);
}
}
cout << 2 * ans - 1 << '\n';
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.util.*;
import java.io.*;
public class C1359 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static StringTokenizer st;
public static void main(String[]args)throws IOException {
int t = readInt();
loop:
while(t--!=0) {
int h = readInt(); int c = readInt(); int temp = readInt();
int sum = h+c;
if(h==temp) {
pr.println(1); continue;
}
if(sum==temp*2) {
pr.println(2); continue;
}
if((double)sum/2>temp) {
pr.println(2); continue;
}
double close = 0;
long times = 0;
if(h>temp & (double)sum/2<temp) {
long low = 0;
long high = (long)1e9;
long mid = (low+high)/2;
while(low<high-1) {
double ans = (h+(double)sum*mid)/(2*mid+1);
if(Math.abs(ans-temp)<=0.000000000000001) {
pr.println(2*mid+1);
continue loop;
}
if(ans-temp>0.000000000000001) {
low = mid;
}
else {
high = mid;
}
mid = (low+high)/2;
}
double s1 = (h+(double)sum*low)/((long)2*low+1);
double s2 = (h+(double)sum*high)/((long)2*high+1);
if(Math.abs(s1-temp)<=Math.abs(s2-temp)+0.000000000000001) {
close = Math.abs(s1-temp);
times = 2*low+1;
}
else {
close = Math.abs(s2-temp);
times = 2*high+1;
}
if(low>6)
for(int i = -5; i<=5; i++) {
double an = (h+(double)sum*(low+i))/(2*(low+i)+1);
if(Math.abs(an-temp)<=close+0.000000000000001) {
close = Math.abs(an-temp);
times = 2*(low+i)+1;
}
}
pr.println(times);
/*
if(Math.abs((double)sum/2-temp)<=close+0.00000000000001) {
if(h-temp<=Math.abs((double)sum/2-temp)+0.00000000001) {
pr.println(1);
}
else {
pr.println(2);
}
continue;
}
else {
pr.println(times);
continue;
}
*/
}
}
pr.close();
}
static String next () throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}
static long readLong () throws IOException {
return Long.parseLong(next());
}
static int readInt () throws IOException {
return Integer.parseInt(next());
}
static double readDouble () throws IOException {
return Double.parseDouble(next());
}
static char readCharacter () throws IOException {
return next().charAt(0);
}
static String readLine () throws IOException {
return br.readLine().trim();
}
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import math
from decimal import *
T= int(input())
for i in range(T):
h,c,t= map(int,input().split())
a=h-t
b=2*t-h-c
if b<=0:
print(2)
else:
e=math.ceil(a/b)
r=abs(Decimal(((h+c)*e+h))/Decimal((2*e+1)) - t)
r1=abs(Decimal((h+c))/2 - t)
e1=e+1
e2=e-1
r2=abs(Decimal(((h+c)*e1+h))/Decimal((2*e1+1)) - t)
r3=abs(Decimal(((h+c)*e2+h))/Decimal((2*e2+1)) - t)
a=[(r,e),(r2,e1),(r3,e2)]
a.sort()
q=a[0][0]
if q<r1:
print(2*a[0][1]+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 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
double h, c, t;
cin >> h >> c >> t;
if ((h + c) / 2 >= t)
cout << "2" << endl;
else {
int a = (t - c) / (2 * t - h - c);
double A = ((h + c) * a - c) / (2 * a - 1) - t;
double B = ((h + c) * a + h) / (2 * a + 1) - t;
if (abs(A) > abs(B))
cout << 2 * a + 1 << endl;
else
cout << 2 * a - 1 << endl;
}
}
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | T = int(input())
import math
for _ in range(T):
h,c,t = map(int,input().split())
ans = 1145141919810
av = (h+c)/2
if av >= t:
print(2)
elif t == h:
print(1)
else:
an = h-av
bn = t-av
x = math.ceil(an / bn)
aa = x//2 * 2 + 1
bb = x//2 * 2 - 1
ta = an/aa
tb = an/bb
tc = t-av
if abs(ta-tc) >= abs(tb-tc):
print(bb)
else:
print(aa) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t 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<vector<int>> comp;
vector<int> onecycle;
vector<int> clength;
vector<int> v9;
vector<int> v8;
int cycle_start;
int cycle_end;
vector<int> vbip1;
vector<int> vbip2;
void addedge(int u, int v, vector<int> adj[]) {
adj[u].push_back(v);
adj[v].push_back(u);
}
void dfsforconnectedcomponents(int s, vector<int> adj[], int V, int u[],
int &count) {
v9.push_back(s);
u[s] = 1;
int i;
for (i = 0; i < adj[s].size(); i++) {
if (u[adj[s][i]] == 0) {
count++;
dfsforconnectedcomponents(adj[s][i], adj, V, u, count);
}
}
}
void connectedcomponents(int V, vector<int> adj[], int u[]) {
int i;
for (i = 1; i < V + 1; i++) {
if (u[i] == 0) {
int count = 1;
dfsforconnectedcomponents(i, adj, V, u, count);
comp.push_back(v9);
v9.clear();
}
}
}
bool dfs1foronecycle(int s, int V, int color[], vector<int> adj[], int p[]) {
color[s] = 1;
int i;
for (i = 0; i < adj[s].size(); i++) {
if (color[adj[s][i]] == 0) {
p[adj[s][i]] = s;
if (dfs1foronecycle(adj[s][i], V, color, adj, p)) {
return true;
}
} else if (color[adj[s][i]] == 1 && p[s] != adj[s][i]) {
cycle_start = adj[s][i];
cycle_end = s;
return true;
}
}
color[s] = 2;
return false;
}
void cyclesusingdfs1(int V, vector<int> adj[], int color[], int p[]) {
int i;
for (i = 1; i < V + 1; i++) {
if (color[i] == 0) {
if (dfs1foronecycle(i, V, color, adj, p)) {
int count = 1;
i = cycle_end;
while (i != cycle_start) {
i = p[i];
count++;
}
clength.push_back(count);
count = 0;
}
}
}
}
bool bfsbipartite(vector<int> adj[], int V) {
int u[V + 1];
int i;
for (i = 0; i < V + 1; i++) {
u[i] = 0;
}
int color[V + 1];
for (i = 0; i < V + 1; i++) {
color[i] = -1;
}
int s;
for (s = 1; s < V + 1; s++) {
if (color[s] == -1) {
u[s] = 1;
color[s] = 1;
queue<int> q;
q.push(s);
while (!q.empty()) {
int z = q.front();
q.pop();
for (i = 0; i < adj[z].size(); i++) {
int k = adj[z][i];
if (color[k] == -1) {
color[k] = 1 - color[z];
u[k] = 1;
q.push(k);
}
}
}
}
}
int flag[V + 1];
for (i = 0; i < V + 1; i++) {
flag[i] = 0;
}
for (i = 1; i < V + 1; i++) {
for (int j = 0; j < adj[i].size(); j++) {
if (color[i] == color[adj[i][j]]) {
return false;
}
if (color[adj[i][j]] == 1 && flag[adj[i][j]] == 0) {
flag[adj[i][j]] = 1;
vbip1.push_back(adj[i][j]);
}
if (color[adj[i][j]] == 0 && flag[adj[i][j]] == 0) {
flag[adj[i][j]] = 1;
vbip2.push_back(adj[i][j]);
}
}
}
return true;
}
void bfs1(int V, int s, vector<int> adj[], int d[], int anc[], int u[]) {
int i;
d[s] = 0;
u[s] = 1;
anc[s] = -1;
v8.push_back(s);
queue<int> q;
q.push(s);
vector<int> v;
while (!q.empty()) {
int z = q.front();
q.pop();
for (i = 0; i < adj[z].size(); i++) {
int k = adj[z][i];
if (u[adj[z][i]] == 0) {
anc[z] = adj[z][i];
d[k] = d[z] + 1;
anc[k] = z;
u[k] = 1;
q.push(k);
}
}
}
}
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int binarySearch(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return l;
}
string bin(long long x, long long n) {
string s = "";
while (x != 0) {
if (x % 2 == 0) {
s.push_back('0');
} else {
s.push_back('1');
}
x = x / 2;
}
int l = s.length();
int i;
for (i = 0; i < n - l; i++) {
s.push_back('0');
}
reverse(s.begin(), s.end());
return s;
}
vector<long long> prime(int n) {
int prime[n + 1];
for (int i = 0; i < n + 1; i++) prime[i] = 1;
for (int p = 2; p * p <= n + 1; p++) {
if (prime[p] == 1) {
for (int i = p * p; i <= n + 1; i += p) prime[i] = 0;
}
}
vector<long long> v;
long long i;
for (i = 2; i < n + 1; i++) {
if (prime[i] == 1) {
v.push_back(i);
}
}
return v;
}
int fun(int x, int y, int n) { return x * n + y + 1; }
int fun1(int arr[], int n) {
int fw[n], bw[n];
int cur_max = arr[0], max_so_far = arr[0];
fw[0] = arr[0];
for (int i = 1; i < n; i++) {
cur_max = max(arr[i], cur_max + arr[i]);
max_so_far = max(max_so_far, cur_max);
fw[i] = cur_max;
}
cur_max = max_so_far = bw[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--) {
cur_max = max(arr[i], cur_max + arr[i]);
max_so_far = max(max_so_far, cur_max);
bw[i] = cur_max;
}
int fans = max_so_far;
for (int i = 1; i < n - 1; i++) fans = max(fans, fw[i - 1] + bw[i + 1]);
return fans;
}
int main() {
long long t;
cin >> t;
while (t--) {
double h, c, t1;
cin >> h >> c >> t1;
h = h * 1.0;
c = c * 1.0;
double x = (h + c) / 2 * 1.0;
if (t1 == h) {
cout << 1 << endl;
} else if (t1 > x) {
long long z1 = 2 * t1 - (h + c);
long long z2 = t1 - c;
if (z2 % z1 == 0) {
long long z = z2 / z1;
cout << 2 * z - 1 << endl;
} else {
long long x1 = z2 / z1;
long long x2 = z2 / z1 + 1;
long long z;
double a1 = (h * x1 * 1.0 + c * (x1 - 1) * 1.0) / (2 * x1 - 1) * 1.0;
double a2 = (h * x2 * 1.0 + c * (x2 - 1) * 1.0) / (2 * x2 - 1) * 1.0;
double z3, z4;
if (a1 - t1 > 0) {
z3 = a1 - t1;
} else {
z3 = t1 - a1;
}
if (a2 - t1 > 0) {
z4 = a2 - t1;
} else {
z4 = t1 - a2;
}
if (z3 > z4) {
z = x2;
} else {
z = x1;
}
cout << 2 * z - 1 << endl;
}
} else {
cout << 2 << endl;
}
}
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import sys
input = sys.stdin.readline
import bisect
test = int(input())
for _ in range(test):
h,c,t = map(int,input().split())
if t*2 <= (h+c):
print(2)
continue
l = 0
r = 100000000
while l+1 < r:
x = (l+r)//2
if (h*(x+1)+c*x) < t*(2*x+1):
r = x
else:
l = x
if abs(t*(2*l+1)*(2*r+1)-(h*(l+1)+c*l)*(2*r+1)) <= abs(t*(2*l+1)*(2*r+1)-(h*(r+1)+c*r)*(2*l+1)):
print(2*l+1)
else:
print(2*r+1) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
def main():
for _ in range(II()):
h,c,t=MI()
if t>=h:
print(1)
continue
if t*2<=c+h:
print(2)
continue
a1=(-t+h)//(-h-c+2*t)
a2=a1+1
#print(a1,a2,(t-h)/(h+c-2*t))
if 2*t*(2*a2+1)*(2*a1+1)-(h*(a2+1)+c*a2)*(2*a1+1)<(2*a2+1)*(h*(a1+1)+c*a1):ans = a2 * 2 + 1
else: ans = a1 * 2 + 1
print(ans)
main() | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from math import *
def solve(h, c, t):
if t * 2 <= (h + c): return 2
ans = 1
x = (c-t)//(h+c-2*t)
def den(x): return abs((2*x-1)*t-x*h-(x-1)*c)
def num(x): return 2*x-1
for i in range(x-5, x+5):
if i <= 0: continue
if den(i)*num(ans) < den(ans)*num(i): ans = i
#if floor(x) > 0 and f(floor(x)) < f(ans): ans = floor(x)
return 2 * ans - 1
def main():
t = int(input())
for _ in range(t):
print(solve(*map(int, input().split())))
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.*;
import java.util.*;
public class MixingWater {
public static void main(String[] args) throws Exception {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int T = sc.nextInt();
while (T-->0) {
long h = sc.nextLong();
long c = sc.nextLong();
long t = sc.nextLong();
if (2 * t <= (c + h)) {
out.println(2);
}
else {
int lo = 0;
int hi = (int) 1e9;
while (lo < hi) {
// find last position that's positive (try that and also +1)
// 1100
int mid = (lo + hi + 1) / 2;
if (calcNum(h, c, mid) >= calcDenom(mid) * t) {
lo = mid;
}
else {
hi = mid - 1;
}
}
int a = lo;
// try here and next
int curr = a;
int next = a + 1;
long denom1 = calcDenom(curr);
long num1 = calcNum(h, c, curr) - t * denom1;
long denom2 = calcDenom(next);
long num2 = t * denom2 - calcNum(h, c, next);
out.println(num1 * denom2 <= num2 * denom1 ? 2 * curr + 1 : 2 * next + 1);
}
}
out.close();
}
static long calcNum(long h, long c, long a) {
return ((a + 1) * h + a * c);
}
static long calcDenom(long a) {
return 2 * a + 1;
}
static void generate() {
int h = (int) (200 * Math.random());
int c = (int) (h * Math.random());
int t = (int) (((h + c) / 2) + (h - (h + c) / 2) * Math.random());
System.out.println(h + " " + c + " " + t);
}
static class FastScanner {
private int BS = 1<<16;
private char NC = (char)0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt=1;
boolean neg = false;
if(c==NC)c=getChar();
for(;(c<'0' || c>'9'); c = getChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=getChar()) {
res = (res<<3)+(res<<1)+c-'0';
cnt*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=getChar();
while(c>32) {
res.append(c);
c=getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=getChar();
while(c!='\n') {
res.append(c);
c=getChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=getChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
}
static void ASSERT(boolean assertion, String message) {
if (!assertion) throw new AssertionError(message);
}
} | JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int solve(long long int h, long long int c, long long int t) {
if (h == t) return 1;
if ((h + c) / 2.0 >= t) return 2;
long long int x = (h - t) / (2 * t - h - c);
long long int y = x + 1;
long double tx = (long double)(x * (h + c) + h) / (long double)(2 * x + 1);
long double ty = (long double)(y * (h + c) + h) / (long double)(2 * y + 1);
if (abs(tx - t) <= abs(t - ty))
return 2 * x + 1;
else
return 2 * y + 1;
}
int main() {
int N;
cin >> N;
while (N--) {
long long int h, c, t;
cin >> h >> c >> t;
cout << solve(h, c, t) << 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:
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+1)+1)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from math import floor,ceil
for _ in range(int(input())):
h,c,t = map(int,input().split())
if t > (h+c)/2:
k1 = floor((t-h)/(h+c-2*t))
k2 = ceil((t-h)/(h+c-2*t))
if abs(t*(2*k1+1)*(2*k2+1)-((k1+1)*h+k1*c)*(2*k2+1)) > abs(t*(2*k1+1)*(2*k2+1)-((k2+1)*h+k2*c)*(2*k1+1)):
print(2*k2+1)
else:
print(2*k1+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 | from math import *
# from collections import Counter,defaultdict,deque
# from bisect import bisect_left as bl, bisect_right as br
# from itertools import accumulate,permutations
# import heapq
# from functools import reduce
from io import BytesIO, IOBase
import sys
import os
# IO region
def Iint(): return int(input())
def Ilist(): return list(map(int, input().split())) # int list
def Imap(): return map(int, input().split()) # int map
def Plist(li, s=' '): # non string list
print(s.join(map(str, li)))
# /IO region
def main():
t = Iint()
for _ in range(t):
h, c, t = Imap()
mid = (h+c)/2
if t <= mid:
print(2)
continue
x1 = (h-t)//(2*t-h-c)
x2 = x1+1
t1 = ((x1+1)*h+(x1*c))*(2*x2+1)
t2 = ((x2+1)*h+(x2*c))*(2*x1+1)
t *= (2*x2+1)*(2*x1+1)
if abs(t1-t)-abs(t2-t) > 0:
print(2*x2+1)
else:
print(2*x1+1)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip("\r\n")
if __name__ == '__main__':
main()
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class CE88C {
public static void main(String[] args) throws NumberFormatException, IOException {
FastReader sc=new FastReader();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
for(int tt=0; tt<t; tt++) {
int h = sc.nextInt();
int c = sc.nextInt();
int temp = sc.nextInt();
h -= c;
temp -= c;
c = 0;
if (temp == 0 || h/temp >= 2) {
out.println(2);
continue;
}
//double e = 1e-4;
Fraction p = new Fraction(temp, h);
Fraction extra = p.subtract(new Fraction(1,2));
Fraction recip = new Fraction(extra.denominator, extra.numerator);
Fraction index = (recip.add(new Fraction(2,1))).multiply(new Fraction(1,4));
//long round = Math.round(index);
if (index.denominator == 1l) {
out.println(index.numerator * 2 - 1);
continue;
}
double floor = Math.floor((double)index.numerator / (double)index.denominator);
double ceil = Math.ceil((double)index.numerator / (double)index.denominator);
long df = Math.round(floor)*4 - 2;
long dc = Math.round(ceil)*4 - 2;
Fraction avg = ((new Fraction(2,1).divide(new Fraction(1,df).add(new Fraction(1,dc)))).add(new Fraction(2,1))).multiply(new Fraction(1,4));
// out.println(index);
// out.println(floor);
// out.println(ceil);
// out.println(df);
// out.println(dc);
// out.println(avg);
if (index.numerator * avg.denominator > index.denominator * avg.numerator) {
out.println(Math.round(ceil) * 2 - 1);
} else {
out.println(Math.round(floor) * 2 - 1);
}
}
out.flush();
}
/**********************************************************
Fraction.java - a Java representation of a fraction
Author: Diane Kramer
History:
Created: 9/25/01
Modified: 10/16/01 - added gcd method to reduce fraction
Modified: 02/19/06 - include licence terms in comments
Description: This class provides storage for internal
representation of, and methods to manipulate fractions.
A fraction consists of two integers, one for numerator
and one for denominator. An example fraction is 3/4.
A valid fraction must not have zero in the denominator.
This software is licensed "as-is" under a non-exclusive,
worldwide, royalty-free right to reproduce the software,
prepare derivative works of the software and distribute
the software or any derivative works created. The user
bears the risk of using it. No express warranties,
guarantees or conditions are implied.
***********************************************************/
static class FastReader{BufferedReader br;StringTokenizer st;public FastReader()
{br=new BufferedReader(new InputStreamReader(System.in), 32768);}
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;}}
}
class Fraction
{
// member variables
public long numerator, denominator; // stores the fraction data
/**********************************************************
Method: Default Constructor
Purpose: Create a new Fraction object and initialize it
with "invalid" data
Parameters: None
Preconditions: None
Postconditions: a new fraction object is created with numerator
and denominator set to 0
***********************************************************/
public Fraction()
{
numerator = denominator = 0;
}
public Fraction(long n, long d) {
numerator = n;
denominator = d;
}
/********************************************/
/* public accessor methods for private data */
/********************************************/
/**********************************************************
Method: getNumerator
Purpose: access data stored in numerator member variable
Parameters: None
Preconditions: None
Postconditions: None
Returns: integer data stored in numerator member variable
***********************************************************/
public long getNumerator()
{
return numerator;
}
/**********************************************************
Method: setNumerator
Purpose: provide data to store in numerator member variable
Parameters: num: an integer value
Preconditions: None
Postconditions: the value of num will be stored in numerator
member variable
***********************************************************/
public void setNumerator(long num)
{
numerator=num;
}
/**********************************************************
Method: getDenominator
Purpose: access data stored in denominator member variable
Parameters: None
Preconditions: None
Postconditions: None
Returns: integer data stored in denominator member variable
***********************************************************/
public long getDenominator()
{
return denominator;
}
/**********************************************************
Method: setDenominator
Purpose: provide data to store in denominator member variable
Parameters: den: an integer value
Preconditions: None
Postconditions: the value of den will be stored in denominator
member variable
***********************************************************/
public void setDenominator(long den)
{
denominator=den;
}
/****************************************************/
/* public action methods for manipulating fractions */
/****************************************************/
/**********************************************************
Method: add
Purpose: Add two fractions, a and b, where a is the "this"
object, and b is passed as the input parameter
Parameters: b, the fraction to add to "this"
Preconditions: Both fractions a and b must contain valid values
Postconditions: None
Returns: A Fraction representing the sum of two
fractions a & b
***********************************************************/
public Fraction add(Fraction b)
{
// check preconditions
if ((denominator == 0) || (b.denominator == 0))
throw new IllegalArgumentException("invalid denominator");
// find lowest common denominator
long common = lcd(denominator, b.denominator);
// convert fractions to lcd
Fraction commonA = new Fraction();
Fraction commonB = new Fraction();
commonA = convert(common);
commonB = b.convert(common);
// create new fraction to return as sum
Fraction sum = new Fraction();
// calculate sum
sum.numerator = commonA.numerator + commonB.numerator;
sum.denominator = common;
// reduce the resulting fraction
//sum = sum.reduce();
return sum;
}
/**********************************************************
Method: subtract
Purpose: Subtract fraction b from a, where a is the "this"
object, and b is passed as the input parameter
Parameters: b, the fraction to subtract from "this"
Preconditions: Both fractions a and b must contain valid values
Postconditions: None
Returns: A Fraction representing the differenct of the
two fractions a & b
***********************************************************/
public Fraction subtract(Fraction b)
{
// check preconditions
if ((denominator == 0) || (b.denominator == 0))
throw new IllegalArgumentException("invalid denominator");
// find lowest common denominator
long common = lcd(denominator, b.denominator);
// convert fractions to lcd
Fraction commonA = new Fraction();
Fraction commonB = new Fraction();
commonA = convert(common);
commonB = b.convert(common);
// create new fraction to return as difference
Fraction diff = new Fraction();
// calculate difference
diff.numerator = commonA.numerator - commonB.numerator;
diff.denominator = common;
// reduce the resulting fraction
//diff = diff.reduce();
return diff;
}
/**********************************************************
Method: multiply
Purpose: Multiply fractions a and b, where a is the "this"
object, and b is passed as the input parameter
Parameters: The fraction b to multiply "this" by
Preconditions: Both fractions a and b must contain valid values
Postconditions: None
Returns: A Fraction representing the product of the
two fractions a & b
***********************************************************/
public Fraction multiply(Fraction b)
{
// check preconditions
if ((denominator == 0) || (b.denominator == 0))
throw new IllegalArgumentException("invalid denominator");
// create new fraction to return as product
Fraction product = new Fraction();
// calculate product
product.numerator = numerator * b.numerator;
product.denominator = denominator * b.denominator;
// reduce the resulting fraction
//product = product.reduce();
return product;
}
/**********************************************************
Method: divide
Purpose: Divide fraction a by b, where a is the "this"
object, and b is passed as the input parameter
Parameters: The fraction b to divide "this" by
Preconditions: Both fractions a and b must contain valid values
Postconditions: None
Returns: A Fraction representing the result of dividing
fraction a by b
***********************************************************/
public Fraction divide(Fraction b)
{
// check preconditions
if ((denominator == 0) || (b.numerator == 0))
throw new IllegalArgumentException("invalid denominator");
// create new fraction to return as result
Fraction result = new Fraction();
// calculate result
result.numerator = numerator * b.denominator;
result.denominator = denominator * b.numerator;
// reduce the resulting fraction
//result = result.reduce();
return result;
}
/**********************************************************
Method: output
Purpose: Print the value of the "this" object to the screen.
Makes use of the toString() method.
Uses System.out.print, rather than println for flexibility
Parameters: None
Preconditions: User needs access to command line window to see output
Postconditions: The value of the "this" object will be printed to
the screen
***********************************************************/
public void output()
{
System.out.print(this);
}
/**********************************************************
Method: toString
Purpose: Convert the internal representation of a fraction,
which is stored in two integers, into a String
(which could then be printed to the screen)
Parameters: None
Preconditions: None
Postconditions: The value of the "this" object will be converted
to a String
Returns: A String representation of the "this" fraction
***********************************************************/
public String toString()
{
String buffer = numerator + "/" + denominator;
return buffer;
}
/*****************************************************/
/* private methods used internally by Fraction class */
/*****************************************************/
/**********************************************************
Method: lcd
Purpose: find lowest common denominator, used to add and
subtract fractions
Parameters: two integers, denom1 and denom2
Preconditions: denom1 and denom2 must be non-zero integer values
Postconditions: None
Returns: the lowest common denominator between denom1 and
denom2
***********************************************************/
private long lcd(long denom1, long denom2)
{
return denom1*denom2;
// long factor = denom1;
// while ((denom1 % denom2) != 0)
// denom1 += factor;
// return denom1;
}
/**********************************************************
Method: gcd
Purpose: find greatest common denominator, used to reduce
fractions
Parameters: two integers, denom1 and denom2
Preconditions: denom1 and denom2 must be positive integer values
denom1 is assumed to be greater than denom2
(denom1 > denom2 > 0)
Postconditions: None
Returns: the greatest common denominator between denom1 and
denom2
Credits: Thanks to Euclid for inventing the gcd algorithm,
and to Prof. Joyce for explaining it to me.
***********************************************************/
private long gcd(long denom1, long denom2)
{
long factor = denom2;
while (denom2 != 0) {
factor = denom2;
denom2 = denom1 % denom2;
denom1 = factor;
}
return denom1;
}
/**********************************************************
Method: convert
Purpose: convert a fraction to an equivalent one based on
a lowest common denominator
Parameters: an integer common, the new denominator
Preconditions: the "this" fraction must contain valid data for
numerator and denominator
the integer value common is assumed to be greater
than the "this" fraction's denominator
Postconditions: None
Returns: A new fraction which is equivalent to the "this"
fraction, but has been converted to the new
denominator called common
***********************************************************/
private Fraction convert(long common)
{
Fraction result = new Fraction();
long factor = common / denominator;
result.numerator = numerator * factor;
result.denominator = common;
return result;
}
/**********************************************************
Method: reduce
Purpose: convert the "this" fraction to an equivalent one
based on a greatest common denominator
Parameters: None
Preconditions: The "this" fraction must contain valid data for
numerator and denominator
Postconditions: None
Returns: A new fraction which is equivalent to a, but has
been reduced to its lowest numerical form
***********************************************************/
private Fraction reduce()
{
Fraction result = new Fraction();
long common = 0;
// get absolute values for numerator and denominator
long num = Math.abs(numerator);
long den = Math.abs(denominator);
// figure out which is less, numerator or denominator
if (num > den)
common = gcd(num, den);
else if (num < den)
common = gcd(den, num);
else // if both are the same, don't need to call gcd
common = num;
// set result based on common factor derived from gcd
result.numerator = numerator / common;
result.denominator = denominator / common;
return result;
}
}
| 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 | """
Code of Ayush Tiwari
Codeforces: servermonk
Codechef: ayush572000
"""
import sys
input = sys.stdin.buffer.readline
def solution():
for _ in range(int(input())):
# This is the main code
h,c,t=map(int,input().split())
bb=(h+c)/2
if t<=bb:
print(2)
else:
k= (h-t)/(2*t-h-c)
k=int(k)
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+1)+1)
solution() | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from collections import deque
import sys
def input(): return sys.stdin.readline().rstrip()
for _ in range(int(input())):
h, c, t = list(map(int, input().split()))
if (2 * t == c + h):
print(2)
continue
base = abs(h - c) // abs(2*t - c - h)
ans = 0
d = 2e18
for i in range(base - 10, base + 10):
if i < 1: continue
if i % 2 == 1 and abs((2 * t - c - h) * i + c - h) * ans < d * i:
d = abs((2 * t - c - h) * i + c - h)
ans = i
if (ans * abs(2 * t - c - h) <= abs(2 * t * ans - (c + h) * (ans - 1) - 2 * h)): ans = 2
print(ans) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.io.BufferedWriter;
import java.math.MathContext;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author unknown
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
CMixingWater solver = new CMixingWater();
solver.solve(1, in, out);
out.close();
}
static class CMixingWater {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int ntc = in.nextInt();
while ((ntc--) > 0) {
int h = in.nextInt();
int c = in.nextInt();
int t = in.nextInt();
if (t == h) {
out.println(1);
continue;
}
double evenAvg = ((double) h + c) / 2;
if (Math.abs(evenAvg - t) <= (int) 1e-9 || t < evenAvg) {
out.println(2);
continue;
}
int tt = cupSearch(t, h, c);
out.println(tt + (tt - 1));
}
}
int cupSearch(int t, int h, int c) {
int l = 1;
int r = 1000000001;
BigDecimal target = new BigDecimal(t);
while (l <= r) {
int mid = l + (r - l + 1) / 2;
BigDecimal temp = getAvgOfCups(mid, h, c);
if (target.compareTo(temp) < 0) {
l = mid + 1;
} else {
r = mid - 1;
}
}
BigDecimal lavg = getAvgOfCups(l, h, c);
BigDecimal ravg = getAvgOfCups(r, h, c);
// debug("BDlavg",lavg,l);
// debug("BDRavg",ravg,r);
BigDecimal diff1 = lavg.subtract(target);
BigDecimal diff2 = ravg.subtract(target);
// debug("Diff1",diff1.abs());
// debug("Diff2",diff2.abs());
int compare = diff1.abs().compareTo(diff2.abs());
if (compare < 0) {
return l;
} else if (compare > 0) {
return r;
} else {
return Math.min(l, r);
}
}
BigDecimal getAvgOfCups(int n, int h, int c) {
BigDecimal temp1 = new BigDecimal((double) n * h + (double) (n - 1) * c);
return temp1.divide(new BigDecimal(n + n - 1), MathContext.DECIMAL128);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public 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;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(long i) {
writer.println(i);
}
}
}
| 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 double pi = 3.14159265358979311599796346854;
long long mod = 998244353;
long long fast_exp(long long a, long long b) {
if (b <= 0)
return 1;
else {
long long res = 1;
res = fast_exp(a, b / 2);
res = (res * res) % mod;
if (b % 2 == 1) res = (res * a) % mod;
return res;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
long double h, c, t;
cin >> h >> c >> t;
t -= c, h -= c, c -= c;
long double avg = h / 2;
map<long double, long long> first;
if (t <= (avg)) {
cout << 2 << '\n';
} else {
long double minim = 1e15;
long double term = t / (2 * t - h);
vector<pair<long long, long double>> vec;
for (long long i = term - 100; i <= term + 100; i++) {
if (i >= 1) {
long double value = ((long double)i * h) / ((long double)(2 * i) - 1);
if (abs(value - t) <= minim) {
minim = abs(value - t);
vec.push_back({i, abs(value - t)});
}
}
}
set<long long> cand;
for (auto e : vec) {
if (abs(minim - e.second) <= 1e-12) cand.insert(e.first);
}
long long num = *cand.begin();
cout << 2 * num - 1 << '\n';
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | /**
* @author egaeus
* @mail [email protected]
* @veredict Not sended
* @url <https://codeforces.com/problemset/problem/>
* @category ?
* @date 28/05/2020
**/
import java.io.*;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;
import static java.lang.Integer.*;
import static java.lang.Math.*;
public class CFC {
static int H, C, K;
public static void main(String args[]) throws Throwable {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int T = parseInt(in.readLine());
for (int t = 0; t < T; t++) {
StringTokenizer st = new StringTokenizer(in.readLine());
H = parseInt(st.nextToken());
C = parseInt(st.nextToken());
K = parseInt(st.nextToken());
long a = 0, b = 10000000L;
//for(int i=0;i<100;i++)
//System.out.println(i+" "+f1(i));
k = BigDecimal.valueOf(K);
BigDecimal even = f1(2);
long search = busquedaTernaria(a, b);
//System.out.println(search);
long s = 0;
if (even.compareTo(f(search)) <= 0)
s = 2;
else if (search == 10000000L)
s = 2;
else s = 2 * search + 1;
System.out.println(s);
}
}
static BigDecimal g(long x) {
BigDecimal bigDecimal = BigDecimal.valueOf((x / 2) * C + ((x / 2) + (x % 2)) * H);
bigDecimal = bigDecimal.divide(BigDecimal.valueOf(x), MathContext.DECIMAL128);
return bigDecimal;
}
static BigDecimal k;
static BigDecimal f1(long x) {
BigDecimal g = g(x);
return g.subtract(k).abs();
}
static double fD(long x) {
x = 2 * x + 1;
return abs(K -1. * ((x / 2) * C + ((x / 2) + (x % 2)) * H) / (1. * x));
}
static BigDecimal f(long x) {
x = 2 * x + 1;
BigDecimal g = g(x);
return g.subtract(k).abs();
}
static long busquedaTernaria(long l, long r) {
if (l == r) {
return l;
}
if (r - l <= 2) {
if (f(l + 1).compareTo(f(l)) < 0 && f(l + 1).compareTo(f(r)) <= 0)
return l + 1;
if (f(l).compareTo(f(l + 1)) <= 0 && f(l).compareTo(f(r)) <= 0)
return l;
return r;
} else {
long m1 = (2 * l + r) / 3;
long m2 = (l + 2 * r) / 3;
double a = fD(m1), b = fD(m2);
if(abs(a-b)<1e-7) {
if (f(m1).compareTo(f(m2)) > 0) {
return busquedaTernaria(m1, r);
} else {
return busquedaTernaria(l, m2);
}
}
else if(a>b)
return busquedaTernaria(m1, r);
else
return busquedaTernaria(l, m2);
}
}
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from collections import deque
import sys
def input(): return sys.stdin.readline().rstrip()
for _ in range(int(input())):
h, c, t = list(map(int, input().split()))
if (2 * t == c + h):
print(2)
continue
base = abs(h - c) // abs(2*t - c - h)
ans = 0
d = 2e18
for i in range(base - 100, base + 100):
if i < 1: continue
if i % 2 == 1 and abs((2 * t - c - h) * i + c - h) * ans < d * i:
d = abs((2 * t - c - h) * i + c - h)
ans = i
if (ans * abs(2 * t - c - h) <= abs(2 * t * ans - (c + h) * (ans - 1) - 2 * h)): ans = 2
print(ans) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | T = int(input().strip())
eps = 1e-20
def diff(n):
return abs(((n+1)*h + n*c)/(2*n+1) - t)
def better(k, n):
return (2*k + 1) * abs((n+1)*(h-t) +n*(c-t) ) >= (2*n+1) * abs((k+1)*(h-t) + k*(c-t))
def strictlybetter(k, n):
return (2*k + 1) * abs((n+1)*(h-t) +n*(c-t) ) > (2*n+1) * abs((k+1)*(h-t) + k*(c-t))
for t in range(T):
h, c, t = list(map(int, input().split()))
if t>=h or h ==c :
print(1)
continue
elif 2*t <= h + c:
print(2)
continue
bestn =((h-t)) // (2* t - h - c)
# bestn = int(((h-t) - (t - (h+c)/2)) - 0.5)
# print('found', bestn)
bestn = int(bestn)
curdif = diff(bestn)
while bestn > 0 and better (bestn -1, bestn):
bestn -= 1
# print('found better ', bestn )
while strictlybetter(bestn +1, bestn):
bestn += 1
# print('found better ', bestn)
curdif = diff(bestn)
if abs(h-t) <= curdif:
print(1)
continue
if abs((h+c)/2 - t) <= curdif:
print(2)
continue
print(2*bestn +1) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | t=int(input())
for i in range(t):
a=[]
a=list(map(int, input().split()))
h=a[0]
c=a[1]
t=a[2]
ans=0
m=2
ans=float(h)
ch=ans
if h<=t:
print(1)
elif (h+c)/2>=t:
print(2)
else:
while 0==0:
m+=2
ch=ans
ans=((h+c)*(m//2)-c)/(m-1)
if abs(ch-t)<=abs(ans-t):
print(m-3)
break
if m>10:
m=(1-c/t)/(1-(h+c)/2/t)
m=int(m)
if m%2==0:
m+=1
ans1=((h+c)*(m//2)+h)/(m)
ans2=((h+c)*(m//2-1)+h)/(m-2)
ans3=((h+c)*(m//2+1)+h)/(m+2)
ans1=abs(t-ans1)
ans2=abs(t-ans2)
ans3=abs(t-ans3)
if (ans1<=ans2) and (ans1<=ans3):
print(m)
elif (ans2<=ans3) and (ans2<=ans1):
print(m-2)
else:
print(m+2)
break
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t 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 <class T>
inline void amin(T &x, const T &y) {
if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
if (x < y) x = y;
}
using ll = signed long long;
using PII = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAXN = 700;
const int N = 200005;
int _, n;
int t, h, c;
double get(int n) { return (1ll * n * (h + c) + h) * 1.0 / (2ll * n + 1); }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
for (scanf("%d", &_); _; _--) {
scanf("%d%d%d", &h, &c, &t);
if (h == c) {
puts("1");
} else if (t >= h) {
puts("1");
} else if (h + c >= 2 * t) {
puts("2");
} else {
int res = -1;
int l = 0, r = 100000000;
while (l <= r) {
int m = (l + r) >> 1;
if (get(m) >= t)
res = m, l = m + 1;
else
r = m - 1;
}
if (fabs(get(res) - t) <= fabs(get(res + 1) - t))
printf("%d\n", 2 * res + 1);
else
printf("%d\n", 2 * res + 3);
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.io.*;
import java.util.*;
import java.lang.*;
public class Codechef {
PrintWriter out;
StringTokenizer st;
BufferedReader br;
class Pair implements Comparable<Pair>
{
int f;
int s;
Pair(int t, int r) {
f = t;
s = r;
}
public int compareTo(Pair p)
{
if(this.f!=p.f)
return this.f-p.f;
return this.s-p.s;
}
}
// class Sort implements Comparator<String>
// {
// public int compare(String a, String b)
// {
// return (a+b).compareTo(b+a);
// }
// }
String ns() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
int nextInt() {
return Integer.parseInt(ns());
}
long nextLong() {
return Long.parseLong(ns());
}
double nextDouble() {
return Double.parseDouble(ns());
}
int lowerBound(long a[],long key)
{
int l=0,r=a.length-1;
int i=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(a[mid]<key){
i=mid;
l=mid+1;
}
else
r=mid-1;
}
return i;
}
// int lowerBound(ArrayList<Integer> a,int key)
// {
// int l=0,r=a.size()-1;
// int i=-1;
// while(l<=r)
// {
// int mid=(l+r)/2;
// if(a.get(mid)<=key)
// {
// i=mid;
// l=mid+1;
// }
// else
// r=mid-1;
// }
// return i;
// }
long power(long x,long y)
{
long ans=1;
while(y!=0)
{
if(y%2==1) ans=(ans*x)%mod;
x=(x*x)%mod;
y/=2;
}
return ans%mod;
}
int mod= 1000000007;
long gcd(long x ,long y)
{
if(y==0)
return x;
return gcd(y,x%y);
}
// ArrayList a[];
// int vis[],cnt=0;
// void dfs(int ver)
// {
// ArrayList<Integer> l=a[ver];
// if(l.size()==1)
// cnt++;
// for(int v:l)
// {
// if(vis[v]==0){
// vis[v]=vis[ver]+1;
// dfs(v);
// }
// }
// }
void solve() throws IOException{
int t=nextInt();
while(t-->0)
{
long h=nextLong();
long c=nextLong();
long k=nextLong();
long l=0,r=Integer.MAX_VALUE;
long ans=0;
long i=0;
double d=0;
//out.println("min "+min+" ans"+ans);
while(l<=r)
{
long mid=(l+r)/2;
long n=mid*2+1;
if((long)((mid*(h+c)+h)/(double)n)>=k)
{
l=mid+1;
i=(long)((mid*(h+c)+h)/(double)n);
d=((mid*(h+c)+h)%n)/(double)n;
ans=n;
}
else
r=mid-1;
// out.println("mid "+mid+" key "+((mid*(h+c)+h)/(double)n)+" i "+i+" d "+d+ " ans "+ans);
}
long i1=(long)(((ans+2)/2*(h+c)+h)/(double)(ans+2));
double d1=(((ans+2)/2*(h+c)+h)%(ans+2))/(double)(ans+2);
//out.println(i1+d1);
if(Math.abs(k-i1-d1)<Math.abs(i-k+d))
{
i=i1;
d=d1;
ans+=2;
}
if(Math.abs(k-(h+c)/2.0)<=Math.abs(k-(i+d)))
ans=2;
out.println(ans);
}
}
void run() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
out.close();
}
public static void main(String args[]) throws IOException {
new Codechef().run();
}
}
/*
int n=nextInt();
long sum=nextLong();
Long a[]= new Long[n];
long pre[]= new long[n+1];
for(int i=0;i<n;i++)
a[i]=nextLong();
Arrays.sort(a);
for(int i=0;i<n;i++)
{
if(i==0)
pre[i]=a[i];
else
pre[i]=pre[i-1]+a[i];
}
out.println(Arrays.toString(pre));
long total=pre[0];
long cnd=1;
for(int i=1;i<n;i++)
{
if(a[i]>total)
{
long t=a[i]-total;
out.println(((t-1)/pre[i-1])*pre[i-1]+" hey");
total+=((t-1)/pre[i-1])*pre[i-1];
cnd+=((t-1)/pre[i-1])*i;
out.println(total+" "+cnd);
t=a[i]-total;
int j=upperBound(pre,t);
cnd+=j+1;
total+=pre[j];
}
total+=a[i];
cnd++;
out.println(total);
}
*/
/*
int n=nextInt();
int k=nextInt();
int a[]= new int[k+1];
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
a[nextInt()]++;
for(int i=0;i<k;i++)
list.add(a[i+1]);
Collections.sort(list);
out.println(list);
int l=list.get(list.size()-1),r=l*2;
int min=Integer.MAX_VALUE;
for(int i=l;i<=r;i++)
{
int j=lowerBound(list,i-list.get(0));
out.println("lowerBound "+j);
if(j==-1)
{
min=Math.min(min,list.size()*i);
out.println(list.size()*i);
}
else
{
min=Math.min(min,(list.size()-(j+1)/2)*i);
out.println((list.size()-(j+1)/2)*i+" hey");
}
}
out.println(min);
*/
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import fractions
def calc_temp(h, c, curr):
numerator = h * curr[0] + c * curr[1]
denominator = curr[0] + curr[1]
return fractions.Fraction(numerator, denominator)
def solve(h, c, t):
if h == t or t >= (c + 5 / 6 * (h - c)):
return 1
if t <= (h + c) / 2:
return 2
start = 1
end = pow(10, 10)
curr = None
while start <= end:
mid = (start + end) // 2
curr = (mid + 1, mid)
current_temperature = calc_temp(h, c, curr)
if current_temperature == t:
return curr[0] + curr[1]
if current_temperature < t:
end = mid - 1
if current_temperature > t:
start = mid + 1
ans = None
deviation = h - t
for i in range(-1, 2):
temp = (curr[0] + i, curr[1] + i)
current_temperature = calc_temp(h, c, temp)
if abs(current_temperature - t) < deviation:
deviation = abs(current_temperature - t)
ans = temp[0] + temp[1]
return ans
def main():
t = int(input())
test_case_number = 1
while test_case_number <= t:
h, c, temp = map(int, input().split())
print(solve(h, c, temp))
test_case_number += 1
main()
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | _ = int(input())
h=0
c=0
t=0
def fn(cnt):
return t*(2*cnt-1)-cnt*h-(cnt-1)*c
for __ in range(_):
h, c, t = [int(a) for a in input().split()]
if h+c >= t*2:
print(2)
continue
# if h == t:
# print(1)
# continue
l = 1
r = 1
while fn(r) < 0:
r *= 2
l = r // 2
while l < r:
# print(l,r)
mid = (l+r)//2
if fn(mid) < 0:
l = mid+1
else:
r = mid
# print(cnt*h+(cnt-1)*c - t*(2*cnt-1), t*(2*cnt-3) - (cnt-1)*h-(cnt-2)*c)
if abs(fn(l))*(2*l-3) < abs(fn(l-1))*(2*l-1):
print(2*l - 1)
else:
print(2*l - 3)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from fractions import Fraction as f
for tc in xrange(input()):
H, C, T = map(int, raw_input().split())
ans = f(H + C, 2)
cups = 2
if abs(H - T) <= abs(ans - T):
ans = H
cups = 1
if H + C != 2 * T:
n = (T - H) / (H + C - 2 * T)
itr = max(0, n - 3)
while itr <= n + 3:
a, b = itr + 1, itr
temp = f(a * H + b * C, a + b)
if abs(temp - T) < abs(ans - T):
ans = temp
cups = a + b
itr += 1
print cups | 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 | q=int(input())
for i in range(q):
h,c,t=list(map(int,input().split()))
if 2*t<=(h+c):
print(2)
else:
k=(h-t)/((2*t)-h-c)
m=int(k)
n=m+1
x=abs(((m+1)*h)+(m*c)-(((2*m)+1)*t))/((2*m)+1)
y=abs(((n+1)*h)+(n*c)-(((2*n)+1)*t))/((2*n)+1)
if x<=y:
print((2*m)+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 | cases = int(input())
for _ in range(cases):
h, c, t = [int(x) for x in input().split()]
if t <= (h+c)/2:
print(2)
continue
n = (t-h)/(h+c-2*t)
x, y = int(n), int(n)+1
# print(x,y)
xx = ((x+1)*h+x*c)
yy = ((y+1)*h+y*c)
# print(xx,yy)
if xx*(2*y+1)+yy*(2*x+1) <= 2*t*(2*y+1)*(2*x+1):
print(2*x+1)
else:
print(2*y+1) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | for i in range(int(input())):
h,c,t=map(int,input().split())
if (h+c)/2>=t:
print(2)
else:
k=(h-t)//(2*t-h-c)
x = abs((k*(h+c)+h)-t*(2*k+1))*(2*k+3)
y = abs(((k+1)*(h+c)+h)-t*(2*k+3))*(2*k+1)
if y>=x:
print(2*k+1)
else:
print(2*k+3) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long 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() {
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 | // Utilities
import java.io.*;
import java.util.*;
public class Main {
static int T;
static int h, c, t;
static int k;
static double cmp1, cmp2;
public static void main(String[] args) throws IOException {
T = in.iscan();
while (T-- > 0) {
h = in.iscan(); c = in.iscan(); t = in.iscan();
if (2*t <= h+c) { out.println(2); continue; }
k = (int)Math.floor((double)(h-t)/(2*t-h-c));
if (Math.abs((k*(h+c)+h)-t*(2*k+1))*(2*k+3)<=Math.abs(((k+1)*(h+c)+h)-t*(2*k+3))*(2*k+1)) {
out.println(2*k+1);
}
else {
out.println(2*(k+1)+1);
}
}
out.close();
}
static INPUT in = new INPUT(System.in);
static PrintWriter out = new PrintWriter(System.out);
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public int cscan () throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
}
if (numChars == -1)
return numChars;
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c))
c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
public static class UTILITIES {
static final double EPS = 10e-6;
public static int lower_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static long gcd (long a, long b) {
return b == 0 ? a : gcd (b, a % b);
}
public static long lcm (long a, long b) {
return a * b / gcd (a, b);
}
public static long fast_pow_mod (long b, long x, int mod) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod;
return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod;
}
public static int fast_pow (int b, int x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public static long choose (long n, long k) {
k = Math.min (k, n - k);
long val = 1;
for (int i = 0; i < k; ++i)
val = val * (n - i) / (i + 1);
return val;
}
public static long permute (int n, int k) {
if (n < k) return 0;
long val = 1;
for (int i = 0; i < k; ++i)
val = (val * (n - i));
return 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 | from __future__ import division, print_function
_interactive = False
def main():
def f(h, c, t, i):
return (h*(i+1) + c*i - t*(2*i+1))
def interpolation(h, c, t, st):
prev = st
i = 0
while True:
cur = st + 2**i
if f(h, c, t, cur) <= 0:
if prev == st:
return st
else:
return interpolation(h, c, t, prev)
prev = cur
i += 1
for _ in range(int(input())):
h, c, t = input_as_list()
mid = (h+c)/2
if t >= h:
print(1)
elif t <= mid:
print(2)
else:
i = interpolation(h, c, t, 0)
v1 = abs(f(h, c, t, i))
v2 = abs(f(h, c, t, i+1))
ans = 2*i+1
if v1*(2*i+3) > v2*(2*i+1):
ans += 2
print(ans)
# Constants
INF = float('inf')
MOD = 10**9+7
alphabets = 'abcdefghijklmnopqrstuvwxyz'
# Python3 equivalent names
import os, sys, itertools
if sys.version_info[0] < 3:
input = raw_input
range = xrange
filter = itertools.ifilter
map = itertools.imap
zip = itertools.izip
# print-flush in interactive problems
if _interactive:
flush = sys.stdout.flush()
def printf(*args, **kwargs):
print(*args, **kwargs)
flush()
# Debug print, only works on local machine
LOCAL = "LOCAL_" in os.environ
debug_print = (print) if LOCAL else (lambda *x, **y: None)
# Fast IO
if (not LOCAL) and (not _interactive):
from io import BytesIO
from atexit import register
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
# Some utility functions(Input, N-dimensional lists, ...)
def input_as_list():
return [int(x) for x in input().split()]
def input_with_offset(o):
return [int(x)+o for x in input().split()]
def input_as_matrix(n, m):
return [input_as_list() for _ in range(n)]
def array_of(f, *dim):
return [array_of(f, *dim[1:]) for _ in range(dim[0])] if dim else f()
main()
| PYTHON |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.util.*;
import java.io.*;
public class I {
public I(FastScanner in, PrintWriter out) {
int T = in.nextInt();
for (int i = 0; i < T; i++) {
long h = in.nextInt();
long c = in.nextInt();
long t = in.nextInt();
double minT = (h + c) / 2.0;
if (t <= minT) {
out.println(2);
} else {
double x = 1.0 * (t - c) / (2 * t - c - h);
long x0 = (long)Math.floor(x), x1 = (long)Math.ceil(x);
long c0 = (2 * x0 + 1) * ((h + c) * x0 - c) - (2 * x0 - 1) * (2 * x0 + 1) * t;
long c1 = (2 * x0 - 1) * ((h + c) * (x0 + 1) - c) - (2 * x0 - 1) * (2 * x0 + 1) * t;
//out.printf("%d, %d%n", c0, c1);
if (Math.abs(c0) <= Math.abs(c1))
out.println(x0 + x0 - 1);
else
out.println(x1 + x1 - 1);
}
}
}
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out);
// Scanner in = new Scanner(
// new BufferedReader(new InputStreamReader(System.in)));
FastScanner in = new FastScanner(System.in);
I sol = new I(in, out);
out.close();
}
}
class FastScanner{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream)
{
this.stream = stream;
}
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;
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
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();
}
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();
}
}
| 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;
public class CF_1363_C {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int tc=sc.nextInt();
while(tc-->0) {
int h=sc.nextInt();
int c=sc.nextInt();
int t=sc.nextInt();
if((h+c)/2>=t) {
System.out.println("2");
} else {
int a=h-t;
int b=2*t-h-c;
int k=2*(a/b)+1;
long val1=Math.abs((k/2*1l*(h+c)+h)-t*1l*k);
long val2=Math.abs(((k/2+1)*1l*(h+c)+h)-t*1l*(k+2));
int pp=val1*(k+2)<=val2*k?k:k+2;
System.out.println(pp);
}
}
}
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from sys import stdin, gettrace
if not gettrace():
def input():
return next(stdin)[:-1]
# def input():
# return stdin.buffer.readline()
def main():
def solve():
h,c,t = map(int, input().split())
if t <= (h+c)/2:
print(2)
return
if t >= h:
print(1)
return
m = (h-t)//(2*t - h - c)
t1n = ((m+1) * h + m*c)
t2n = ((m+2) * h + (m+1)*c)
t1d = 2*m+1
t2d = 2*m+3
if abs(t1n*t2d -t*t1d*t2d) <= abs(t2n*t1d -t*t1d*t2d):
print(2*m+1)
else:
print(2*m+3)
q = int(input())
for _ in range(q):
solve()
if __name__ == "__main__":
main() | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | for i in range(int(input())):
h,c,t = map(int,input().split())
if h+c>= 2*t:
print (2)
elif h==t:
print (1)
else:
x = int((t-c)/(2*t -h-c))
tb1 = abs((2*x-1)*t- ((h+c)*x-c))*(2*x+1)
tb2 = abs(t*(2*x+1) - ((h+c)*x+h))*(2*x-1)
if tb2 < tb1:
z = x+1 +x
else:
z = x + x-1
print(z) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t 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);
int t;
cin >> t;
while (t--) {
int c, h, t1;
cin >> h >> c >> t1;
int ans = 0;
if (t1 <= (c + h) / 2)
cout << 2 << endl;
else {
int a = h - t1;
int b = 2 * t1 - c - h;
int k = 2 * (a / b) + 1;
long long v1 =
abs(k / 2 * 1ll * c + (k + 1) / 2 * 1ll * h - t1 * 1ll * k);
long long v2 = abs((k + 2) / 2 * 1ll * c + (k + 3) / 2 * 1ll * h -
t1 * 1ll * (k + 2));
long long ans = v1 * (k + 2) <= v2 * k ? k : k + 2;
cout << ans << endl;
}
}
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
double h, c, t;
double f(ll i) { return (ll((i + 1) / 2) * h + ll(i / 2) * c) / (double)i; }
void solve() {
cin >> h >> c >> t;
if (t >= h) {
cout << 1 << endl;
return;
}
if (t <= (h + c) / 2) {
cout << 2 << endl;
return;
}
ll low = 1;
ll len = 1ll << 38;
ll steps;
while (len >= 1) {
ll mid = low + len / 2;
double cavg = f(mid);
if (cavg >= t) {
steps = mid;
low = mid;
}
len /= 2;
}
ll ans = 2;
double avg = (h + c) / 2.0;
for (int i = 0; i <= 100; ++i) {
double cavg = f(steps + i);
if (fabs(t - cavg) < fabs(t - avg)) {
ans = steps + i;
avg = cavg;
}
}
cout << ans << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int maxn = 100000 + 5;
int h, c, t;
double fun(double x) {
return fabs((double)((x + 1) * h + (x)*c) / (double)(2 * x + 1) - (double)t);
}
int main() {
int T;
cin >> T;
while (T--) {
scanf("%d%d%d", &h, &c, &t);
int r, l;
if (t >= h) {
cout << 1 << endl;
continue;
} else if (t <= (h + c) / 2) {
cout << 2 << endl;
continue;
} else {
r = 0x3f3f3f3f;
l = 0;
int mid1 = (r - l) / 3 + l;
int mid2 = (2 * (r - l)) / 3 + l;
int tim = 1000;
while (tim--) {
mid1 = min((r - l) / 3 + l, r);
mid2 = min((2 * (r - l)) / 3 + l + 1, r);
if (fun(mid1) < fun(mid2)) {
r = mid2;
} else {
l = mid1;
}
}
int ans = 0x3f3f3f3f;
double save = 0x3f3f3f3f;
for (int i = l - 100; i <= r + 100; i++) {
if (i < 0) {
i = 0;
}
if (fun(i) < save) {
save = fun(i);
ans = i * 2 + 1;
}
}
cout << ans << endl;
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | # Why do we fall ? So we can learn to pick ourselves up.
from decimal import *
getcontext().prec = 40
from math import ceil
t = int(input())
for _ in range(0,t):
h,c,t = map(int,input().split())
if h+c >= 2*t:
print(2)
else:
# x, x-1, error +- 10
num = t-c
den = 2*t - h-c
x = ceil(num/den)
ans = 2*x - 1
diff = 10**9+7
# for + 10 error
for i in range(x,x+11):
if abs(Decimal(i*h+(i-1)*c)/Decimal(2*i-1) - t) < diff:
# print("i out",i,2*i-1)
ans = 2*i-1
diff = abs(Decimal(i*h+(i-1)*c)/Decimal(2*i-1) - t)
# print(diff,"diff")
# for - 10 error
for i in range(max(1,x-10),x):
if abs(Decimal(i*h+(i-1)*c)/Decimal(2*i-1) - t) <= diff:
# print(i,"i")
ans = 2*i-1
diff = abs(Decimal(i*h+(i-1)*c)/Decimal(2*i-1) - t)
# print("diff in", diff)
print(ans)
"""
1
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 java.io.*;
import java.io.IOException;
import java.util.*;
//import javafx.util.Pair;
//import java.util.concurrent.LinkedBlockingDeque;
//import sun.net.www.content.audio.wav;
public class Codeforces {
public static long mod = (long)Math.pow(10, 9)+7 ;
public static double epsilon=0.00000000008854;//value of epsilon
public static InputReader sc = new InputReader(System.in);
public static PrintWriter pw = new PrintWriter(System.out);
public static int firstNode(int a,HashMap<Integer,Integer> p){
if(p.containsKey(a)){
p.put(a, firstNode(p.get(a), p));
}
else return a;
return p.get(a);
}
public static void Union(int a,int b,HashMap<Integer,Integer> p){
//int a=firstNode(a1, p);
//int b=firstNode(b1, p);
/*if(a!=b){
if(r[a]<r[b]){
p[a]=b;
}
else if(r[a]>r[b]){
p[b]=a;
}
else{
r[b]++;
p[a]=b;
}
}*/
a=firstNode(a, p);
b=firstNode(b, p);
if(a!=b)
p.put(a, b);//if no rank than
}
public static ArrayList<ArrayList <Integer>> GetGraph(int n,int m){
ArrayList<ArrayList <Integer>> a=new ArrayList<>();
for(int i=0;i<n;i++){
a.add(new ArrayList<>());
}
for(int i=0;i<m;i++){
int u=sc.nextInt()-1;
int v=sc.nextInt()-1;
a.get(u).add(v);
a.get(v).add(u);
}
return a;
}
public static void dfs(char a[][],int vis[][],int x,int y){
int n=a.length;
int m=a[0].length;
if(x<0||x>=n||y<0||y>=m){
return;
}
if(vis[x][y]==1)
return;
if(a[x][y]=='G'||a[x][y]=='.'){
vis[x][y]=1;
dfs(a, vis, x+1, y);
dfs(a, vis, x-1, y);
dfs(a, vis, x, y+1);
dfs(a, vis, x, y-1);
}
}
public static void main(String[] args) {
// code starts..
int q=sc.nextInt();
while(q-->0){
long min=Long.MAX_VALUE;
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
long t=2;
min=Math.abs(a+b-2*c);
long l=0,r=100000000;
while(l<=r){
long mid=(l+r)/2;
//pw.println("kefnkenf "+mid);
long v=(2*mid+1)*c-mid*(a+b)-a;
if(v>0){
r=mid-1;
}
else{
l=mid+1;
}
if((2*mid+1)*min>t*Math.abs(v)||((2*mid+1)*min==t*Math.abs(v)&&t>(2*mid+1))){
t=2*mid+1;
min=Math.abs(v);
}
// pw.println(t+" "+min);
}
//pw.println(t+" "+min);
pw.println(t);
}
// Code ends...
pw.flush();
pw.close();
}
public static Comparator<Integer[]> column(int i){
return
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
return o1[i].compareTo(o2[i]);//for ascending
//return o2[i].compareTo(o1[i]);//for descending
}
};
}
public static String reverseString(String s){
StringBuilder input1 = new StringBuilder();
input1.append(s);
input1 = input1.reverse();
return input1.toString();
}
public static int[] scanArray(int n){
int a[]=new int [n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
return a;
}
public static long[] scanLongArray(int n){
long a[]=new long [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLong();
return a;
}
public static String [] scanStrings(int n){
String a[]=new String [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLine();
return a;
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class SegmentTree{
int s[],n;
SegmentTree(int a[]){
n=a.length;
int l=(int)Math.ceil(Math.log(n)/Math.log(2));
l=2*(int)Math.pow(2,l)-1;
s=new int[l];
createSegmentTreeUtil(a, 0, 0, a.length-1);
}
int createSegmentTreeUtil(int a[],int root,int l,int r){
if(l==r)
s[root]=a[l];
else
s[root]= Compare(createSegmentTreeUtil(a, 2*root+1, l, (l+r)/2), createSegmentTreeUtil(a,2*root+2, (l+r)/2+1,r));
return s[root];
}
int getValue(int gl,int gr){
return getValueUtil(0, 0, n-1, gl, gr);
}
int getValueUtil(int root,int l,int r,int gl,int gr){
if(l>=gl&&r<=gr){
return s[root];
}
if(l>gr||r<gl){
return 0;
}
return Compare(getValueUtil(2*root+1, l, (l+r)/2, gl, gr), getValueUtil(2*root+2, (l+r)/2+1, r, gl, gr));
}
void update(int value,int index){
updateUtil(value, index,0,0,n-1);
}
int updateUtil(int p,int k,int root,int l,int r){
if(l==r&&l==k){
return s[root]=p;
}
else if(l>k||r<k)
return s[root];
else{
return s[root]=Compare(updateUtil(p, k, 2*root+1, l, (l+r)/2), updateUtil(p, k, 2*root+2,(l+r)/2+1,r ));
}
}
int Compare(int a,int b){
return Math.max(a, b);
}
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
using namespace std;
using namespace std;
inline long long int addmod(long long int a, long long int b) {
return (a % 1000000007 + b % 1000000007) % 1000000007;
}
inline long long int submod(long long int a, long long int b) {
return (a % 1000000007 - b % 1000000007) % 1000000007;
}
inline long long int mulmod(long long int a, long long int b) {
return ((a % 1000000007) * (b % 1000000007)) % 1000000007;
}
bool isPrime(int n) {
if (n == 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
vector<int> factors(int n) {
vector<int> fact;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
fact.push_back(i);
if (i * i != n) {
fact.push_back(n / i);
}
}
}
return fact;
}
long long int pw(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if (b & 1) {
res = (res * a);
}
b = b >> 1;
a = (a * a);
}
return res;
}
long long int pwmod(long long int a, long long int b) {
long long int res = 1;
a = a % 1000000007;
while (b > 0) {
if (b & 1) {
res = (res * a) % 1000000007;
}
b = b >> 1;
a = (a * a) % 1000000007;
}
return res;
}
long long int modinv(long long int a) { return pwmod(a, 1000000007 - 2); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt;
cin >> tt;
while (tt--) {
int a, b, c;
cin >> a >> b >> c;
if (c >= a) {
cout << 1 << '\n';
continue;
}
if (c <= (a + b) / 2) {
cout << 2 << '\n';
continue;
}
long long int st = 1;
long long int dr = (1LL << 40);
long long int mini = 0;
double te = 0;
while (st <= dr) {
long long int mid = (st + dr) / 2;
long long int sum = 1LL * mid * a + 1LL * (mid - 1) * b;
double avg = (0.0000 + sum) / (0.0000 + mid + mid - 1);
if (avg >= c)
mini = mid, st = mid + 1;
else
dr = mid - 1;
}
te = (a + b) / 2;
long long int ans = 2;
for (long long int mid = mini; mid <= mini + 100; mid++) {
long long int sum = 1LL * mid * a + 1LL * (mid - 1) * b;
double avg = (0.0000 + sum) / (0.0000 + mid + mid - 1);
if (abs(avg - c) < abs(te - c)) {
ans = mid + mid - 1;
te = avg;
}
}
cout << ans << '\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
testc = int(input())
def diff(h, t, c, n):
n = float(n)
curt = round((n * h + (n - 1) * c) / (2 * n - 1), 20)
return curt
for i in range(testc):
l = input().split()
h = float(l[0])
c = float(l[1])
t = float(l[2])
if(h == t):
print(1)
elif((h + c) / 2.0 >= t):
print(2)
else:
n = (t - c) / (2 * t - h - c)
n1 = math.floor(n)
n2 = math.ceil(n)
a1 = n1 * h + (n1 - 1) * c
b1 = 2 * n1 - 1
a2 = n2 * h + (n2 - 1) * c
b2 = 2 * n2 - 1
if((t * b1 - a1) * b2 < (a2 - t * b2) * b1):
ans = n2
else:
ans = n1
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
input = sys.stdin.buffer.readline
def print(val):
sys.stdout.write(str(val) + '\n')
def val(x,h,c):
return (h-c)/(2*(x*2 -1))
def prog():
for _ in range(int(input())):
L = 1
R = 10**12
h,c,t = map(int,input().split())
if t <= (h+c)/2:
print(2)
else:
t -= (h+c)/2
while R > L:
m = (R+L)//2
temp = val(m,h,c)
if temp > t:
L = m+1
else:
R = m-1
left = val(L,h,c)
if left > t:
right = val(L+1,h,c)
if abs(t-left) <= abs(t-right):
print(2*L-1)
else:
print(2*L+1)
else:
right = val(L-1,h,c)
if abs(t-left) >= abs(t-right):
print(2*L-3)
else:
print(2*L-1)
prog()
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | """ Python 3 compatibility tools. """
from __future__ import division, print_function
import itertools
import sys
import os
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
input = raw_input
range = xrange
filter = itertools.ifilter
map = itertools.imap
zip = itertools.izip
def is_it_local():
script_dir = str(os.getcwd()).split('/')
username = "dipta007"
return username in script_dir
def READ(fileName):
if is_it_local():
sys.stdin = open(f'./{fileName}', 'r')
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
if not is_it_local():
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
def input1(type=int):
return type(input())
def input2(type=int):
[a, b] = list(map(type, input().split()))
return a, b
def input3(type=int):
[a, b, c] = list(map(type, input().split()))
return a, b, c
def input_array(type=int):
return list(map(type, input().split()))
def input_string():
s = input()
return list(s)
##############################################################
import math, fractions
F = fractions.Fraction
def main():
t = input1()
for ci in range(t):
h, c, t = input3()
avg = F((h + c), 2)
#
# for i in range(40):
# odd = i * 2 + 1
# m = (i * (h+c) + h) / odd
# print(odd, m)
# Just 2 ta nibo
res = 2
mx = abs(t - avg)
low = 0
high = 40000000
now = 0
while low <= high:
mid = (low + high) // 2
odd = mid * 2 + 1
m = (mid * (h+c) + h) / odd
if m <= t:
high = mid - 1
now = mid
else:
low = mid + 1
# print(now)
for i in range(max(0, now-10), now + 10):
odd = i * 2 + 1
m = F((i * (h + c) + h), odd)
if abs(m - t) < mx:
mx = abs(m-t)
res = odd
elif abs(m - t) == mx:
res = min(res, odd)
print(res)
pass
if __name__ == '__main__':
READ('in.txt')
main() | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | # -*- coding: utf-8 -*-
import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2**62-1
def read_int():
return int(buff_readline())
def read_int_n():
return list(map(int, buff_readline().split()))
def read_float():
return float(buff_readline())
def read_float_n():
return list(map(float, buff_readline().split()))
def read_str():
return readline().strip()
def read_str_n():
return readline().strip().split()
def error_print(*args):
print(*args, file=sys.stderr)
def mt(f):
import time
def wrap(*args, **kwargs):
s = time.time()
ret = f(*args, **kwargs)
e = time.time()
error_print(e - s, 'sec')
return ret
return wrap
class Bisect:
def __init__(self, func):
self.__func = func
def bisect_left(self, x, lo, hi):
while lo < hi:
mid = (lo+hi)//2
if self.__func(mid) < x:
lo = mid+1
else:
hi = mid
return lo
def bisect_right(self, x, lo, hi):
while lo < hi:
mid = (lo+hi)//2
if x < self.__func(mid):
hi = mid
else:
lo = mid+1
return lo
from fractions import Fraction
# @mt
def slv(H, C, T):
if 2*T <= (H+C):
return 2
def f(n):
m = n-1
t = Fraction((n*H + m*C), n+m)
return -t
i = Bisect(f).bisect_left(-T, 1, 10**9)
if abs(T+f(i)) >= abs(T+f(i-1)):
i -= 1
if abs(T+f(i)) > abs(T+f(i+1)):
i += 1
i += i-1
return i
def ref(H, C, T):
n = 0
m = 0
d = (INF, -1)
for i in range(1000):
n += 1
t = ((n*H + m*C) / (n+m))
d = min(d, (abs(T-t), n+m))
m += 1
t = ((n*H + m*C) / (n+m))
d = min(d, (abs(T-t), n+m))
return d[1]
def main():
T = read_int()
for _ in range(T):
H, C, T = read_int_n()
print(slv(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 | #include <bits/stdc++.h>
using namespace std;
long long h, c, t;
double f(long long n) { return abs(t - (h + (h + c) * n) * 1.0 / (2 * n + 1)); }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int nTest = 0;
cin >> nTest;
while (nTest--) {
cin >> h >> c >> t;
if (h + c >= 2 * t) {
cout << 2 << "\n";
continue;
}
long long l = 0, r = t + 1, m;
while (r - l > 1) {
m = (l + r) >> 1;
if (f(m) < f(m - 1))
l = m;
else
r = m;
}
cout << l * 2 + 1 << "\n";
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | for _ in range(int(input())):
h,c,t = map(int,input().split())
if t<=(h+c)/2:
print(2)
else:
x = (h-t)//(2*t-h-c)
v1 = ((x+1)*h + x*c)
v2 = ((x+2)*h + (x+1)*c)
if abs(v1-(2*x+1)*t)/(2*x+1)<=abs(t*(2*x+3)-v2)/(2*x+3):
print(2*x+1)
else:
print(2*x+3)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97 # a
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return [int(i) for i in input().split()]
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def revn(n): return str(n)[::-1]
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
def sieve(n):
if n < 2: return list()
prime = [True for _ in range(n + 1)]
p = 3
while p * p <= n:
if prime[p]:
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 2
r = [2]
for p in range(3, n + 1, 2):
if prime[p]:
r.append(p)
return r
def divs(n, start=1):
r = []
for i in range(start, int(math.sqrt(n) + 1)):
if (n % i == 0):
if (n / i == i):
r.append(i)
else:
r.extend([i, n // i])
return r
def divn(n, primes):
divs_number = 1
for i in primes:
if n == 1:
return divs_number
t = 1
while n % i == 0:
t += 1
n //= i
divs_number *= t
def prime(n):
if n == 2: return True
if n % 2 == 0 or n <= 1: return False
sqr = int(math.sqrt(n)) + 1
for d in range(3, sqr, 2):
if n % d == 0: return False
return True
def convn(number, base):
newnumber = 0
while number > 0:
newnumber += number % base
number //= base
return newnumber
def cdiv(n, k): return n // k + (n % k != 0)
arr = []
for _ in range(ii()):
h, c, t = mi()
if t - (h + c) / 2 <= 0:
arr.append(2)
else:
k = int((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):
arr.append(2 * k + 1)
else:
arr.append(2 * k + 3)
print(*arr, sep='\n')
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long check(long long n, long long m, long long x, long long y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
void pr() { cout << '\n'; }
template <class A, class... B>
void pr(const A &a, const B &...b) {
cout << a << (sizeof...(b) ? " " : "");
pr(b...);
}
template <class A>
void PR(A a, long long n) {
for (long long i = (long long)(0); i < (long long)(n); i++) {
if (i) cout << ' ';
cout << a[i];
}
cout << '\n';
}
const long long MAX = 1e9 + 7, MAXL = 1LL << 61,
dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1},
dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
void Main() {
long long T;
cin >> T;
while (T--) {
long long h, c, t;
cin >> h >> c >> t;
if (t <= (h + c) / 2) {
pr(2);
continue;
}
long long l = 0, r = MAX;
while (l + 1 < r) {
long long m = (l + r) / 2;
double d = m * h + (m - 1) * c;
d /= m * 2 - 1;
if (d <= t)
r = m;
else
l = m;
}
double M = MAXL;
long long ans;
for (long long i = (long long)(-3); i < (long long)(3); i++) {
long long m = l + i;
if (m <= 0) continue;
double d = m * h + (m - 1) * c;
d /= m * 2 - 1;
double c = fabs(t - d);
if (c < M) {
M = c;
ans = m * 2 - 1;
}
}
pr(ans);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
Main();
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from collections import Counter, defaultdict, deque
import bisect
from sys import stdin, stdout
from itertools import repeat
import math
# sys.stdin = open('input')
def inp(force_list=False):
re = map(int, raw_input().split())
if len(re) == 1 and not force_list:
return re[0]
return re
def inst():
return raw_input().strip()
def gcd(x, y):
while(y):
x, y = y, x % y
return x
def my_main():
t = inp()
# t = 1000
for i in range(t):
h, c, t = inp()
# h, c, t = 2000000, 100, 1010150
def bg(a, b):
return abs(a[0]) * abs(b[-1]) > abs(a[-1]) * abs(b[0])
def ck1():
fi = (h+c)/2.0
if 2*t==h+c:
nk = 0
else:
nk = (h-t)/(2*t-h-c)
ans = 1
mx = (h-t, 1)
if bg(mx, (h+c-2*t, 2)):
mx = (h+c-2*t, 2)
ans = 2
if 2*t <= h+c:
return ans
for off in range(-2, 3):
kk = max(0, int(off+nk))
nt = (abs(((kk+1)*h+kk*c)-t*(2*kk+1)), (2*kk+1))
# print mx[0]/float(mx[-1]), nt[0]/float(nt[-1])
if bg(mx, nt):
mx = nt
ans = kk*2+1
return ans
c1 = ck1()
print c1
my_main()
| PYTHON |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, mod = 1e9 + 7;
int h, c, t;
long double get(int x) {
long double cur = ((x / 2) + (x % 2)) * 1ll * h + (x / 2) * 1ll * c;
cur /= x;
return cur;
}
int main() {
int tc = 1;
scanf("%d", &tc);
for (int cn = 1; cn <= tc; cn++) {
scanf("%d%d%d", &h, &c, &t);
long double best = (h + c) / (double)2;
int l = 0, r = 1e8, res = 1;
while (l <= r) {
int mid = l + (r - l) / 2;
int odd = 1 + 2ll * mid;
if (get(odd) < t)
r = mid - 1;
else {
res = odd;
l = mid + 1;
}
}
int ans = 2;
if (abs(t - get(res)) < abs(t - best)) {
ans = res;
best = get(res);
}
if (abs(t - get(res + 2)) < abs(t - best)) ans = res + 2;
printf("%d\n", ans);
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from decimal import *
getcontext().prec = 28
def solve():
if h == c:
return 1
if (h + c) // 2 >= t:
print(2)
return
ans = bs(1, 10 ** 6)
a1 = getdif(ans)
a2 = getdif(ans - 1)
if abs(t - a1) < abs(t - a2):
print(2 * ans - 1)
else:
ans -= 1
print(2 * ans - 1)
return
def bs(l, r):
while l < r:
m = (l + r) // 2
if check(m):
r = m
else:
l = m + 1
return r
def check(hh):
temp = getdif(hh)
# print(hh, ": ", temp)
if temp < t:
return 1
else:
return 0
def getdif(hh):
return Decimal(hh * h + (hh-1) * c) / Decimal(2*hh-1)
# def solve2():
# hc = [0, 0]
# mt = 10**9
# ans = [0, 0]
# for i in range(10**6):
# hc[i&1] += 1
# temp = abs(t- (hc[0] * h + hc[1]*c) / float(hc[0] + hc[1]))
# if temp < mt:
# mt = temp
# ans = list(hc)
# print(sum(ans))
# return
T = int(input())
for _ in range(T):
h, c, t = [int(i) for i in input().split()]
solve() | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import sys
input = sys.stdin.readline
def solve(mid):
return (mid + 1) * h + mid * c >= t * (2 * mid + 1)
t = int(input())
for _ in range(t):
h, c, t = map(int, input().split())
if 2 * t <= h + c:
print(2)
continue
if h <= t:
print(1)
continue
ok = 0
ng = 10 ** 20
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if solve(mid):
ok = mid
else:
ng = mid
ans = 10 ** 30
t_ = (2 * ok + 1) * (2 * ng + 1) * t
if ((ok + 1) * h + c * ok) * (2 * ng + 1) - t_ > t_ - ((ng + 1) * h + c * ng) * (2 * ok + 1):
print(2 * ng + 1)
else:
print(2 * ok + 1) | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.