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;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hhjian6666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值