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

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

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

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

代码:

# define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize(2)
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#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 >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 >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.

你可能感兴趣的:(0x04二分,Codeforces,思维)