#include <bits/stdc++.h>using namespacestd;structin{Long LongA, B; DoubleT;} s[100005];BOOLCMP (in P, in Q) {if(P.T <q.t)return true; Else if(p.t = = q.t && P.a <q.a)return true; return false;}intMain () {Long LongN, p; scanf ("%lld%lld", &n, &p); Long LongSUM1 =0; for(Long Longi =0; I < n; i++) {scanf ("%lld%lld", &S[I].A, &s[i].b); Sum1+=s[i].a; S[I].T= s[i].b *1.0/s[i].a; } if(Sum1 <=p) {printf ("-1\n"); } Else{sort (s, S+N, CMP); //for (int i = 0; i < n; i++)//printf ("%lld--a%lld--b%.9f--t\n\n", S[i].a, s[i].b, s[i].t); DoubleTime = s[0].t; DoublePOW = s[0].T *p; DoubleDis, x; Long Longsum = s[0].a; Long Longi =1; while(I <n) {dis= s[i].t-Time ; if(Sum >p) {x= POW/(Sum-p); if(x <=dis) {printf ("%.10f\n", Time +x); Break; }} POW= Pow + dis * (P-sum); Sum+=s[i].a; time=s[i].t; I++; } if(i = =N) {x= POW/(Sum-p); printf ("%.10f\n", Time +x); } } return 0;}
Codeforces 801C Voltage Keepsake Structure sort order + greedy