AT2644 [ARC076C] Connected?
可以发现这个问题是存在边界的,那么我们可以先放宽一下条件思考一下没有边界的情况。
通过手玩可以发现,若不存在边界总是可以完成这个任务的。
因为两条曲线之间不存在交点,那么每次我们可以从空隙穿过一条直线然后一直绕道另一端。
那么存在边界的情况特殊在一条两个点都在边界上的曲线会将整个矩形划分成两个部分,而此时若两个部分还存在点没有连接就不合法了。
但是,如果这条曲线不全是两个端点都在边界上,那么我们先将其连接对两个端点都在边界上的曲线是不会影响的,一样可以通过缝隙绕道。
那么此时只需要考虑两个端点都在边界上的曲线了。
不难发现,此时合法当且仅当不存在一条曲线将矩形分成两个区域后存在两个端点在不同区域,即不存在两条有端点连成的线段会相交。
因为每条线段都只存在于边界上,可以考虑将二维的矩形变成一维的由边界展开变成的一条线段。
那么此时原图上的线段就会变成新线段上的若干个区间。
可以发现合法的条件当且仅当这些区间只有包含和相离关系。
于是只需要从左往右扫一遍开个栈做一遍括号匹配即可。
#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
const int N = 2e5 + 5;
struct node { int x, y, id;} a[N];
int n, m, h, w, x, y, xx, yy, top, st[N], book[N];
bool check(int x, int y) { return (!x || !y || (x == h) || (y == w));}
bool cmp(node a, node b) {
if(a.y == w) return (b.y < w || b.x > a.x);
if(a.x == h) return (b.y != w && (b.x < h || b.y < a.y));
if(!a.y) return (b.y != w && b.x != h && (!b.x || b.x < a.x));
if(!a.x) return (b.y != w && b.x != h && b.y && b.y > a.y);
}
int main () {
cin >> h >> w >> m;
rep(i, 1, m) {
cin >> x >> y >> xx >> yy;
if(!check(x, y) || !check(xx, yy)) continue ;
a[++n] = (node){x, y, i}, a[++n] = (node){xx, yy, i};
}
sort(a + 1, a + n + 1, cmp);
rep(i, 1, n) {
if(book[a[i].id]) {
if(a[st[top]].id != a[i].id) { puts("NO"); return 0;}
else --top;
}
else st[++top] = i, book[a[i].id] = 1;
}
puts("YES");
return 0;
}
可以发现,先放宽条件是一个当条件非常多的时候对一道题下手的一个良好方法。
其次,当某些平面 / 三维空间上只涉及边界时,可以考虑降维将边界拆成一条连续的线段然后在序列上完成这个问题。
AT2644 [ARC076C] Connected?的更多相关文章
- [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 ...
- Supporting Connected Routes to Subnet Zero
Supporting Connected Routes to Subnet Zero IOS allows the network engineer to tell a router to eithe ...
- lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素
题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...
随机推荐
- uniapp动态修改导航栏
1.修改导航栏buttons 如图动态修改角标 <template> <view> </view> </template> <script> ...
- vue中使用JSX报错,如何解决
Support for the experimental syntax 'jsx' isn't currently enabled (32:12): 30 | }, 31 | render() { & ...
- vue3获取当前路由
正解 使用useRouter: // router的 path: "/user/:uid" <template> <div>user</div> ...
- 简单的 Shell 脚本入门教程
Shell脚本 运作方式与解释型语言相当,如果有语言基础,学起 Shell 脚本就非常容易,但是 Shell 与常见的语言不同,一些常见的函数在 Shell 中需要组合一些命令得以实现 工具推荐 Sh ...
- JAVA获取上下行网速
JAVA获取上下行网速 package com.iecas.zwterminalstate.util;import java.io.BufferedReader;import java.io.IOEx ...
- Ubuntu18.04安装/卸载NVIDIA显卡驱动
1 显卡驱动下载 官网:NVIDIA 搜索适合本机的驱动 获取最新版本驱动 立即下载 文件 以上,显卡驱动下载完成. 2 显卡驱动安装 2.1 添加可执行权限 进入驱动文件目录sudo chmod a ...
- Redis OM .NET Redis对象映射框架
Redis OM Redis OM 是 Redis 官方推出的对象映射框架,即:Object Mapping.让开发人员更简单.方便的操作 Redis 数据.Redis 存储的数据抽象为对象映射,支持 ...
- PyCharm撤消/恢复
PyCharm在撤消/重做的每个步骤之前移动插入符号,然后执行撤消/重做操作. 要撤消操作,请执行以下操作之一: 在主菜单上,选择Edit | Undo. 按Ctrl+Z. 要恢复操作,请执行以下操作 ...
- 日志收集系统系列(五)之LogTransfer
从kafka里面把日志取出来,写入ES,使用Kibana做可视化展示 1. ElasticSearch 1.1 介绍 Elasticsearch(ES)是一个基于Lucene构建的开源.分布式.RES ...
- python+selenium 定位元素的主要方法
selenium对web各元素的操作首先就要先定位元素,定位元素的方法主要有以下几种: 通过id定位元素:find_element_by_id("id_vaule") 通过name ...