博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Educational Codeforces Round 88 (Rated for Div. 2)C. Mixing Water(数学+二分法)---题解
阅读量:4034 次
发布时间:2019-05-24

本文共 2745 字,大约阅读时间需要 9 分钟。

C. Mixing Water

来源:

There are two infinite sources of water:

hot water of temperature h;

cold water of temperature c (c<h).
You perform the following procedure of alternating moves:

take one cup of the hot water and pour it into an infinitely deep barrel;

take one cup of the cold water and pour it into an infinitely deep barrel;
take one cup of the hot water …
and so on …
Note that you always start with the cup of hot water.

The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.

You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is tb, then the absolute difference of tb and t (|tb−t|) should be as small as possible.

How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.

Input

The first line contains a single integer T (1≤T≤3⋅104) — the number of testcases.

Each of the next T lines contains three integers h, c and t (1≤c<h≤106; c≤t≤h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.

Output

For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.

Example

inputCopy
3
30 10 20
41 15 30
18 13 18
outputCopy
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.

In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won’t get us closer to t than that.

In the third testcase the temperature after 1 poured cup: 1 hot is 18. That’s exactly equal to t.

题意 :

给一个容器,往里面不断地倒 热水 冷水 交替进行,问最少倒多少杯水后容器内的平均温度最接近于 T.

思路:

  • 很容易观察到,如果倒进去的水是偶数杯,那温度一定是 (h+c)/ 2 。而奇数杯的话 ,越多肯定是越接近于(h+c)/2,并且是递减的。
  • 然后考虑一下特殊情况,如果 h=t ,那答案一定是 1,如果 (h+c)/2 >=t ,那答案一定是 2,因为达到平均温度的时候 差值是最小的,最后的温度不可能低于平均温度。
  • 最后考虑普遍的情况就是 找最接近于温度 t 的左右两个状态,一个大于 t 一个小于 t ,二分奇数杯就好了。(有点像高中的数学题)

代码:

#include
using namespace std;typedef long long ll;const int MAXN = 1e3 + 7;using namespace std;int q, h, c, t;char s[MAXN][MAXN];double f(ll mid) {
return (mid * (h + c) + h) * 1.0 / (mid * 2 + 1) * 1.0;}int main() {
cin >> q; while (q--) {
cin>>h>>c>>t; if (t >= h) {
cout<<1<
= 2 * t) {
cout<<2<
= t * 1.0) ans = mid, l = mid + 1; else r = mid - 1; } if (fabs(f(ans) - t * 1.0) > fabs(f(ans + 1) - t * 1.0)) cout<<2 * ans + 3<

转载地址:http://kofdi.baihongyu.com/

你可能感兴趣的文章
gdb debug tips
查看>>
arm linux 生成火焰图
查看>>
linux和windows内存布局验证
查看>>
linux config
查看>>
linux insmod error -1 required key invalid
查看>>
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>
C语言位扩展
查看>>
linux dump_backtrace
查看>>
linux irqdebug
查看>>
git 常用命令
查看>>
linux位操作API
查看>>
uboot.lds文件分析
查看>>
uboot start.s文件分析
查看>>
没有路由器的情况下,开发板,虚拟机Ubuntu,win10主机,三者也可以ping通
查看>>
本地服务方式搭建etcd集群
查看>>
安装k8s Master高可用集群
查看>>
忽略图片透明区域的事件(Flex)
查看>>
忽略图片透明区域的事件(Flex)
查看>>