VK Cup 2017 - Round 2 -Voltage Keepsake(二分)

29 篇文章 0 订阅
5 篇文章 0 订阅

题目链接:https://codeforces.com/contest/772/my

题意:有n个装置,每个装置每秒会消耗a能量,初始自带b能量,你可以在任意时间点,为任意一个机器充值能量,每秒能充值p能量,如果有一个装置能量为0,则游戏结束,问游戏最长持续时间,如果游戏不可能结束,那么输出-1。

题解:二分时间,具体看代码。~

代码:

# define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize(2)
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
#include <unordered_map>
#include <vector>
#include <queue>
//#define int long long
#define fir first
#define sec second
using namespace std;
/*---------------------------------------------------------------------------------------------------------------------------*/
const int N = 3e5 + 5;
const double pi = acos(-1.0);
typedef long long ll;
const int mod = 998244353;
//const int mod = 1e9 + 7;
#define inf 0x3f3f3f3f

struct something {
  int num, period, time;
  bool operator < (const something& y)const {
    return y.time < time || (y.time == time && y.num < num);
  }
  something() {}
  something(int num, int period, int time) :num(num), period(period), time(time) {}
};

int dx[8] = { 0, 0, 1,-1, 1, 1,-1,-1 };
int dy[8] = { 1,-1, 0, 0, 1,-1, 1,-1 };

bool f(vector<pair<double, double> >pr, double t, double p) {
  bool ok = 0;
  double sum = t * p;
  for (auto it : pr) {
    double nt = it.sec / it.fir;
    if (nt >= t)continue;
    else sum -= (t - nt) * it.fir;
    if (sum < 0.0)return false;
  }
  return true;
}

signed main() {
  //freopen("Data.txt", "r", stdin);
  int n;
  double p;
  cin >> n >> p;
  vector<pair<double, double> >pr;
  for (int i = 0; i < n; i++) {
    double a, b;
    cin >> a >> b;
    pr.push_back(make_pair(a, b));
  }
  double l = 0.0, r = 1e10;


  for (int i = 0; i < 1000; i++) {
    double mid = (l + r) / 2.0;
    if (f(pr, mid, p))l = mid;
    else r = mid;
  }
  if (l == 1e10 * 1.0)cout << -1 << endl;
  else cout << setprecision(10) << l << endl;
  return 0;
}

题目描述:

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值