Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) C. Voltage Keepsake

直接二分即可,右端點的初始值要設大一些,然後直接循環若干次,若大於一個足夠大的數就輸出-1,否則就輸出左端點

#include<bits/stdc++.h>
using namespace std;
int n,p,a[100005],b[100005];
double l=0,r=1e15;
bool check(double x)
{
    double ret=0;
    for(int i=0;i<n;i++)ret+=max((x*a[i]-b[i])/p,0.0);
    return ret<=x;
}
int main()
{
    scanf("%d%d",&n,&p);
    for(int i=0;i<n;i++)scanf("%d%d",&a[i],&b[i]);
    for(int i=0;i<200;i++)
    {
        double m=(l+r)/2;
        if(check(m))l=m;
        else r=m;
    }
    if(l>1e14)cout<<-1;
    else printf("%.10lf",l);
    return 0;
}


發佈了50 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章