Codeforces Round #693 (Div. 3) E. Correct Placement 思维

传送门

题意:
在这里插入图片描述
思路: 对于每个人都有个二元组 ( x , y ) (x,y) (x,y),从题意中提取有效信息就是:当 ( x 1 , y 1 ) (x_1,y_1) (x1,y1)的最大值大于 ( x 2 , y 2 ) (x_2,y_2) (x2,y2)的最大值, ( x 1 , y 1 ) (x_1,y_1) (x1,y1)的最小值大于 ( x 2 , y 2 ) (x_2,y_2) (x2,y2)的最小值,那么 ( x 2 , y 2 ) (x_2,y_2) (x2,y2)就是符合条件的。这样我们可以对于 h h h w w w,如果 h < w h<w h<w,那么就交换 h h h w w w。让后按照 h h h排序,从头维护一个 w w w的最小值和对应的 i d id id,让后在 h h h不相等的时候更新就好啦。

//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
int ans[N];
struct Node
{
    
    
    int x,y;
    int id;
    bool operator < (const Node &W) const
    {
    
    
        if(x!=W.x) return x<W.x;
        else return y<W.y;
    }
}q[N];

int main()
{
    
    
//	ios::sync_with_stdio(false);
//	cin.tie(0);

    int _; scanf("%d",&_);
    while(_--)
    {
    
    
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
    
    
            ans[i]=-1;
            scanf("%d%d",&q[i].x,&q[i].y); q[i].id=i;
            if(q[i].x<q[i].y) swap(q[i].x,q[i].y);
        }
        sort(q+1,q+1+n);
        int mi=INF,id=-1;
        for(int i=1;i<=n;i++)
        {
    
    
            int x=q[i].x,now=i;
            while(i<=n&&x==q[i].x)
            {
    
    
                if(mi!=INF&&mi<q[i].y) ans[q[i].id]=id;
                i++;
            }
            i--;
            if(mi>q[now].y) mi=q[now].y,id=q[now].id;
        }
        for(int i=1;i<=n;i++) printf("%d ",ans[i]);
        puts("");
    }



	return 0;
}
/*

*/




猜你喜欢

转载自blog.csdn.net/m0_51068403/article/details/114451172