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;
}