codeforce Round #409(div 2) C. Voltage Keepsake(2점!! 2점대법tql)

3927 단어 #codeforces
C. Voltage Keepsake
You have n devices that you want to use simultaneously.
The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power stored. All devices can store an arbitrary amount of power.
You have a single charger that can plug to any single device. The charger will add p units of power per second to a device. This charging is continuous. That is, if you plug in a device for λseconds, it will gain λ·p units of power. You can switch which device is charging at any arbitrary unit of time (including real numbers), and the time it takes to switch is negligible.
You are wondering, what is the maximum amount of time you can use the devices until one of them hits 0 units of power.
If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.
Input
The first line contains two integers, n and p (1 ≤ n ≤ 100 000, 1 ≤ p ≤ 109) — the number of devices and the power of the charger.
This is followed by n lines which contain two integers each. Line icontains the integers ai and bi (1 ≤ ai, bi ≤ 100 000) — the power of the device and the amount of power stored in the device in the beginning.
Output
If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.
Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.
Namely, let's assume that your answer is a and the answer of the jury is b. The checker program will consider your answer correct if .
Examples
Input
2 1
2 2
2 1000

Output
2.0000000000

Input
1 100
1 1

Output
-1

Input
3 5
4 3
5 2
6 1

Output
0.5000000000

Note
In sample test 1, you can charge the first device for the entire time until it hits zero power. The second device has enough power to last this time without being charged.
In sample test 2, you can use the device indefinitely.
In sample test 3, we can charge the third device for 2 / 5 of a second, then switch to charge the second device for a 1 / 10 of a second.
 
제목: n개의 가전제품의 소모 속도와 기존의 전기량, 그리고 하나의 충전기를 드리겠습니다. 이미 알고 있는 충전기의 충전 속도, 충전기는 모든 가전제품을 충전할 수 있습니다. 마지막으로 모든 가전제품이 동시에 견지할 수 있는 가장 긴 시간을 물어보세요.
사고방식: 2분의 1의 답안을 가지고 check을 통해 답안이 합리적인지 판단한다. 합리적인 조건은 충전기가 모든 가전제품이 이 시간 안에 작업을 멈추지 않거나 충전기가 필요하지 않다는 것을 보장하는 것이다.
AC 코드:
#include
using namespace std;

const int maxn = 1e5 + 10;
const double eps = 1e-7;
int a[maxn];
int b[maxn];
int n,p;

bool check(double tim){
    double leftim = tim;
    for(int i = 0;i < n;i ++){
        double tot = a[i] * 1.0 * tim;
        double need = tot - b[i] * 1.0;
        if(need > p * leftim + eps && need > eps) return 0;     /// need > eps          need, need       0 
        if(leftim > need / p && need > eps) leftim -= need / p;
    }
    return 1;
}

int main()
{
    while(~scanf("%d%d",&n,&p)){
        for(int i = 0;i < n;i ++)
            scanf("%d %d",&a[i],&b[i]);
        double l = 0.0,r = 1e10;
        double t = -1.000;
        double flag = 0;                ///              
        int T = 100;
        while(T --){
            double mid = (l + r) / 2;
            if(check(mid)){
                l = mid;
                t = mid;
            }
            else{
                flag = 1;
                r = mid;
            }
        }
        if(!flag)
            printf("-1
"); else printf("%.6lf
",t); } return 0; }

좋은 웹페이지 즐겨찾기