Klee's definition of playing is throwing explosives all over the place; she is particularly fond of "fish blasting'' --- throwing bombs in lakes full of fish.
Today, Klee also plans to blast fish in Starfell Lake. There are n fish in Starfell Lake. Klee will use a bomb with power x, then, every fish whose HP less than or equal to x will be blasted.
However, Klee doesn't know HP of every fish. Instead, she only knows HP of the i-th fish is a uniformly random real number in [li,ri][l_i, r_i][li,ri]. Now, Klee wants to determine minimum possible x such that the expected number of blasted fish is greater or equal to m.
Your answer will be considered correct, if its absolute or relative error does not exceed 10−410^{-4}10−4 . More formally, if your answer is a and jury's answer is b, your answer will be considered correct if ∣a−b∣max(1,b)≤10−4\frac{|a-b|}{\max(1,b)} \leq 10^{-4}max(1,b)∣a−b∣≤10−4 .
Today, Klee also plans to blast fish in Starfell Lake. There are n fish in Starfell Lake. Klee will use a bomb with power x, then, every fish whose HP less than or equal to x will be blasted.
However, Klee doesn't know HP of every fish. Instead, she only knows HP of the i-th fish is a uniformly random real number in [li,ri][l_i, r_i][li,ri]. Now, Klee wants to determine minimum possible x such that the expected number of blasted fish is greater or equal to m.
Your answer will be considered correct, if its absolute or relative error does not exceed 10−410^{-4}10−4 . More formally, if your answer is a and jury's answer is b, your answer will be considered correct if ∣a−b∣max(1,b)≤10−4\frac{|a-b|}{\max(1,b)} \leq 10^{-4}max(1,b)∣a−b∣≤10−4 .
输入描述:
The first line contains two integers n, m (1≤m≤n≤1051 \leq m \leq n \leq 10^51≤m≤n≤105) --- the number of fish and Klee's expection of the number of blasted fish.
In the following n line, each line contains two real numbers li,ril_i, r_ili,ri (0≤li≤ri≤1090 \leq l_i \leq r_i \leq 10^90≤li≤ri≤109), which means HP of the i-th fish will be a random real number in [li,ri][l_i, r_i][li,ri].
输出描述:
Output minimum possible x such that the expected number of blasted fish is greater or equal to m.
示例2
备注:
In the first example, if Klee throws a bomb with power 2.1, the probability for blasting fish 1 is 0.5, the probability for blasting fish 2 is 0.5, so the expection of the number of blasted fish is 0.5+0.5=1.
In the second example, if Klee throws a bomb with power 11441≈2.7804878048\frac{114}{41} \approx 2.780487804841114≈2.7804878048, the probability for blasting fish 1 is 3541\frac{35}{41}4135, the probability for blasting fish 2 is 641\frac{6}{41}416, the probability for blasting fish 3 is 1, so the expection of the number of blasted fish is 3541+641+1=2\frac{35}{41}+\frac{6}{41}+1=24135+416+1=2.
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
struct pairs
{
double l,r;
}seg[maxn];
const double exps=1e-5;
int n;
int maybe;
double sgs[maxn];
double per;
int i;
inline bool judge(double t)
{
per=0.0;
for(i=1;i<=n;i++){
if(t<seg[i].l)per+=0;
else if(t>seg[i].r)per+=1;
else per+=(t-seg[i].l)/(sgs[i]);
if(per>=maybe)return true;
}
return false;
}
void solve()
{
double maxx=0.0,minn=0x3f3f3f3f;
for( i=1;i<=n;i++){
scanf("%lf%lf",&seg[i].l,&seg[i].r);
sgs[i]=seg[i].r-seg[i].l;
maxx=max(maxx,seg[i].r);
minn=min(minn,seg[i].l);
}
double l=minn-1,r=maxx+1;
double mid;
while(r-l>exps)
{
mid=(l+r)/2;
if(judge(mid))r=mid;
else l=mid;
}
printf("%.10lf\n",r);
}
int main()
{
scanf("%d%d",&n,&maybe);
solve();
}