# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
131037 | imyujin | Bodyguards (CEOI10_bodyguards) | 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 <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXR 200005
#define fi first
#define se second
typedef long long lint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
vector<pll> rv, cv;
int main() {
int R, C;
scanf("%d", &R);
rv.resize(R);
for(int i = 0; i < R; i++) scanf("%lld%lld", &rv[i].fi, &rv[i].se);
scanf("%d", &C);
cv.resize(C);
for(int i = 0; i < C; i++) scanf("%lld%lld", &cv[i].fi, &cv[i].se);
lint sumr = 0, sumc = 0;
for(int i = 0; i < R; i++) sumr += rv[i].fi * rv[i].se;
for(int i = 0; i < C; i++) sumc += cv[i].fi * cv[i].se;
if(sumr != sumc) {
printf("0");
return 0;
}
rv.push_back(make_pair(1ll << 30, 0));
sort(rv.begin(), rv.end(), greater<pll>());
sort(cv.begin(), cv.end(), greater<pll>());
vector<pll> v;
for(int i = 0; i < cv.size(); i++) {
if(i > 0 && cv[i - 1].fi == cv[i].fi) v.back().se += cv[i].se;
else v.push_back(cv[i]);
}
swap(cv, v);
for(int i = 1; i < cv.size(); i++) cv[i].se += cv[i - 1].se;
rv[0].fi = rv[0].fi * rv[0].se;
for(int i = 1; i < rv.size(); i++) {
rv[i].fi = rv[i - 1].fi + rv[i].fi * rv[i].se;
rv[i].se += rv[i - 1].se;
}
/*
for(int i = 0; i < cv.size(); i++) printf("(%lld, %lld)", cv[i].fi, cv[i].se);
printf("\n");
for(int i = 0; i < rv.size(); i++) printf("(%lld, %lld)", rv[i].fi, rv[i].se);
printf("\n");
*/
lint A = 0ll, h = 0ll;
int p = cv.size() - 1;
for(int i = 1; i < rv.size(); i++) {
//printf("h = %lld, A = %lld, p = %d\n", h, A, p);
if(A + cv[p].se < rv[i - 1].fi + (rv[i].fi - rv[i - 1].fi) / rv[i].se) {
printf("0");
return 0;
}
while(p >= 0 && cv[p].fi <= rv[i].se) {
A += (cv[p].fi - h) * cv[p].se;
h = cv[p].fi;
p--;
}
if(p < 0) break;
A += (rv[i].se - h) * cv[p].se;
h = rv[i].se;
if(A < rv[i].fi) {
printf("0");
return 0;
}
}
printf("1");
return 0;
}