C. MIXING WATER

http://www.yyycode.cn/index.php/2020/05/30/c-mixing-water/


There are two infinite sources of water:

  • hot water of temperature hh;
  • cold water of temperature cc (c<hc<h).

You perform the following procedure of alternating moves:

  1. take one cup of the hot water and pour it into an infinitely deep barrel;
  2. take one cup of the cold water and pour it into an infinitely deep barrel;
  3. take one cup of the hot water ……
  4. and so on ……

Note that you always start with the cup of hot water.

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

You want to achieve a temperature as close as possible to tt. So if the temperature in the barrel is tbtb, then the absolute difference of tbtb and tt (|tb−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 tt? If there are multiple answers with the minimum absolute difference, then print the smallest of them.Input

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

Each of the next TT lines contains three integers hh, cc and tt (1≤c<h≤1061≤c<h≤106; c≤t≤hc≤t≤h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.Output

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

3
30 10 20
41 15 30
18 13 18

outputCopy

2
7
1

Note

In the first testcase the temperature after 22 poured cups: 11 hot and 11 cold is exactly 2020. So that is the closest we can achieve.

In the second testcase the temperature after 77 poured cups: 44 hot and 33 cold is about 29.85729.857. Pouring more water won’t get us closer to tt than that.

In the third testcase the temperature after 11 poured cup: 11 hot is 1818. That’s exactly equal to tt.


题意:有热水和冷水,给了热水和冷水的温度,给了一个T,问在先一杯热水再一杯冷水循环倒水的情况下问最接近给定T的杯数是多少?

思路:注意水的体积,所以热水杯数和冷水杯数一样的时候温度和一杯热水一杯冷水的平均温度是一样的。所以温度只有两种情况,一种是热水杯数和冷水杯数一样(固定温度),一种是热水杯数比冷水杯数多一。

我们可以 二分 找一个>=t的次数 , 找一个小于等于t的次数 ,比较看哪个更近。这个方法有很多表示会卡精度,有longdouble也不行的,有的说要改成分数的,有的枚举上下10杯。当然好像是有距离一样的时候,特判找最小的杯数。

所以更方便的是推公式。

算出来的时候x是小数,我们找x和x+1比较就可以了

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
typedef long long LL;
using namespace std;
int main()
{
	LL t;
	cin>>t;
	while(t--)
	{
		double a,b,c;
		cin>>a>>b>>c;
		if(a==c)	
		{
			cout<<1<<endl;
			continue;
		}
		double tk  =(a+b)/2;
		if(tk>=c)
		cout<<2<<endl;
		else
		{	
		LL step1 = (c - a)/(a+b-2*c);
		double rr3 = (step1*b+ (step1+1)*a)/(2*step1+1);
		double rr2 =((step1+1)*b+(step1+2)*a)/(2*step1+3);
		if(fabs(rr3-c)<=fabs(rr2-c))
		cout<<2*step1+1<<endl;
		else
		cout<<2*step1+3<<endl;
		
			}
	}
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/106447769