CodeForces - 127C Hot Bath

题目链接:CodeForces - 127C Hot Bath
题目大意:

有冷、热两个水龙头,水温分别为 t1,t2 ,调节水龙头中的流速 y1,y2 分别为 [0,x1] [0,x2] 范围内的整数,同时打开,混合后的水温满足 t=t1y1+t2y2y1+y2 ,要求得到的t不小于给定的 t0(t1<=t0<=t2) 且尽可能接近 t0 (可相等),输出满足条件的流速方案。若此时有多种流速方案,选择流速和最大的一组方案输出。

错因:没有将最后一句的题意理解清楚,流速与温度的优先性没有明确。这种题目,应该能想到一定是有一方是优先于另一方的,或是给出了判别公式,否则就无准确解。

Bob wants to open both taps so that the bath water temperature was not less than t0. However, the temperature should be as close as possible to this value. If there are several optimal variants, Bob chooses the one that lets fill the bath in the quickest way possible.

AC代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
#include <algorithm>

using namespace std;

int main(void)
{
    int x1, x2;

    double t1, t2, t0;
    double eps = 1e-6;

    while (~scanf("%lf%lf%d%d%lf", &t1, &t2, &x1, &x2, &t0))
    {
        int s1 = 0, s2 = 0;

        double up = t0 - t1, down = t2 - t0;
        if (up <= eps && down <= eps)  //t1=t2特判
            s1 = x1, s2 = x2;
        else if (down <= eps)  //t2=t0(不可除0)特判
            s1 = 0, s2 = x2;
        //else if (up <= eps)  //可以不另判
           // s1 = x1, s2 = 0;
        else
        {
            double tr = 1e9;
            for (int i = 0; i <= x1; i++)
            {
                double tmp = up / down * i;
                if (i == 0)
                    tmp = x2;
                if (tmp <= (double)x2) //(double)x2不能+eps(eps >= 1e-6);
                {
                    int t = tmp;
                    if ((double)t < tmp)
                        t++;
                    double ttmp = 1. * (1. * t1 * i + 1. * t2 * t) / (i + t);
                    if (ttmp > tr || ttmp < t0)
                        continue;
                    if (t + i >= s1 + s2 || tr - ttmp > 0)//要将s1+s2很大但比现在数据的t(水温)大的排除掉
                        s1 = i, s2 = t, tr = ttmp;
                }
            }
        }
        printf("%d %d\n", s1, s2);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值