美团codeM预赛A轮 倒水

[编程题] 倒水

时间限制:1秒

空间限制:32768K

有一个大水缸,里面水的温度为T单位,体积为C升。另有n杯水(假设每个杯子的容量是无限的),每杯水的温度为t[i]单位,体积为c[i]升。
现在要把大水缸的水倒入n杯水中,使得n杯水的温度相同,请问这可能吗?并求出可行的最高温度,保留4位小数。
注意:一杯温度为t1单位、体积为c1升的水与另一杯温度为t2单位、体积为c2升的水混合后,温度变为(t1*c1+t2*c2)/(c1+c2),体积变为c1+c2。 
输入描述:
第一行一个整数n, 1 ≤ n ≤ 10^5
第二行两个整数T,C,其中0 ≤ T ≤ 10^4, 0 ≤ C ≤ 10^9
接下来n行每行两个整数t[i],c[i]
0 ≤ t[i], c[i] ≤ 10^4


输出描述:
如果非法,输出“Impossible”(不带引号)否则第一行输出“Possible"(不带引号),第二行输出一个保留4位小数的实数表示答案。

样例解释:往第二杯水中倒0.5升水
往第三杯水中到1升水
三杯水的温度都变成了20

输入例子1:
3
10 2
20 1
25 1
30 1

输出例子1:
Possible
20.0000
————————————————————————————————————

解题思路:若有一杯水温度大于水缸的水温一杯水温小于水缸的水温则不可能,若都小于水缸的水温,则先都上升到最高水杯的水温,然后继续上升,若都大于水缸的水温,则先下降到最低水杯的水温


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

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

struct node
{
    double t,c;
} p[100005];
double T,C;
int n;
bool ok(double x)
{
    double sum=0;
    for(int i=0; i<n; i++)
        sum=sum+(p[i].t*p[i].c-x*p[i].c)/(x-T);
    if(sum>C)
        return 0;
    return 1;
}

int main()
{


    while(~scanf("%d",&n))
    {
        scanf("%lf%lf",&T,&C);
        double mx=-1,mn=INF;
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf",&p[i].t,&p[i].c);
            mx=max(mx,p[i].t);
            mn=min(mn,p[i].t);
        }
        if((mx>=T&&mn<T)||(mx>T&&mn<=T))
        {
            printf("Impossible\n");
            continue;
        }
        if(mn>=T)
        {
            double sum=0;
            for(int i=0; i<n; i++)
            {
                sum=sum+(p[i].t*p[i].c-mn*p[i].c)/(mn-T);
            }
            if(sum>C)
                printf("Impossible\n");
            else
                printf("Possible\n%.4f\n",mn);
        }
        else
        {
            double sum=0;
            for(int i=0; i<n; i++)
            {
                int xx=(p[i].t*p[i].c-mx*p[i].c)/(mx-T);
                sum+=xx;
                p[i].t=mx;
                p[i].c+=xx;
            }
            if(sum>C)
                printf("Impossible\n");
            else
            {
                double l=mx,r=T;
                C-=sum;
                while(r-l>1e-7)
                {
                    double mid=(l+r)/2;
                    if(ok(mid)) l=mid;
                    else r=mid;
                }
                printf("Possible\n%.4f\n",l);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值