Codeforces gym 101149 G 想法

来源:互联网 发布:数据库安全性论文 编辑:程序博客网 时间:2024/05/21 21:40

Of Zorcs and Axes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Zorcs don't like other species. Zorcs like massive curve axes. Right now each of them is going to choose a suitable axe and show beings of other races entrenched nearby that they are not ready to peaceful cultural assimilation. There are n zorcs and m axes in total, and the i-th zorc wants to take an axe with the weight not less than ai and the curvature not less than bi, while the j-th axe has the weight wjand the curvature cj. Zorcs don't like programming problems. Zorcs like massive curve axes. So it's your task to determine which zorc must choose which axe.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of zorcs.

Each of the next n lines contains two space-separated integers: ai and bi (1 ≤ ai, bi ≤ 109) — the minimal weight and curvature of the axe that suits the i-th zorc.

The next line contains a single integer m (1 ≤ m ≤ 2·105) — the number of axes.

Each of the next m lines contains two space-separated integers: wj and cj (1 ≤ wj, cj ≤ 109) — the weight and the curvature of the j-th axe.

Output

Output n integers, the i-th of which must be the number of axe that should be given to the i-th zorc to fulfill all his requirements. Of course, the same axe can't be given to two different zorcs.

If there is no possibility to distribute axes, fulfilling all requirements of all zorcs, output a single number  - 1 instead of that.

Examples
input
33 45 12 645 74 53 35 2
output
2 4 1
input
22 54 325 63 4
output
-1

题意:给你n对数  m对数  找出对应的下标  使得n[i]的x和y都小于等于m[i]的x和y  输出方案  不存在输出-1


题解:将n和m都从大到小排序  然后for n  对于每个n  将大于等于当前对x的m全部加到set里面  然后随便找一个my比当前y大的即可  如果不存在 -1


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<set>using namespace std;struct node{int x,y,lab;bool operator<(const node& a)const{if(x==a.x&&y==a.y)return lab<a.lab;if(x==a.x)return y>a.y;return x>a.x;}}e[200005],e1[200005];int ans[200005];struct nodes{int y,lab;bool operator<(const nodes& a)const{if(y==a.y)return lab<a.lab;return y<a.y;}};multiset<nodes>sp;multiset<nodes>::iterator it,it1;int main(){int n,m,i,j,num,flag=1;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d%d",&e[i].x,&e[i].y);e[i].lab=i;}sort(e+1,e+1+n);scanf("%d",&m);for(i=1;i<=m;i++){scanf("%d%d",&e1[i].x,&e1[i].y);e1[i].lab=i;}sort(e1+1,e1+1+m);nodes f;int now=1;for(i=1;i<=n;i++){while(e1[now].x>=e[i].x&&now<=m){f.y=e1[now].y;f.lab=e1[now].lab;sp.insert(f);now++;}f.y=e[i].y;f.lab=e[i].lab;sp.insert(f);it=sp.find(f);it1=sp.end();it1--;if(it==it1){if(it!=sp.begin()){it--;if((*it).y>=f.y){ans[f.lab]=(*it).lab;sp.erase(it);it1=sp.find(f);sp.erase(it1);}else{flag=0;break;}}else{flag=0;break;}}else{if(it!=sp.begin()){it--;if((*it).y>=f.y){ans[f.lab]=(*it).lab;sp.erase(it);it1=sp.find(f);sp.erase(it1);}else{it++;it++;ans[f.lab]=(*it).lab;sp.erase(it);it1=sp.find(f);sp.erase(it1);}}else{it++;ans[f.lab]=(*it).lab;sp.erase(it);it1=sp.find(f);sp.erase(it1);}}}if(!flag)printf("-1\n");else{for(i=1;i<=n;i++)printf("%d ",ans[i]);printf("\n");}return 0;}


0 0
原创粉丝点击