CF 505C(Mr. Kitayuta, the Treasure Hunter-Dp考虑可用范围)

在一片由3000多个岛屿组成的群岛中,玩家从第0号岛屿开始,利用跳跃能力探索并收集宝石。通过动态规划算法解决寻找收集最大数量宝石的最优路径问题,最终证明跳岛距离范围不超过500。此挑战要求在限定时间内完成搜索,展示了算法优化的重要性。

C. Mr. Kitayuta, the Treasure Hunter
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The islands are evenly spaced along a line, numbered from 0 to 30000 from the west to the east. These islands are known to contain many treasures. There are n gems in the Shuseki Islands in total, and the i-th gem is located on island pi.

Mr. Kitayuta has just arrived at island 0. With his great jumping ability, he will repeatedly perform jumps between islands to the east according to the following process:

  • First, he will jump from island 0 to island d.
  • After that, he will continue jumping according to the following rule. Let l be the length of the previous jump, that is, if his previous jump was from island prev to island cur, let l = cur - prev. He will perform a jump of length l - 1l or l + 1 to the east. That is, he will jump to island (cur + l - 1)(cur + l) or (cur + l + 1) (if they exist). The length of a jump must be positive, that is, he cannot perform a jump of length 0 when l = 1. If there is no valid destination, he will stop jumping.

Mr. Kitayuta will collect the gems on the islands visited during the process. Find the maximum number of gems that he can collect.

Input

The first line of the input contains two space-separated integers n and d (1 ≤ n, d ≤ 30000), denoting the number of the gems in the Shuseki Islands and the length of the Mr. Kitayuta's first jump, respectively.

The next n lines describe the location of the gems. The i-th of them (1 ≤ i ≤ n) contains a integer pi (d ≤ p1 ≤ p2 ≤ ... ≤ pn ≤ 30000), denoting the number of the island that contains the i-th gem.

Output

Print the maximum number of gems that Mr. Kitayuta can collect.

Sample test(s)
input
4 10
10
21
27
27
output
3
input
8 8
9
19
28
36
45
55
66
78
output
6
input
13 7
8
8
9
16
17
17
18
21
23
24
24
26
30
output
4
Note

In the first sample, the optimal route is 0  →  10 (+1 gem)  →  19  →  27 (+2 gems)  → ...

In the second sample, the optimal route is 0  →  8  →  15  →  21 →  28 (+1 gem)  →  36 (+1 gem)  →  45 (+1 gem)  →  55 (+1 gem)  →  66 (+1 gem)  →  78 (+1 gem)  → ...

In the third sample, the optimal route is 0  →  7  →  13  →  18 (+1 gem)  →  24 (+2 gems)  →  30 (+1 gem)  → ...


这题直接dp很简单,但是d的范围很大

但是可以证明最终用到的d的取值最多不超过500

截取一段explanation:

Below is the explanation from yosupo, translated by me.

[From here]

Let m be the number of the islands (that is, 30001). First, let us describe a solution with time and memory complexity of O(m2).

We will apply Dynamic Programming. let dp[i][j] be the number of the gems that Mr. Kitayuta can collect after he jumps to island i, when the length of his previous jump is j (let us assume that he have not collect the gems on island i). Then, you can calculate the values of the table dp by the following:

  • dp[i][j] = 0, if i ≥ m
    (actually these islands do not exist, but we can suppose that they exist and when Mr. Kitayuta jumps to these islands, he stops jumping)
  • dp[i][j] =  (the number of the gems on island i + max(dp[i + j][j], dp[i + j + 1][j + 1]), if i < m, j = 1
    (he cannot perform a jump of length 0)
  • dp[i][j] =  (the number of the gems on island i + max(dp[i + j - 1][j - 1], dp[i + j][j], dp[i + j + 1][j + 1]), if i < m, j ≥ 2

This solution is unfeasible in terms of both time and memory. However, the following observation makes it an Accepted solution: there are only 491 values of j that we have to consider, which are d - 245, d - 244, d - 243, ..., d + 244 and d + 245.

Why? First, let us find the upper bound of j. Suppose Mr. Kitayuta always performs the "l + 1" jump (l: the length of the previous jump). Then, he will reach the end of the islands before he performs a jump of length d + 246, because
d + (d + 1) + (d + 2) + ... + (d + 245) ≥ 1 + 2 + ... + 245 = 245·(245 + 1) / 2 = 30135 > 30000. Thus, he will never be able to perform a jump of length d + 246 or longer.

Next, let us consider the lower bound of j in a similar way. If d ≤ 246, then obviously he will not be able to perform a jump of length d - 246 or shorter, because the length of a jump must be positive. Suppose Mr. Kitayuta always performs the "l - 1" jump, where d ≥ 247. Then, again he will reach the end of the islands before he performs a jump of length d - 246, because
d + (d - 1) + (d - 2) + ... + (d - 245) ≥ 245 + 244 + ... + 1 = 245·(245 + 1) / 2 = 30135 > 30000. Thus, he will never be able to perform a jump of length d - 246 or shorter.

Therefore, we have obtained a working solution: similar to the O(m2) one, but we will only consider the value of j between d - 245 andd + 245. The time and memory complexity of this solution will be O(m1.5), since the value "245" is slightly larger than .

This solution can be implemented by, for example, using a "normal" two dimensional array with a offset like this: dp[i][j - offset]. The time limit is set tight in order to fail most of naive solutions with search using std::map or something, so using hash maps (unordered_map) will be risky although the complexity will be the same as the described solution.

[End]


 
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<map>
#include<vector> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (30000+10)
#define MAXD (30000+10)
#define M (30001)
#define MP(a,b) make_pair(a,b) 
#define MAX_d_change (250+10)
#define C (250)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,d,a[MAXN]={0},s[MAXN]={0},f[MAXN][MAX_d_change*2]={0};
int main()
{
//	freopen("Treasure.in","r",stdin);
//	freopen(".out","w",stdout);
	cin>>n>>d;
	For(i,n)
	{
		int p;
		scanf("%d",&p);
		a[p]++;
	}
	For(i,M) s[i]=s[i-1]+a[i];
	
	int ans=0;
	memset(f,-1,sizeof(f));
	ans=f[d][C]=a[d];
	
	Fork(i,d,M)
	{
		Rep(j,2*C+1)
			if (f[i][j]>=0)
			{
				int dis=j-C+d;
				if (dis>0&&i+dis<=M) {f[i+dis][j]=max(f[i+dis][j],f[i][j]+a[i+dis]);ans=max(ans,f[i+dis][j]);}
				if (i+dis+1<=M) {f[i+dis+1][j+1]=max(f[i+dis+1][j+1],f[i][j]+a[i+dis+1]);ans=max(ans,f[i+dis+1][j+1]);}
				if (dis-1>0&&i+dis-1<=M) {f[i+dis-1][j-1]=max(f[i+dis-1][j-1],f[i][j]+a[i+dis-1]);ans=max(ans,f[i+dis-1][j-1]);}
			}
	}
	
	
	cout<<ans<<endl;
	
	
	
	return 0;
}







Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.9.34728.123 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pco_csharp", "pco\pco_csharp.csproj", "{C1DB2DF5-836D-4343-9AC0-EDD4968BA997}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tbt.LIVINGLSCI", "Tbt.LIVINGLSCI\Tbt.LIVINGLSCI.csproj", "{5E2D9E3B-F8C2-483A-8DB6-9CCBCFBB5932}" EndProject Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "LivingLSci", "LivingLSci\LivingLSci.vdproj", "{4D0FC170-4E48-4F17-95B0-A985253384EF}" EndProject Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "LivingLSciDemo", "LivingLSciDemo\LivingLSciDemo.vdproj", "{FAAC505C-8356-4CB8-9FE1-952ACA9A945C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Debug|x64 = Debug|x64 Release|Any CPU = Release|Any CPU Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {C1DB2DF5-836D-4343-9AC0-EDD4968BA997}.Debug|Any CPU.ActiveCfg = Debug|x64 {C1DB2DF5-836D-4343-9AC0-EDD4968BA997}.Debug|Any CPU.Build.0 = Debug|x64 {C1DB2DF5-836D-4343-9AC0-EDD4968BA997}.Debug|x64.ActiveCfg = Debug|x64 {C1DB2DF5-836D-4343-9AC0-EDD4968BA997}.Debug|x64.Build.0 = Debug|x64 {C1DB2DF5-836D-4343-9AC0-EDD4968BA997}.Release|Any CPU.ActiveCfg = Release|x64 {C1DB2DF5-836D-4343-9AC0-EDD4968BA997}.Release|Any CPU.Build.0 = Release|x64 {C1DB2DF5-836D-4343-9AC0-EDD4968BA997}.Release|x64.ActiveCfg = Release|x64 {C1DB2DF5-836D-4343-9AC0-EDD4968BA997}.Release|x64.Build.0 = Release|x64 {5E2D9E3B-F8C2-483A-8DB6-9CCBCFBB5932}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {5E2D9E3B-F8C2-483A-8DB6-9CCBCFBB5932}.Debug|Any CPU.Build.0 = Debug|Any CPU {5E2D9E3B-F8C2-483A-8DB6-9CCBCFBB5932}.Debug|x64.ActiveCfg = Debug|Any CPU {5E2D9E3B-F8C2-483A-8DB6-9CCBCFBB5932}.Debug|x64.Build.0 = Debug|Any CPU {5E2D9E3B-F8C2-483A-8DB6-9C
07-31
在 Visual Studio 中,解决方案文件(`.sln`)和项目文件(`.vcxproj` 或 `.csproj`)的配置对构建过程至关重要。如果配置不当,可能会导致项目无法正确加载或构建。以下是一些常见问题及其排查方法: ### 1. 检查项目文件路径和依赖项 确保所有项目文件路径正确,并且没有丢失或损坏的依赖项。如果项目引用了外部库或 SDK,必须确保这些库已正确安装并且路径配置无误。某些情况下,如果未为所有 CPU 架构安装库,则某些 Visual C++ 项目可能无法构建,导致错误信息显示为所有配置均失败 [^1]。 ### 2. 配置平台设置 在 Visual Studio 中,解决方案平台(如 x86、x64、ARM)必须与项目目标平台匹配。可以通过以下方式检查和更改平台设置: - 打开“配置管理器”(右键点击解决方案 -> 配置管理器)。 - 确保每个项目的“平台”列与解决方案平台一致。 - 如果缺少所需平台,可以选择“<新建...>”来添加新的平台配置。 ### 3. 构建配置(Debug/Release) 确保当前构建配置(Debug 或 Release)与项目设置一致。可以在“配置管理器”中更改活动配置,或在“项目属性”中检查编译器和链接器设置。某些情况下,如 iOS 或 Android 项目,应将链接器设置为“不链接”以避免构建失败 [^2]。 ### 4. 检查项目文件格式 对于较旧的 Visual Studio 项目文件格式(如 `.vcxproj` 文件),如果未正确升级或包含无效的 XML 节点,可能导致项目无法加载。可以尝试以下方法: - 右键点击项目 -> “卸载项目”。 - 再次右键点击 -> “重新加载项目”。 - 如果仍然无法加载,可以尝试使用文本编辑器打开 `.vcxproj` 文件,检查是否有语法错误或不兼容的配置。 ### 5. 清理并重新生成解决方案 有时临时构建文件可能损坏,导致构建失败。可以尝试: - 在“生成”菜单中选择“清理解决方案”。 - 然后选择“重新生成解决方案”。 ### 6. 更新 Visual Studio 和 SDK 确保 Visual Studio 已更新到最新版本,并且所有必要的 SDK 已安装。某些构建错误可能源于缺少最新更新或未安装特定平台的库。 ### 示例:查看和修改构建配置 ```xml <!-- 示例 .vcxproj 文件中的构建配置片段 --> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries> <PlatformToolset>v143</PlatformToolset> <CharacterSet>Unicode</CharacterSet> </PropertyGroup> ``` ### 7. 使用 MSBuild 命令行工具排查 可以使用 `msbuild` 命令行工具以详细模式构建解决方案,以获取更详细的错误信息: ```bash msbuild MySolution.sln /t:Build /p:Configuration=Debug /v:detailed ``` 这将输出构建过程中的详细日志,有助于识别问题根源。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值