双指针 Codeforces Round #693 (Div. 3) E题
题意:
有n个人 高度h和宽度w,如果第i个人要排在第j个人的前面,那么必须得满足wi<wj&&hi<hj或者wi<hj&&hi<wj.
对于每一个人,输出一个可以排在前面的人的下标,如果没有就输出-1,输出一种方案即可
解析:
双指针,
先按照w从小到大排序,然后遍历数组,j指针在前面找w小于i 且 h最小的那个人
int t,n,m;
struct node{
int w,h,id;
bool operator<(const node &t)const{
if(w != t.w) return w < t.w;
return h < t.h;
}
}a[N];
int ans[N];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
int x,y;
scanf("%d%d",&x,&y);
if(x > y) swap(x,y);
a[i].w = x;
a[i].h = y;
a[i].id = i;
ans[i] = -1;
}
sort(a+1,a+1+n);
int j = 1;
int cnt = inf,id = -1;
for(int i=1;i<=n;i++){
while(j < i && a[j].w < a[i].w){
if(a[j].h < cnt){
cnt = a[j].h;
id = a[j].id;
}
j++;
}
if(cnt < a[i].h) ans[a[i].id] = id;
}
for(int i=1;i<=n;i++){
printf("%d ",ans[i]);
}puts("");
}
return 0;
}