Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2), problem: (C) Voltage Keepsake Solution In C/C++

#include <bits/stdc++.h>

using namespace std;

struct Time {
int i;
double v;
};

const int MAXN = 1e5;
int a[MAXN], b[MAXN];
Time t[MAXN+1];

int main() {
int n, p;
cin >> n >> p;
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
t[i].v = 1. * b[i] / a[i];
t[i].i = i;
}
sort(t, t+n, [](Time const & a, Time const & b){ return a.v < b.v;});
t[n].v = 1e15;
double sum_a = 0, sum_b = 0, time;
cout.precision(10);
cout << fixed;
for (int i = 0; i < n; ++i) {
int j = t[i].i;
sum_a += a[j];
sum_b += b[j];
time = (1. * sum_b / (sum_a – p));
if(time < t[i+1].v && sum_a > p) {
cout << time;
return 0;
}
}
cout << -1;
}

(Visited 43 times, 1 visits today)

About the Author:

Leave A Comment