# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
784792 | dungz | Robots (IOI13_robots) | C++17 | 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.
#pragma GCC optimize ("O2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define task "task"
#define task "task"
#define prll pair<ll,ll>
#define pb push_back
#define ld long double
#define int ll
const ll MIN=-1e18,MAX=1e18,MOD=1e9+7;
int a[50005],b[50005],c[50005];
pair<int,int> q[1000005];
vector<int> f[50005];
int f3[50005],f2[50005];
int n,m,t;
bool check(int x)
{
for(int i=1;i<=m;i++)
{
f3[i]=f2[i];
}
priority_queue<int,vector<int>,greater<int>> pq;
int occ=0;
for(int i=n;i>=1;i--)
{
int need=f[i].size();
int rem=(n-i+1)*x-occ;
for(auto j:f[i]) pq.push(q[j].se);
if(need>rem)
{
while(need>rem)
{
int top=pq.top();
pq.pop();
int l=1,r=m,tob=-1;
while(l<=r)
{
int mid=(r+l)/2;
if(b[mid]>top)
{
tob=mid;
r=mid-1;
}
else l=mid+1;
}
if(tob==-1) return 0;
f3[tob]+=1;
need--;
}
occ=(n-i+1)*x;
}
else
{
occ+=need;
}
}
occ=0;
for(int i=m;i>=1;i--)
{
int need=f3[i];
int rem=(m-i+1)*x-occ;
if(need>rem) return 0;
occ+=need;
}
return 1;
}
signed main(){
#ifndef ONLINE_JUDGE
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
#endif
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m>>t;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=m;i++)
{
cin>>b[i];
}
sort(a+1,a+1+n);
sort(b+1,b+1+m);
for(int i=1;i<=t;i++)
{
cin>>q[i].fi>>q[i].se;
}
for(int i=1;i<=t;i++)
{
if(q[i].fi<a[n]) c[i]+=1;
if(q[i].se<b[m]) c[i]+=2;
if(c[i]==0)
{
cout<<-1;
return 0;
}
}
for(int i=1;i<=t;i++)
{
if(c[i]==2) continue;
int l=1,r=n,pos=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(a[mid]>=q[i].fi)
{
pos=mid;
r=mid-1;
}
else l=mid+1;
}
// cout<<q[i].fi<<" "<<pos<<endl;
f[pos].push_back(i);
}
for(int i=1;i<=t;i++)
{
if(c[i]!=2) continue;
int l=1,r=m,pos=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(b[mid]>=q[i].se)
{
pos=mid;
r=mid-1;
}
else l=mid+1;
}
f2[pos]+=1;
}
int l=1,r=t,ans=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
cout<<ans;
}
/*
*/