跳到正文

个人记录

[CSP/J 2019多校模拟赛4]可爱路径

crashed约 12 分钟
原文
文章信息与版本

文章信息

发布
2019年9月30日
收录
2025年12月12日
更新
2026年10月2日
篇幅
约 12 分钟

版本

  1. v2当前
  2. v1

题目

        校内OJ。

分析

        好题。估计超过了正常普及的T4T_4难度。         显然可以发现,由于路径的特殊性,走KK条边就相当于走K+1K+1个点。所以下文中讨论KK实际上是指K+1K+1个点。         从最暴力的算法来思考这道题。

35分

        题目中我们不难发现这样一个单调性:如果存在一条路径,它的可爱度≥k\ge k,那么必然存在一条路径,它的可爱度≥k−1\ge k-1。         显而易见的废话。         顺便说一句,如果看到了这样的单调性的话,建议冲上去就开始打二分先思考二分。这道题中二分就可以解决问题;而如果此题根据答案必然在点权之中这个性质来搞的话,就会复杂很多。(反正我是不会)         假设当前得到的答案为kk,下面考虑如何检查答案。         不难想到枚举起点,再用DFSDFS枚举路径。我们在DFSDFS中记录路径的长度ll和路径中≥k\ge k的点的个数mm。         考虑如何判断一条路径是否合法。根据二分合法的要求(存在可爱度≥k\ge k的路径),也就是要求l−m≤⌊l2⌋l-m\le \lfloor\frac l 2\rfloor。

        这样暴力写就有3535分。

70分

        观察我们暴力程序的运行情况,我们发现,它的主要时间花在找路径上面。         而这个题中还有一个性质——这个图是一个DAGDAG。         DAGDAG上面可以干什么呀?         DPDP呀!         所以我们就很自然地想到用DPDP简化找路径的过程。         考虑我们的条件判别式:         l−m≤⌊l2⌋\displaystyle l-m\le\lfloor\frac l 2\rfloor         发现:在ll一定的时候,mm越大,就越有可能找出需要的路径。         很自然地想到如下的DPDP:         DP(u,i)DP(u,i):从uu点出发,长度为ii的路径中最大的mm。         转移比较简单,大概长这个样子:         DP(u,i)={[w[u]≥k]i=1max⁡(u,v)∈E{DP(v,i−1)+[w[u]≥k]}i≠1\displaystyle DP(u,i)=\begin{cases}[w[u]\ge k] & i=1\\\max_{(u,v)\in E}\left\{DP(v,i-1)+[w[u]\ge k] \right\} & i\not = 1\end{cases}         DPDP完之后就可以枚举起点和长度找路径了。         加上去之后…还是只有7070分。(单链的数据过不去呀TAT)         (要DPDP就要先跑拓扑序。你要写记搜倒也可以)

100分

        O(n2)O(n^2)的单次检查速度对于单链数据来说还是不够优秀,所以我们得搞一些特殊的方法来处理它。         单链上搞的话,我们就可以视作是在一个序列上搞。在序列上搞的话,我们就直接枚举区间。         考虑枚举区间右端点。对于一个右端点ii,它的可行的左端点(暂时只考虑区间长度)就是[0,i−K][0,i-K],若ii持续右移,这个区间显然是递增的。         设s(i)=∑j=1i[w[j]≥k]s(i)=\sum_{j=1}^i[w[j]\ge k]。         当右端点为ii,j∈[0,i−K]j\in[0,i-K]时,还是对条件的式子变形:         l−m≤⌊l2⌋\displaystyle l-m\le \lfloor\frac l 2 \rfloor   ⇒i−j−s(i)+s(j)≤⌊i−j2⌋\displaystyle \Rightarrow i-j-s(i)+s(j)\le\lfloor\frac{i-j}2\rfloor   ⇒2i−2j−2s(i)+2s(j)≤i−j\displaystyle \Rightarrow 2i-2j-2s(i)+2s(j)\le i - j   ⇒i−2s(i)≤j−2s(j)\displaystyle \Rightarrow i - 2s(i)\le j - 2s(j)         显然j−2s(j)j-2s(j)越大,越有可能发现合法的路径,所以我们用一个变量tt存储max⁡0≤j≤i−K{j−2s(j)}\max_{0\le j \le i-K}\{j-2s(j)\}。当ii右移的时候,tt可以递推地求出来。我们每次只需要判断i−2s(i)≤ti-2s(i)\le t即可。         然后这道题就做完了。         (流下了菜鸡的泪水)

代码

C++
#include <queue>
#include <cstdio>
using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 + 5, MAXM = 3e3 + 5, MMAX = 1e5 + 5;

template<typename _T>
void read( _T &x )
{
	x = 0;char s = getchar();int f = 1;
	while( s < '0' || '9' < s ){ f = 1; if( s == '-' ) f = -1; s = getchar(); }
	while( '0' <= s && s <= '9' ) { x = ( x << 3 ) + ( x << 1 ) + s - '0', s = getchar(); }
	x *= f;
}

template<typename _T>
void write( _T x )
{
	if( x < 0 ){ putchar( '-' ), x = -x; }
	if( 9 < x ) write( x / 10 );
	putchar( x % 10 + '0' );
}

template<typename _T>
_T MIN( const _T a, const _T b )
{
	return a < b ? a : b;
}

template<typename _T>
_T MAX( const _T a, const _T b )
{
	return a > b ? a : b;
}

struct edge
{
	int to, nxt;
}Graph[MMAX];

queue<int> q;

int DP[MAXN][MAXN];
int seq[MMAX], s[MMAX];
int head[MMAX];
int w[MMAX], into[MMAX];
int N, M, K, cnt, gre, len;
bool visited[MMAX];
bool state = true;

void addEdge( const int from, const int to )
{
	cnt ++;
	into[to] ++;
	Graph[cnt].to = to;
	Graph[cnt].nxt = head[from];
	head[from] = cnt;
}

bool chk( const int val )
{
	if( state )
	{
		for( int i = 1 ; i <= N ; i ++ )
			s[i] = w[seq[i]] >= val;
		for( int i = 1 ; i <= N ; i ++ )
			s[i] += s[i - 1];
		int tmp = -INF, j;
		for( int i = K ; i <= N ; i ++ )
		{
			j = i - K;
			tmp = MAX( j - 2 *s[j], tmp );
			if( i - 2 * s[i] <= tmp ) return true;
		}
		return false;
	}
	for( int i = 1 ; i <= N ; i ++ )
		for( int j = 1 ; j <= N ; j ++ )
			DP[i][j] = 0;
	int u, v;
	for( int i = N ; i >= 1 ; i -- )
	{
		u = seq[i];
		DP[u][1] = w[u] >= val;
		for( int j = head[u] ; j ; j = Graph[j].nxt )
		{
			v = Graph[j].to;
			for( int k = 1 ; k <= N ; k ++ )
				DP[u][k] = MAX( DP[u][k], DP[v][k - 1] + ( w[u] >= val ) );
		}
	}
	for( int i = 1 ; i <= N ; i ++ )
		for( int j = K ; j <= N ; j ++ )
			if( j - DP[i][j] <= j / 2 )
				return true;
	return false;
}

void topological()
{
	while( ! q.empty() ) q.pop();
	for( int i = 1 ; i <= N ; i ++ )
		if( ! into[i] )
			q.push( i ), visited[i] = true;
	int u, v, tot = 0;
	while( ! q.empty() )
	{
		u = q.front();
		seq[++tot] = u;
		q.pop();
		for( int i = head[u] ; i ; i = Graph[i].nxt )
		{
			v = Graph[i].to;
			into[v] --;
			if( ! visited[v] && ! into[v] )
			{
				visited[v] = true;
				q.push( v );
			}
		}
	}
}

int main()
{
	int fr, to, l = INF, r = -INF;
	read( N ), read( M ), read( K );
	K ++;
	for( int i = 1 ; i <= N ; i ++ ) read( w[i] ), r = MAX( r, w[i] ), l = MIN( l, w[i] );
	for( int i = 1 ; i <= M ; i ++ )
	{
		read( fr ), read( to ), addEdge( fr, to );
		if( fr - 1 != to ) state = false;
	}
	topological();
	int mid;
	while( r - l > 1 )
	{
		mid = ( l + r ) >> 1;
		if( chk( mid ) ) l = mid;
		else r = mid - 1;
	}
	if( chk( r ) ) write( r ), putchar( '\n' );
	else if( chk( l ) ) write( l ), putchar( '\n' );
	else puts( "No" );
	return 0;
}
原文