ARC076E Connected?

题意简述:给你一个\(W*H(W,H<=10^8)\)的一个矩形,并给你\(N(N<=10^5)\)个数对,问是否存在一种可行方案将这些数对使用曲线两两相连且连线互不相交。

看到样例解释时,才搞明白曲线到底是啥。。

然后自己画了画,好像只有两个端点都在边界上的点才有可能造成无解,然后就有了一个\(O(n^2)\)的做法,即枚举点对。

可好像并不能过,然后去看了一下队爷的题解,可以顺时针/逆时针排序一遍,然后用一个栈以括号配对的方式去整就可以了。

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+10;
struct fk{int id,val;}stk[N<<1],p[N<<1];
int n,m,q,top;
bool on(int x,int y){return (x==n||y==m||x==0||y==0);}
int get(int x,int y){
    if(!x)return y;
    if(y==m)return m+x;
    if(x==n)return m+n+m-y;
    if(!y)return 2*m+2*n-x;
    return 0;
}
bool cmp(fk a,fk b){return a.val<b.val;}
int main(){
    scanf("%d%d%d",&n,&m,&q);
    for(int i=1,x,y,xx,yy;i<=q;i++){
        scanf("%d%d%d%d",&x,&y,&xx,&yy);
        if(!on(x,y)||!on(xx,yy)){--i;--q;continue;}
        p[(i<<1)-1]=(fk){i,get(x,y)};
        p[(i<<1)]=(fk){i,get(xx,yy)};
    }
    sort(p+1,p+2*q+1,cmp);
    for(int i=1;i<=2*q;i++)
        if(!top||p[i].id!=stk[top].id)stk[++top]=p[i];
        else --top;
    puts(top?"NO":"YES");
}

猜你喜欢

转载自www.cnblogs.com/yxc2003/p/10712416.html