题意:

给出一个$R\times C$的棋盘,其中$1$到$N$之间的每个正整数都会在棋盘上出现两次,第$i$个数出现的位置是$(X_{i,1},Y_{i,1})$和$(X_{i,2},Y_{i,2})$,现在目的是把每一对相同的数用线(粗细忽略不计)连起来,且线不能相交也不能越过棋盘边界,求是否能完成。

$1\leq R,C\leq 10^8$

$1\leq N\leq 10^5$

题解:

看上去是神仙题,实际上很假。。。

大家有没有玩过麻将连连看那种小游戏?题意中的连线意义就差不多。首先如果把这个棋盘扩展到无限大,即没有棋盘边界的限制,显然一定能满足条件。因为棋盘边上的数字连的线肯定可以在向外连足够远之后连回来,而内部的线由于可以跨越每个格子的边界,必定可以满足条件。(正确性感性理解一下?)

那么有了边界限制之后,就只用考虑在两个位置都在边界上的那些数字,把这些数字看成一对括号,如果整个边界上按顺序(顺时针或逆时针)能构成一个合法括号序列,那么就能满足,否则就不行。画个图感受一下:

如图,左图是非法的而右图是合法的。那这个东西直接用栈判断一下就好了。。。先把所有位置排序,然后如果现在位置的数字和栈顶相等则弹出栈顶,否则把当前数字压入栈,最后判断栈是否为空即可。

ps:这题细节极其恶心!写挂了五六次才过样例。。。(可能是我写法比较挫)

代码:

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#define inf 2147483647
#define eps 1e-9
using namespace std;
typedef long long ll;
struct node{
int x,id;
}li[][];
int r,c,n,x,y,xx,yy,nw,tot[];
stack<int>st;
bool cmp1(node a,node b){
return a.x<b.x;
}
bool cmp2(node a,node b){
return a.x>b.x;
}
int main(){
scanf("%d%d%d",&r,&c,&n);
for(int i=;i<=n;i++){
scanf("%d%d%d%d",&x,&y,&xx,&yy);
if((x&&y&&x!=r&&y!=c)||(xx&&yy&&xx!=r&&yy!=c))continue;
if(!x)nw=;
else if(x==r)nw=;
else if(!y)nw=;
else if(y==c)nw=;
if(nw==||nw==)li[nw][++tot[nw]]=(node){y,i};
else li[nw][++tot[nw]]=(node){x,i};
if(!xx)nw=;
else if(xx==r)nw=;
else if(!yy)nw=;
else if(yy==c)nw=;
if(nw==||nw==)li[nw][++tot[nw]]=(node){yy,i};
else li[nw][++tot[nw]]=(node){xx,i};
}
for(int i=;i<;i++){
if(i==||i==)sort(li[i]+,li[i]+tot[i]+,cmp1);
else sort(li[i]+,li[i]+tot[i]+,cmp2);
for(int j=;j<=tot[i];j++){
if(!st.empty()&&li[i][j].id==st.top())st.pop();
else st.push(li[i][j].id);
}
}
if(st.empty())puts("YES");
else puts("NO");
return ;
}

[arc076e]connected?的更多相关文章

  1. NOIp2018模拟赛四十四

    加量不加价?! 昨晚看时间变成了3.5h以为终于变成了正常难度,结果还是国家集训队作业... A题看起来很神仙,B题看上去很神仙,C题一看就知道很神仙: 结果发现B是假题,放榜后发现A也是假题,C是Y ...

  2. 【arc076E】Connected?

    Portal -->arc076E Description 给你一个\(R*C\)的矩形,矩形中某些格子的端点上填了\(1\sim n\)这\(n\)个数字,每个数字出现了恰好两遍,现在要将每一 ...

  3. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  4. PTA Strongly Connected Components

    Write a program to find the strongly connected components in a digraph. Format of functions: void St ...

  5. poj 1737 Connected Graph

    // poj 1737 Connected Graph // // 题目大意: // // 带标号的连通分量计数 // // 解题思路: // // 设f(n)为连通图的数量,g(n)为非连通图的数量 ...

  6. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  7. Windows Phone 8 解锁提示IpOverUsbSvc问题——IpOverUsbEnum返回No connected partners found解决方案

    我的1520之前总是无法解锁,提示:IpOverUsbSvc服务没有开启什么的. 根据网上网友的各种解决方案: 1. 把手机时间设置为当前时间,并且关闭“自动设置” 2. 确保手机接入了互联网 3.确 ...

  8. POJ1737 Connected Graph

    Connected Graph Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3156   Accepted: 1533 D ...

  9. [LintCode] Find the Weak Connected Component in the Directed Graph

      Find the number Weak Connected Component in the directed graph. Each node in the graph contains a ...

随机推荐

  1. Win10 UI入门 导航滑动条 求UWP工作

    借鉴了 段博琼 大哥写的导航滑动,自己实现了一个类似安卓 IOS 导航滑动条 支持等比例 分割 tabView 支持动画滑动 效果如下图 WYGrid 你可以想象一个GridView  itemsWr ...

  2. 安装django和selenium

    安装很简单(前提是python已经安装),命令窗口直接输入pip install django,回车就会自动进行安装,selenium也是一样pip install selenium 启动django ...

  3. node——由新闻列表跳转到新闻详情页

    当我们在浏览新闻列表页面的时候,想要看感兴趣的新闻内容,需要到详情页面去查看内容. 在之前写好了新闻列表页面,现在需要做列表页面到详情页面的跳转,需要考虑一下问题 1.点击新闻列表某一项跳转到详情页面 ...

  4. js正则表达式注册页面表单验证

    可以这样校验 <html> <head> <meta http-equiv="Content-Type" content="text/htm ...

  5. laravel报错:MassAssignmentException

    报这种错误是因为没有设置白名单或者黑名单.在使用fill填充时,需要设置白/黑名单. $model->fill($params);return $model->save(); 找到对应的m ...

  6. iText操作pdf(生成,导入图片等)

    生成pdf有很多种方法,用pdfbox也很方便,今天我要写的是用iText 主要在pom.xml中配置的jar包如下 <dependency> <groupId>com.low ...

  7. [TypeScript] Generic Functions, class, Type Inference and Generics

    Generic Fucntion: For example we have a set of data and an function: interface HasName { name: strin ...

  8. unity3d 延迟运行脚本语句

    在Unity3D中.有yield语句它负责延迟操作,yield return WaitForSeconds(3.0); //等待 3 秒 查看unity3d脚本手冊,使用方法须要在对应的格式. 以下代 ...

  9. mysql的查询练习1

    1.多表查询

  10. perl getopt 用法

    我们在linux经常常使用到一个程序须要增加參数,如今了解一下perl中的有关控制參数的函数.getopt.在linux有的參数有二种形式.一种是--help,还有一种是-h.也就是-和--的分别.- ...