Voltage Keepsake CodeForces - 801C (贪心 || 二分)

C. Voltage Keepsake
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 0001 ≤ p ≤ 109) — the number of devices and the power of the charger.

This is followed by n lines which contain two integers each. Line i contains 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
Copy
2 1
2 2
2 1000
output
Copy
2.0000000000
input
Copy
1 100
1 1
output
Copy
-1
input
Copy
3 5
4 3
5 2
6 1
output
Copy
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.

这题网上大多都用的二分

先用贪心做一遍

贪心的做法是 先用初始使用时间排序 然后 如果前i个在边充电边使用的情况下  能达到(i + 1)的使用时间 那就把前(i + 1)合并为一个仪器

然后再判断前(i + 1)个在边充电边使用的情况下 能否使用时间达到 第(i + 2)个

#include 
using namespace std;
const int maxn = 100010, INF = 0x7fffffff;
typedef long long LL;

struct node
{
    double t, a, b;
}Node[maxn];

double cmp(node A, node B)
{
    return A.t < B.t;
}

int main()
{
    int n, p;
    LL sum = 0;
    cin >> n >> p;
    for(int i = 0; i < n; i++)
    {
        cin >> Node[i].a >> Node[i].b;
        sum += Node[i].a;
        Node[i].t = Node[i].b / (double) Node[i].a;
    }
    
    if(p >= sum) return puts("-1"), 0; //只有在充电的功率大于消耗的功率的时候 才能无限使用
    
    sort(Node, Node + n, cmp);   //按初始使用时间排序
    double pow = Node[0].t * p, x_p = Node[0].a, ret = 0;
    bool flag = 0;
    int i;
    for(i = 0; i < n - 1; i++)
    {
        if(pow + (Node[i + 1].t - Node[i].t) * p > (Node[i + 1].t - Node[i].t) * x_p)
        {
            pow += (Node[i + 1].t - Node[i].t) * (p - x_p), x_p += Node[i + 1].a;
        }
        else
        {
            break;
        }
    }
    ret = pow / (double) (x_p - p);  //存的功率 除 净消耗功率 是不是就是使用时间
    ret += Node[i].t;  //然后再加上初始的使用时间
    printf("%.10f\n", ret);


    return 0;
}

 

 

 

 

二分 :

#include 
#define eps 1e-9
using namespace std;
const int maxn = 100010, INF = 0x7fffffff;
typedef long long LL;
int n, p;
int a[maxn], b[maxn];


double check(double m)
{
    double sum = 0;
    for(int i = 0; i < n; i++)
    {
        if(b[i] - a[i] * m < 0)
            sum += a[i] * m - b[i];
    }
    return p * m > sum;
}



int main()
{
    LL sum_a = 0, sum_b = 0;
    cin >> n >> p;
    for(int i = 0; i < n; i ++)
    {
        cin >> a[i] >> b[i];
        sum_a += a[i];
    }
    if(p >= sum_a) return puts("-1"), 0;

    double l = 0, r = 1e18, t, m;
    while(r - l > eps)
    {
        m = (l + r) / (double)2;
        if(check(m)) l = m;
        else r = m;
    }
    printf("%.10f\n", m);

    return 0;
}

 

转载于:https://www.cnblogs.com/WTSRUVF/p/10631226.html

你可能感兴趣的:(Voltage Keepsake CodeForces - 801C (贪心 || 二分))