[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 ...
随机推荐
- 从DataTable高效率导出数据到Excel
首先从数据库读取数据到DataTable,这我就不提了,大家都明白.下面直接介绍如何从DataTable高效率导出数据到Excel中的方法,代码如下: using Microsoft.Office.I ...
- thinkphp 5 count()方法在控制器,模板中的使用方法
thinkphp中关于count()方法的使用: 控制器中:echo count($arr)模板中:{$arr | count}模板中if判断语句中 <if condition="co ...
- JAVA导出csv出现0.00E+00
导出csv出现 0.00E+00的问题,打印其值为0E-8:这是因为数据表中无对应数据(decimal),查询结果则为 0e-8. 出现的字段是多个字段相加产生的和,所以这里调用了一个相加的方法.在相 ...
- jq——属性和方法
ps:所有元素要加上“” 属性: 1) attr:属性,元素.attr(); 获取属性 $("input").click(function(){ console.log($( ...
- UGUI图集管理
using UnityEngine; using System.Collections; using System.Collections.Generic; //纹理图集加载管理 public cla ...
- Unity的Json解析<一>--读取Json文件
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50373558 作者:car ...
- java数组简单逻辑代码
package cuteSnow; public class HelloWorld { // 遍历数组里面的每个数字 public static void print(int[] array){ St ...
- Spring Security中的MD5盐值加密
在 spring Security 文档中有这么一句话: "盐值的原理非常简单,就是先把密码和盐值指定的内容合并在一起,再使用md5对合并后的内容进行演算,这样一来,就算密码是一个很常见的字 ...
- 【【henuacm2016级暑期训练】动态规划专题 E】Destroying Roads
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 首先. 这张图是无向无权图. 因此任意两点之间的最短路可以通过N^2的bfs轻易算出来. 即得到d[N+10][N+10] 考虑s[ ...
- tomcat的连接数与线程池
在使用tomcat时,经常会遇到连接数.线程数之类的配置问题,要真正理解这些概念,必须先了解Tomcat的连接器(Connector). 在前面的文章中写到过:Connector的主要功能,是接收连接 ...