[ARC165D] Substring Comparison
Problem Statement
For an integer sequence $X=(X_1,X_2,\dots,X_n)$, let $X[L,R]$ denote the integer sequence $(X_L,X_{L+1},\dots,X_{R})$.
You are given integers $N$ and $M$, and $M$ quadruples of integers $(A_i,B_i,C_i,D_i)$.
Determine if there is an integer sequence $X$ of length $N$ that satisfies the following condition for every $i=1,2,\dots,M$:
- $X[A_i,B_i]$ is lexicographically smaller than $X[C_i,D_i]$.
What is lexicographical order on sequences?
A sequence $S = (S_1,S_2,\ldots,S_{|S|})$ is lexicographically smaller than $T = (T_1,T_2,\ldots,T_{|T|})$ when 1. or 2. below holds.
Here, $|S|$ and $|T|$ denotes the lengths of $S$ and $T$, respectively.
- $|S| \lt |T|$ and $(S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|})$.
- There is an integer $1 \leq i \leq \min\lbrace |S|, |T| \rbrace$ that satisfy both of the following:
- $(S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})$.
- $S_i$ is smaller than $T_i$ (as a number).
Constraints
- $2 \leq N \leq 2000$
- $1 \leq M \leq 2000$
- $1 \leq A_i \leq B_i \leq N$
- $1 \leq C_i \leq D_i \leq N$
- All input values are integers.
首先一定满足 \(X_{A_i}\le X_{C_i}\),从 \(A_i\) 向 \(C_i\) 连边。
跑一次 tarjan 后,此时如果出现了大于 1 的强连通分量,那么这个分量里所有的数都是一样的,用一个并查集并起来。
如果这个强连通分量里的边有 \(i\),那么说明 \(X_{A_{i+1}}\le X_{C_{i+1}}\)
以此类推,知道某一次没有大于 1 的强连通分量,就结束了。
发现每次至少合并两个点,最多跑 \(N\) 次 tarjan。同时每次图中至多有 \(M\) 条边,所以复杂度是 \(O(NM)\) 的。
#include<bits/stdc++.h>
using namespace std;
const int N=2005;
int n,m,k,tp,st[N],hd[N],fl,e_num,tme,p[N],id[N],dfn[N],low[N],idx,a[N],b[N],c[N],d[N],fa[N];
struct edge{
int v,nxt;
}e[N];
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
}
int find(int x)
{
if(fa[x]==x)
return x;
return fa[x]=find(fa[x]);
}
void tarjan(int x)
{
dfn[x]=low[x]=++tme;
st[++tp]=x;
for(int i=hd[x];i;i=e[i].nxt)
{
if(!dfn[e[i].v])
tarjan(e[i].v),low[x]=min(low[x],low[e[i].v]);
else if(!id[e[i].v])
low[x]=min(low[x],dfn[e[i].v]);
}
if(low[x]==dfn[x])
{
++idx;
if(st[tp]^x)
{
fl=1;
while(st[tp]^x)
id[st[tp]]=idx,fa[find(st[tp--])]=find(x);
}
id[st[tp--]]=idx;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d%d",a+i,b+i,c+i,d+i);
add_edge(a[i],c[i]);
}
while(1)
{
fl=tme=idx=0;
memset(dfn,0,sizeof(dfn));
memset(id,0,sizeof(id));
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
if(!fl)
break;
memset(hd,e_num=0,sizeof(hd));
for(int i=1;i<=m;i++)
{
while(a[i]+p[i]<=b[i]&&c[i]+p[i]<=d[i]&&find(a[i]+p[i])==find(c[i]+p[i]))
++p[i];
if(a[i]+p[i]>b[i])
{
if(d[i]-c[i]<=b[i]-a[i])
return puts("No"),0;
}
else if(c[i]+p[i]>d[i])
return puts("No"),0;
else
add_edge(find(a[i]+p[i]),find(c[i]+p[i]));
}
}
puts("Yes");
}
[ARC165D] Substring Comparison的更多相关文章
- Finding the Longest Palindromic Substring in Linear Time
Finding the Longest Palindromic Substring in Linear Time Finding the Longest Palindromic Substring i ...
- Java and C# Comparison
原文:http://www.harding.edu/fmccown/java_csharp_comparison.html Java Program Structure C# package hell ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- substring的用法
public String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串.该子字符串从指定的 beginIndex 处开 ...
- jQuery之常用且重要方法梳理(target,arguments,slice,substring,data,trigger,Attr)-(一)
1.jquery data(name) data() 方法向被选元素附加数据,或者从被选元素获取数据. $("#btn1").click(function(){ $(" ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- C#和Java中的Substring()
吐槽-使用清理软件整理电脑要注意,不要清理的"太狠",不然你会受伤的! C#中的Substring() 示例 实现代码 using System;using System.Coll ...
随机推荐
- VB快速上手文档教程
前言 本来我想可能不会接触到这个语言, 不过在用excel时需要用到VBA. 这就不得不专门去学习一番. 入了个门, 专门写个文档留着. 万一以后用得到呢- 论VB, 我还是初学者. 如有弄错了的地方 ...
- Github的一个奇技淫巧
背景 前段时间给 VictoriaLogs 提交了一个 PR: https://github.com/VictoriaMetrics/VictoriaMetrics/pull/4934 本来一切都很顺 ...
- 探析ElasticSearch Kibana在测试工作中的实践应用
一. 为什么使用ES Kibana 离线数据测试中最重要的就是数据验证,一部分需要测试es存储数据的正确性,另一部分就需要验证接口从es取值逻辑的正确性.而为了验证es取值逻辑的正确性,就需要用到Ki ...
- stata中回归分析常用方法
// 按键盘上的PageUp可以使用上一次输入的代码(Matleb中是上箭头)// 清除所有变量clear// 清屏 和 matlab的clc类似cls // 导入数据(其实是我们直接在界面上粘贴过来 ...
- 其它——python操作kafka实践
文章目录 1.先看最简单的场景,生产者生产消息,消费者接收消息,下面是生产者的简单代码. ------------------------------------------------------- ...
- Go通道机制与应用详解
本文深入探讨了Go语言中通道(Channel)的各个方面,从基础概念到高级应用.文章详细解析了通道的类型.操作方法以及垃圾回收机制,更进一步通过具体代码示例展示了通道在数据流处理.任务调度和状态监控等 ...
- 小白CNN入门指导
小白CNN入门指导 这几天一直在小白入门学习卷积层以准备组会,以下是我自学理解内容,若有错误的地方请各位评论指出 数学部分 一 卷积层 \[输入 32*32*3 (input neurons) \] ...
- Opencv系列之一:简介与基本使用
1 Opencv简介 Opencv是计算机视觉中经典的专用库,其支持多语言,跨平台,功能强大.Opencv-Python为Opencv提供了Python接口,使得使用者在Python中能够调用C/C+ ...
- js数据结构--队列
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- 拒绝恶意IP登录服务器
拒绝恶意IP登录服务器,并加入防火墙黑名单 #!/bin/bash #2020-03-20 16:39 #auto refuse ip dlu #By Precious ############### ...