codeforces 1359C math

只想着求规律了

题意

  q组输入样例,每组三个数,h,c,t。有热水,和冷水,水温分别为h和c(h>c),从热水开始,冷热水交替倒入一个桶里,水里的温度为温度和除以倒入的次数,问最少第几次可以最接近于温度t。

限制

思路

当倒入的水为偶数时候,水温为(h+t)/2,当为奇数的时候,范围在h和(h+t)/2之间且无线接近于(h+t)/2;

所以当t<(h+t)/2,直接输出2.

当t>=(h+t)/2,直接暴力肯定会超时1e10.

如果操作次数为偶数次,则平均水温一定为 mid

但此时答案操作次数为奇数次,令答案为 ans

则操作 ans-1 次时,得到水温总和为 (ans-1)*mid

第 ans 次加入的是热水 h

所以可以得到如下公式

pic2

移项得

pic3

所以可以得到答案的近似操作次数

由于操作次数一定是个整数,所以可以在求出此时的 ans 后,在 ans 周围枚举判断下哪一次操作才是真正的答案

计算误差不会太大,所以在 ±3 范围内查找即可

https://www.cnblogs.com/stelayuri/p/12985058.html

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector> 
// #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sp ' '
#define endl '
'
#define inf  0x3f3f3f3f;
#define FOR(i,a,b) for( int i = a;i <= b;++i)
#define bug cout<<"--------------"<<endl
#define P pair<int, int>
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define sca2(a,b) scanf("%d %d",&(a),&(b))
#define sca(a) scanf("%d",&(a));
#define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c))
#define sca2ll(a,b) scanf("%lld %lld",&(a),&(b))
#define scall(a) scanf("%lld",&(a));


using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;}

const double Pi = acos(-1.0);
const double epsilon = Pi/180.0;
const int maxn = 2e5+10;
int main()
{
    //freopen("input.txt", "r", stdin);
    int q;
    scanf("%d",&q);
    while(q--)
    {
        double h,c,t;
        cin>>h>>c>>t;
        double mid = (h+c)/2;
        if(t <=  mid)
        {
            cout<<2<<endl;
        }
        else 
        {
            int tmp = (mid-h)/(mid-t);
            double minn = 1000010;
            int ans = 0;
            //cout<<tmp<<endl;
            rep(i,-2,2)
            {

                int cnt = tmp + i;
                if(cnt <= 0) continue;
                if(cnt % 2 == 0) continue;
                double nowt = ((h+c)*(double)(cnt/2) + h)/cnt;
                double cha = abs(nowt-t);
                /*cout<<tmp<<sp<<nowt<<endl;*/
                if(cha < minn)
                {
                    minn = cha;
                    ans = cnt;
                }
            }            
            cout<<ans<<endl;
        }

    }
}
原文地址:https://www.cnblogs.com/jrfr/p/13048730.html