[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 ...
随机推荐
- 手写raft(二) 实现日志复制
1. Raft日志复制介绍 在上一篇博客中MyRaft实现了leader选举,为接下来实现日志复制功能打下了基础: 手写raft(一) 实现leader选举 日志复制是raft最核心也是最复杂的功能, ...
- [远程Call]32位远程多参数带返回调用
[远程Call]32位远程多参数带返回调用 引子 在Windows上可以使用CreateRemoteThread实现远程Call,但是有不带返回值且只能传递一个参数的限制. 解决思路 将多个参数利用V ...
- 原来你是这样的JAVA[04]-数组Arrays
一.打印数组 Arrays类提供了打印数组元素的方法,Arrays.toString()和Arrays.deepToString(). //打印数组 System.out.println(Arrays ...
- Dubbo3应用开发—Dubbo序列化方案(Kryo、FST、FASTJSON2、ProtoBuf序列化方案的介绍和使用)
Dubbo序列化方案(Kryo.FST.FASTJSON2.ProtoBuf序列化方案的介绍和使用) 序列化简介 序列化是Dubbo在RPC中非常重要的一个组成部分,其核心作用就是把网络传输中的数据, ...
- 算法打卡|Day4 链表part02
Day4 链表part02 今日任务 ● 24. 两两交换链表中的节点 ● 19.删除链表的倒数第N个节点 ● 面试题 02.07. 链表相交 ● 142.环形链表II 目录 Day4 链表part0 ...
- golang Context应用举例
Context本质 golang标准库里Context实际上是一个接口(即一种编程规范. 一种约定). type Context interface { Deadline() (deadline ti ...
- DB2---创建返回结果集的函数
在数据验证中,经常遇到需返回结果集的操作,故整理一个返回结果集的DB2函数,便于后期查阅 1.准备测试表 /*创建测试表:设置结果集的属性为表字段*/ CREATE TABLE Test_EXWAST ...
- Llama2-Chinese项目:4-量化模型
一.量化模型调用方式 下面是一个调用FlagAlpha/Llama2-Chinese-13b-Chat[1]的4bit压缩版本FlagAlpha/Llama2-Chinese-13b-Chat-4 ...
- c语言代码练习12
//计算1/1-1/2+1/3...-1/100的和#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { in ...
- MySQL快速导入千万条数据(3)
目录 一.测试环境 二.命令行导入方式 三.LOAD DATA导入方式 四.结论 接上文,本次在较高性能的X86物理机上,做真实生产环境的大数据量导入测试. 一.测试环境 ■ CPU是24核,每核2线 ...