首页 > 其他分享 >[ARC165D] Substring Comparison

[ARC165D] Substring Comparison

时间:2023-09-22 20:14:21浏览次数:29  
标签:Comparison int find tp st Substring leq low ARC165D

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.

  1. $|S| \lt |T|$ and $(S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|})$.
  2. 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");
}

标签:Comparison,int,find,tp,st,Substring,leq,low,ARC165D
From: https://www.cnblogs.com/mekoszc/p/17723239.html

相关文章

  • 一次性搞懂JS字符串截取方法substring()、slice()以及substr()的用法和区别
    substring()和slice()都接受两个参数,“start”和“end”。“start”表示截取的开始位置,“end”表示结束的位置(不包括该位置的字符,也就是前要后不要)。如果不传参数,则返回字符串本身的一个副本。 如果只传一个参数,则从该位置开始,截取到字符串的末尾。 如果传递两个参数,则......
  • CF914F Substrings in a String
    知识点:bitset,SAM,根号分治Link:https://codeforces.com/problemset/problem/914/F一种在字符集较小情况下的多轮字符串匹配暴力的优化。好久没写过单题的题解了格式都忘了、、、简述给定一仅包含小写字母的字符串\(s\),给定\(q\)次操作,每次操作都是下列两种形式之一:将字符......
  • Long-read error correction: a survey and qualitative comparison (长读错误纠正:一
    BasicInformation:Title:Long-readerrorcorrection:asurveyandqualitativecomparison(长读错误纠正:一项调查和定性比较)Authors:PierreMorisse,ThierryLecroq,ArnaudLefebvreAffiliation:NormandieUniversité,UNIROUEN,INSARouen,LITIS,76000Rouen,......
  • Flink and Kafka Streams: a Comparison and Guideline for Users
    ThisblogpostiswrittenjointlybyStephanEwen,CTOofdataArtisans,andNehaNarkhede,CTOofConfluent. StephanEwenisPMCmemberofApacheFlinkandco-founderandCTOofdataArtisans.BeforefoundingdataArtisans,Stephanwasleadingthedevelo......
  • MySQL字符串截取之substring_index
    substring_index(str,delim,count)str:要处理的字符串delim:分隔符count:计数 例子:str=www.wikibt.comsubstring_index(str,'.',1)结果是:wwwsubstring_index(str,'.',2)结果是:www.wikibt也就是说,如果count是正数,那么就是从左往右数,第N个分隔符的左边的全部内容相......
  • VB.NET and C# Comparison
    VB.NETandC#Comparison(vb.net与c#对比)//z2012-3-713:30:51PMIS2120@ImportsSystemNamespaceHello  ClassHelloWorld     OverloadsSharedSubMain(ByValargs()AsString)        DimnameAsString="VB.NET"        'See......
  • 数据库PostgreSQL PG 字符串拼接,大小写转换,substring
    前言PostgreSQL数据库简称pg数据库。本文主要介绍使用pg数据库时,字符串的一些常用操作。例如:多个字符串如何连接在一起,字符串如何大小写转换,删除字符串两边的空格,查找字符位置,查找子字符串等。一、多个字符串如何连接,拼接?pg的字符串连接使用||,注意不是+1.将2个字符串hello......
  • SPOJ Substrings 题解
    \(\text{SAM}\)入门好题。首先我们需要知道几个关于\(\text{SAM}\)的结论。结论1:题目中的\(f(x)\)单调下降。显然,对于长度为\(x\)的子串,其必存在一个\(x-1\)的后缀,这个后缀的\(\text{endpos}\)集合肯定包含子串的\(\text{endpos}\)集合,所以必有\(f(x-1)\le......
  • Compile result comparison
    Thesourcecodeasbelow#include<stdint.h>#include<stdio.h>typedefstruct{uint8_tdata1;uint8_tdata2;uint8_tdata3;uint8_tdata4;uint8_tdata5;}config_t;config_tgConfig[2]={0};uint8_tgIndex=0;confi......
  • JS中字符串28种常用API总结,substring、slice、JSON.stringify、match、split、search
    一、引言在前端开发中,处理字符串是一项常见的任务。JavaScript提供了一系列的字符串API,用于操作和处理字符串数据。字符串常用的API方法有很多,包括查找字符串、截取字符串、替换字符串、分割字符串、大小写转换、字符串拼接和字符串比较等等。本文将介绍一些常用的字符串API......