CF - 801C. Voltage Keepsake - 二分

1.题目描述:

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
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.

2.题意概述:

某装置是n个元件的串联,你有一个无限多能量的充电宝,每秒钟能给某元件充电的电量为p,告诉你每个元件的初始电量,和每个元件每秒消耗多少电量,且每个元件的电量上限为无限(这个地方没好好看题结果卡了很久QAQ),你可以选择任意的充电策略充电任意的时间,问你整个装置最多能工作多少秒,如果能一直工作下去则输出-1

3.解题思路:

很容易想到二分时间。

首先是怎么判断能永久工作下去呢?只要所有元件每秒耗电量加起来都不大于p(想想为什么?)。

然后是二分函数怎么判断能否工作到这个时间?可以先算出每个元件在初始电量情况下能持续的时间。然后O(n)地算出要持续time时间需要充的电,再O(n)地更新电池占用的充电时间,如果小于0则说明在当前约束下肯定无解。

4.AC代码:

#include <bits/stdc++.h>
#define INF 1e18
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-4
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int n, p;
struct node
{
	int a, b;
	double t;
} d[maxn];
bool judge(double time)
{
	for (int i = 0; i < n; i++)
		d[i].t = d[i].b;
	for (int i = 0; i < n; i++)
		d[i].t -= time * d[i].a;
	for (int i = 0; i < n; i++)
		if (d[i].t < 0)
		{
			time -= -1.0 * d[i].t / p;
			if (time < 0)
				return 0;
		}
	return 1;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	while (~scanf("%d%d", &n, &p))
	{
		double all = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d", &d[i].a, &d[i].b);
			all += d[i].a;
		}
		if (all <= p)
		{
			puts("-1");
			continue;
		}
		double l = 0, r = INF, ans = 0;
		while (r - l >= eps)
		{
			double mid = (l + r) / 2;
			if (judge(mid))
			{
				ans = mid;
				l = mid;
			}
			else
				r = mid;
		}
		printf("%.10f\n", ans);
	}
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值