# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
779296 | math_rabbit_1028 | Robots (IOI13_robots) | C++14 | 0 ms | 0 KiB |
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;
int a, b, t;
struct toy {
int w, s;
bool operator< (const toy &other) const {
if (w == other.w) return s < other.s;
return w < other.w;
}
} toys[1010101];
bool compare(toy t1, toy t2) {
if (t1.s == t2.s) return t1.w > t2.w;
return t1.s > t2.s;
}
int wrbt[50505], srbt[50505];
priority_queue<toy> pq, pq2;
bool solve(int d) {
while (!pq.empty()) pq.pop();
while (!pq2.empty()) pq2.pop();
int p = 1;
long long remain = 0;
for (int i = b; i >= 0; i--) {
while (p <= t) {
if (toys[p].s >= srbt[i]) {
pq2.push(toys[p]);
p++;
}
else break;
}
while (!pq2.empty()) {
if (remain > 0) {
pq2.pop();
remain--;
}
else break;
}
while (!pq2.empty()) {
pq.push(pq2.top());
pq2.pop();
}
remain += d;
}
long long cnt = 0;
for (int i = a; i >= 0; i--) {
while (!pq.empty()) {
if (pq.top().w >= wrbt[i]) {
cnt++;
pq.pop();
}
else break;
}
if (cnt > (long long)d * (a - i)) return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> a >> b >> t;
for (int i = 1; i <= a; i++) cin >> wrbt[i];
for (int i = 1; i <= b; i++) cin >> srbt[i];
for (int i = 1; i <= t; i++) cin >> toys[i].w >> toys[i].s;
sort(wrbt + 1, wrbt + a + 1);
sort(srbt + 1, srbt + b + 1);
sort(toys + 1, toys + t + 1, compare);
for (int i = 1; i <= t; i++) {
if (toys[i].w >= wrbt[a] && toys[i].s >= srbt[b]) {
cout << "-1\n";
return 0;
}
}
int st = 1, ed = t;
//assert(solve(t));
while (st < ed) {
int mid = (st + ed) / 2;
if (solve(mid)) {
ed = mid;
}
else {
st = mid + 1;
}
}
cout << st << "\n";
return 0;
}