This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define all(v) v.begin(), v.end()
#define dbg(v) "[" #v " = " << (v) << "]"
template<typename T> bool maximize(T &a, T b) {
if(a < b) {
a = b;
return true;
}else return false;
}
template<typename T> bool minimize(T &a, T b) {
if(a > b) {
a = b;
return true;
}else return false;
}
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
const int N = 1e5 + 3;
int n;
double a[N], b[N];
namespace subtask1 {
double best = 0;
void backtrack(int p, double totalA, double totalB, int counter) {
if(p > n) {
maximize(best, min(totalA - (double)counter, totalB - (double)counter));
return;
}
backtrack(p + 1, totalA, totalB, counter);
backtrack(p + 1, totalA + a[p], totalB, counter + 1);
backtrack(p + 1, totalA, totalB + b[p], counter + 1);
backtrack(p + 1, totalA + a[p], totalB + b[p], counter + 2);
}
void solve() {
backtrack(1, 0, 0, 0);
cout << fixed << setprecision(4) << best << endl;
}
};
namespace subtask3 {
double prefA[N], prefB[N];
bool check(double x, int p) {
int limitA = lower_bound(prefA + 1, prefA + 1 + n, x) - prefA;
int limitB = lower_bound(prefB + 1, prefB + 1 + n, x) - prefB;
if(limitA == n + 1 || limitB == n + 1)
return false;
else return (limitA + limitB <= p);
}
void solve() {
sort(a + 1, a + 1 + n, greater<double>());
sort(b + 1, b + 1 + n, greater<double>());
for(int i = 1; i <= n; i++) prefA[i] = prefA[i - 1] + a[i];
for(int i = 1; i <= n; i++) prefB[i] = prefB[i - 1] + b[i];
double best = 0;
for(int p = 1; p <= 2 * n; p++) {
double l = 0, r = 1e9 + 3;
for(int rep = 1; rep <= 50; rep++) {
double mid = (l + r) / 2.0;
if(check(mid, p)) {
l = mid;
}else r = mid;
}
maximize(best, l - p);
}
cout << fixed << setprecision(4) << best << endl;
}
}
void process() {
if(n <= 10) {
subtask1::solve();
return;
}
subtask3::solve();
}
void input() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define task "DateALive"
if(fopen(task".INP", "r")) {
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
input();
process();
return 0;
}
Compilation message (stderr)
sure.cpp: In function 'int main()':
sure.cpp:107:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sure.cpp:108:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen(task".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |