cf Water Taps

n个水龙头,每个有温度,和他的最大出水量,混合后的温度为出水量乘温度/总水量.
问你要获得目标温度,最多放多少水.
一开始总想着..某种数学方式解决,然后失败…
后来想着应该是二分吧,但是我总是想着 logn * n的方式来直接check答案,但是事实上是n*logn的,首先.是一个贪心的做法(贪心的做法很多都是先无脑放入,然后抠出来),把所有水龙头都拉满,然后这时显然温度会不平衡,那我们就可以把另一侧开始减少,至于这个减少量,可以用二分,但是其实可以直接计算.不过事实上二分也只有一次.
这题很奇怪,会卡时间,好像还会卡精度,因为用的是long double,但事实上,这题还是注意他的贪心思想.
我的做法非常蠢…

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
using namespace std;
#define debug(x) //std::cerr << #x << " = " << (x) << std::endl
typedef long long LL;
const int MAXN = 2e5+17;
long double a[MAXN],t[MAXN];
int main(int argc ,char const *argv[])
{
    #ifdef noob
    freopen("Input.txt","r",stdin);freopen("Output.txt","w",stdout);
    #endif  
    ios::sync_with_stdio(false);
    long double n,k,up=0,down=0;
    cin>>n>>k;
    for (int i = 0; i < n; ++i)
    {
        cin>>a[i];
        down+=a[i];
    }
    vector<pair<long double,long double > > vec;
    for (int i = 0; i < n; ++i)
    {
        cin>>t[i];
        up+=a[i]*t[i];
        vec.push_back({t[i],a[i]});
    }
    sort(vec.begin(), vec.end());
    if(up/down>k)
    {
        for (int i = n-1; i > -1 ; --i)
        {
            long double na = vec[i].second,nt = vec[i].first;
            if(nt<=k) break;
            if((up-na*nt)/(down-na)>k)
            {
                up -= na*nt;
                down -= na;
                continue;
            }
            long double l = 0,r = na;
            int tms = 70;
            while(tms--)
            {
                long double mid = (l+r)/2;
                if((up-(mid*nt))/(down-mid)>=k)
                    l = mid;
                else
                    r = mid;
            }
            up-=(l*nt);
            down-=(l);
        }
    }
    else if(up/down<k)
    {
        for (int i = 0; i < n ; ++i)
        {
            long double na = vec[i].second,nt = vec[i].first;
            if(nt>=k) break;
            long double l = 0,r = na;
            if((up-na*nt)/(down-na)<k)
            {
                up -= na*nt;
                down -= na;
                continue;
            }
            int tms = 70;
            while(tms--)
            {
                long double mid = (l+r)/2;
                if((up-(mid*nt))/(down-mid)<=k)
                    l = mid;
                else
                    r = mid;
            }
            up-=(l*nt);
            down-=(l);
        }
    }
    cout<<fixed<<setprecision(8)<<down<<endl;
    return 0;   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值