B1. Koa and the Beach (Easy Version)[简单dp]

简单版本用DP可以轻松解决

d p [ i ] [ j ] j i 定义dp[i][j]表示第j秒是否可以在i位置

j i l , 那么如果第j秒i位置深度大于l,不可行

, d p [ i 1 ] [ j 1 ] 否则,可以由dp[i-1][j-1]上一个位置转移而来

d p [ i ] [ j 1 ] 1 s 也可以由dp[i][j-1]本位置停留1s转移而来

至于这个秒数上限是多少我还真不知道

不过最大应该就是 2 k n 2kn ,因为最多在每个位置停一个周期

我这里选的1000s,也过了

#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int t,n,k,l,d[maxn],dp[109][1009],tide[maxn];
inline void read(int &x)
{
	scanf("%d",&x);
}
int main()
{
	cin >> t;
	while( t-- )
	{
		int flag=0;
		cin >> n >> k >> l;
		for(int i=1;i<=n;i++)	read( d[i] );
		for(int i=0;i<=k;i++)	tide[i]=i;
		for(int i=k+1;i<=2*k-1;i++)	tide[i]=tide[i-1]-1;
		//dp[i][j]表示第i秒停在第j个岛屿是否可行
		for(int i=0;i<=1000;i++)	dp[0][i]=1;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=1000;j++)
		{
			if( d[i]+tide[ j%(2*k) ]>l )	continue;
			dp[i][j]|=dp[i-1][j-1];
			dp[i][j]|=dp[i][j-1];
			if( i==n&&dp[i][j] )	flag=1;
		}
		for(int i=1;i<=n;i++) 
		for(int j=0;j<=1000;j++)
			dp[i][j]=0;
		if( flag )	cout << "YES\n";
		else	cout << "NO\n";
	}
}

猜你喜欢

转载自blog.csdn.net/jziwjxjd/article/details/107949376
今日推荐