6570: Connected?(思维+STL(栈))

6570: Connected?

时间限制: 1 Sec  内存限制: 128 MB
提交: 37  解决: 19
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Snuke is playing a puzzle game. In this game, you are given a rectangular board of dimensions R×C, filled with numbers. Each integer i from 1 through N is written twice, at the coordinates (xi,1,yi,1) and (xi,2,yi,2).
The objective is to draw a curve connecting the pair of points where the same integer is written, for every integer from 1 through N. Here, the curves may not go outside the board or cross each other.
Determine whether this is possible.

Constraints
1≤R,C≤108
1≤N≤105
0≤xi,1,xi,2≤R(1≤i≤N)
0≤yi,1,yi,2≤C(1≤i≤N)
All given points are distinct.
All input values are integers.

输入

Input is given from Standard Input in the following format:
R C N
x1,1 y1,1 x1,2 y1,2
:
xN,1 yN,1 xN,2 yN,2

输出

Print YES if the objective is achievable; print NO otherwise.

样例输入

4 2 3
0 1 3 1
1 1 4 1
2 0 2 2

样例输出

YES

提示


The above figure shows a possible solution.

给你n条线  问你他们是否 不会出现线段交叉的现象。

只有这条线的两个端点都在边缘的时候才可能出现两条线交叉的情况,只需要考虑那些都在边缘的点,然后排序,用栈模拟下大的那个值出栈的位置关系即可。当前到右端点的时候一定是右端点在栈顶,否则肯定是出现了交叉。

//Sinhaeng Hhjian
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<stack>
#include<math.h>
#include<set>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int maxn = 1e5+5;
int r, c, n;
struct node{
    int u, v, f;
}st[maxn*2];
int cmp(node a, node b){
    return a.u<b.u;
}
int dis(int x, int y){
    if(x==0)
        return y;
    else if(y==c)
        return  c+x;
    else if(x==r)
        return r+c+c-y;
    else if(y==0)
        return r+r+c+c-x;
    else
        return -1;
}
int main()
{
    int cnt=0;
    scanf("%d%d%d", &r, &c, &n);
    for(int i=0;i<n;i++){
        int a, b, cc, d;
        scanf("%d%d%d%d", &a, &b, &cc, &d);
        int x=dis(a, b), y=dis(cc, d);
        if(x==-1 || y==-1)
            continue;
        if(x>y)
            swap(x, y);
        st[cnt].f=1;
        st[cnt].u=x;
        st[cnt++].v=y;
        st[cnt].f=2;
        st[cnt].u=y;
        st[cnt++].v=x;
    }
    sort(st, st+cnt, cmp);
    stack<int>s;
    for(int i=0;i<cnt;i++){
        if(st[i].f==1)
            s.push(st[i].v);
        else if(s.top()==st[i].u)
            s.pop();
        else{
            printf("NO\n");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_30796379/article/details/81382242