CodeForces

来源:互联网 发布:java files.write 编辑:程序博客网 时间:2024/06/08 15:45

题目链接: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;}