[arc076e]connected?
题意:
给出一个$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?的更多相关文章
- NOIp2018模拟赛四十四
加量不加价?! 昨晚看时间变成了3.5h以为终于变成了正常难度,结果还是国家集训队作业... A题看起来很神仙,B题看上去很神仙,C题一看就知道很神仙: 结果发现B是假题,放榜后发现A也是假题,C是Y ...
- 【arc076E】Connected?
Portal -->arc076E Description 给你一个\(R*C\)的矩形,矩形中某些格子的端点上填了\(1\sim n\)这\(n\)个数字,每个数字出现了恰好两遍,现在要将每一 ...
- [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), ...
- PTA Strongly Connected Components
Write a program to find the strongly connected components in a digraph. Format of functions: void St ...
- poj 1737 Connected Graph
// poj 1737 Connected Graph // // 题目大意: // // 带标号的连通分量计数 // // 解题思路: // // 设f(n)为连通图的数量,g(n)为非连通图的数量 ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- Windows Phone 8 解锁提示IpOverUsbSvc问题——IpOverUsbEnum返回No connected partners found解决方案
我的1520之前总是无法解锁,提示:IpOverUsbSvc服务没有开启什么的. 根据网上网友的各种解决方案: 1. 把手机时间设置为当前时间,并且关闭“自动设置” 2. 确保手机接入了互联网 3.确 ...
- POJ1737 Connected Graph
Connected Graph Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3156 Accepted: 1533 D ...
- [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 ...
随机推荐
- ZBrush软件如何编辑物体
新手在刚接触ZBrush®的时候,想要选中模型进行编辑,有时怎么都选不中,当再次画的时候只能在边上新建一个,还是不能进行编辑,遇到类似问题,你是如何解决的,本文将为您讲解ZBrush中怎么选中物体并进 ...
- Java中使用MD5加密的简单实现
import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorith ...
- CF #487 (Div. 2) D. A Shade of Moonlight 构造_数形结合
题意: 给 nnn个长度为 lll 且互不相交的开区间 (xi,xi+l)(x_{i}, x_{i}+l)(xi,xi+l) ,每个区间有一个移动速度 vvv,v∈1,−1v∈1,-1v∈1,−1 ...
- 路飞学城Python-Day21(practise)
编写程序, 如下有三点要求: 自定义用户信息数据结构, 写入文件, 然后读取出内容, 利用json模块进行数据的序列化和反序列化 e.g { "egon":{"passw ...
- 分清encodeURIComponent encodeURI 和 escape
encodeURIComponent encodeURI escape 目的:这三个函数的作用都是让一段字符串在所有电脑(所有国家区域语言)上可读. escape对字符串进行处理: encodeURI ...
- Shell(四)函数
函数 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. 一.格式 shell中函数的定义格式如下: [ function ] funname [()] { action; ...
- python_元组、字典
1.元组无法修改,只能索引2.只有两种方法 count 和 indexnames = ("Alex","jack")print(names.count(&quo ...
- Python:Fatal error in launcher: Unable to create process using 问题排查
cmd> django-admin 回车Fatal error in launcher: Unable to create process using '"c:\users\admin ...
- HDU 1796
呃,我竟然傻了,同时被a且b整除的个数为n/(a*b). 其实应该是n/[a,b]才对,是他们的最小公倍数啊... #include <iostream> #include <cst ...
- 各种List、Map、Set的比較
前言:Java中用不同的数据结构(哈希表/动态数组/平衡树/链表)实现了不同的集合接口(List/Map/Set).大多数情况下,集合类被用于不须要线程安全地环境,所以后来的集合实现都没有再採用同步以 ...