CodeM美团点评编程大赛A轮 C.倒水

来源:互联网 发布:mac mini玩游戏怎么样 编辑:程序博客网 时间:2024/06/01 10:17

CodeM美团点评编程大赛A轮 C.倒水


题目

倒水

时间限制: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

输入例子:

310 220 125 130 1

输出例子:

Possible20.0000

题解

作为一名蒟蒻,我果断写了比较简单的方法:

首先,判断最大温度值与最小温度值是否在大水缸中水的温度值上下(因为只能由大水缸向杯子内倒水)

然后将n>=maxn和minn>=n分开来判断

①: n>=maxn

如果所有的水都倒在一起的温度都无法达到maxn的值,那么肯定是不可能实现的,否则就输出所有水倒在一起时的温度

②:minn>=n

如果所有的水都倒在一起的温度还比minn的值大,那么这也是无法实现的,否则就输出minn

当然,还有各种dalao们写的二分(%%%)


代码

#include<cstdio>using namespace std;int t;long long n,m,x,y,totn,totm,maxn,minn;double ans;int readln(){    int x=0;    char ch=getchar();    while (ch<'0'||ch>'9') ch=getchar();    while ('0'<=ch&&ch<='9') x=x*10+ch-48,ch=getchar();    return x;}int main(){    scanf("%d",&t);    scanf("%lld%lld",&n,&m);    totn=n*m;totm=m;maxn=0;minn=1e9;    for (int i=1;i<=t;i++)    {        scanf("%lld%lld",&x,&y);        totn+=x*y;totm+=y;        if (x<minn) minn=x;        if (x>maxn) maxn=x;    }    if (maxn>n&&n>minn) {        printf("Impossible");        return 0;    }    if (n>=maxn) {        if (totn<maxn*totm) {            printf("Impossible");            return 0;        }         else {            printf("Possible\n");            ans=totn;            printf("%.4lf",ans/totm);            return 0;         }    }    if (n<=minn) {        if (totn>minn*totm)        {            printf("Impossible");            return 0;        }        else {            printf("Possible\n");            ans=minn;            printf("%.4lf",ans);            return 0;        }    }}
原创粉丝点击