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;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll INF = 1e9+10;
ll r, c;
int solve(vector<pii> vr, vector<pii> vc) {
ll sz=vc.size(), pc[sz+10]={}, pcnt[sz+10]={};
for (int i=0; i<sz; ++i) pc[i+1]=pc[i]+vc[i].first*vc[i].second;
for (int i=0; i<sz; ++i) pcnt[i+1]=pcnt[i]+vc[i].second;
ll sm=0, cnt=0, ls=0;
for (auto u : vc) ls+=u.first*u.second;
for (auto u : vr) {
sm+=u.first*u.second, cnt+=u.second;
int l=0, r=sz, res=sz-1;
while (l <= r) {
int m=(l+r)/2;
if (vc[m].first >= cnt) l=m+1, res=m;
else r=m-1;
}
if (vc[res].first < cnt) --res;
ll g=1ll*pcnt[res+1]*cnt+(ls-pc[res+1]);
if (g < sm) return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
vector<pii> vr, vc;
cin>>r;
ll rsm=0, csm=0;
for (int i=1; i<=r; ++i) {
ll x, val; cin>>val>>x;
vr.push_back({val, x});
rsm+=val*x;
} sort(vr.begin(), vr.end(), greater<pii>());
cin>>c;
for (int i=1; i<=c; ++i) {
ll x, val; cin>>val>>x;
vc.push_back({val, x});
csm+=val*x;
} sort(vc.begin(), vc.end(), greater<pii>());
if (rsm != csm) cout<<0<<"\n";
else cout<<solve(vr, vc)<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |