codeforces 126A Hot Bath

题目链接:http://codeforces.com/problemset/problem/126/A

题意:两个水龙头出水温度为t1、t2,出水速度y1、y2在0到x1、x2之间调节,根据公式t = (t1y1+t2y2)/(y1+y2) 算出水温,给定t0,要求温度在t0以上并尽可能接近t0,满足条件的多组数据中选出水速度最快的。要求输出为整数。所有数据范围为1e6内int。

题解:很容易想到直接数学计算,但是由于要求输出的是int,很难处理精度、误差。可以先假定水都放满,若温度过低则降低水温低的出水速度,反之亦然。每次变化都为1,则复杂度为x1+x2。记录最低差值的即可。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <stack>
#include <set>
#include <queue>
#include <functional>
#include <map>
#include <bitset>

#define INF 0x7fffffff
#define REP(i,j,k) for(int i = j;i <= k;i++)
#define squr(x) (x) * (x)
#define lowbit(x) (x&(-x))
#define getint(x) scanf("%d", &(x))

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

const double eps = 1e-15;

double t1, t2, t0, t;
LL x1, x2, yy1, yy2;
double ansy1, ansy2;
double del = -1;

int main(int argc, const char * argv[]) {
    cin >> t1 >> t2 >> x1 >> x2 >> t0;

    yy1 = x1; yy2 = x2;
    while (yy1 > 0 || yy2 > 0) {
        t = (t1 * yy1 + t2 * yy2) / (yy1 + yy2);
        if (t - t0 >= -eps && (del == -1 || fabs(t - t0) < del)) {
            del = fabs(t - t0);
            ansy1 = yy1;
            ansy2 = yy2;
        }
        //cout << yy1 << " " << yy2 << " " << t - t0 << endl;
        if (fabs(t - t0) < eps) {
            break;
        } else if (t - t0 >0) {
            yy2--;
        } else {
            yy1--;
        }
    }

    cout << (int)ansy1 << " " << (int)ansy2 << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值