Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP
题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>30000)之前最多可以收集到多少财富。
解法:容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富。
dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1])
dp[i+step] = max(dp[i+step],dp[i][j]+cnt[i+step])
dp[i+step+1] = max(dp[i+step+1],dp[i][j]+cnt[i+step+1])
但是步长直接开30000存的话肯定是不行的,又发现,其实走过30000之前,步长的变化不会很大,如果步长每次增加1的话,那么最少1+2+...+n=n(n+1)/2 > 30000, n<250,即步长变化不会超过250.所以第二维保存相对原始步长的改变量,-250~250,开500就够了,这样就不会MLE了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 10007 int dp[*N][];
int cnt[*N],ans; int main()
{
int i,j,n,d;
while(scanf("%d%d",&n,&d)!=EOF)
{
memset(cnt,,sizeof(cnt));
for(i=;i<=n;i++)
{
scanf("%d",&j);
cnt[j]++;
}
memset(dp,-,sizeof(dp));
dp[d][] = cnt[d];
ans = dp[d][];
for(i=d;i<=;i++) {
for(j=;j<=;j++) {
if(dp[i][j] == -) continue;
int to = i+d+j-;
if(to <= ) {
dp[to][j] = max(dp[to][j],dp[i][j]+cnt[to]);
ans = max(ans,dp[to][j]);
}
if(to+ <= ) {
dp[to+][j+] = max(dp[to+][j+],dp[i][j]+cnt[to+]);
ans = max(ans,dp[to+][j+]);
}
if(to <= && to-i > ) {
dp[to-][j-] = max(dp[to-][j-],dp[i][j]+cnt[to-]);
ans = max(ans,dp[to-][j-]);
}
}
}
cout<<ans<<endl;
}
return ;
}
Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP的更多相关文章
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...
- Codeforces Round #286 (Div. 1) B. Mr. Kitayuta's Technology (强连通分量)
题目地址:http://codeforces.com/contest/506/problem/B 先用强连通判环.然后转化成无向图,找无向图连通块.若一个有n个点的块内有强连通环,那么须要n条边.即正 ...
- Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)
数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...
- Codeforces Round #286 (Div. 2)A. Mr. Kitayuta's Gift(暴力,string的应用)
由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...
- codeforces 505C C. Mr. Kitayuta, the Treasure Hunter(dp)
题目链接: C. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 me ...
随机推荐
- 分享50款 Android 移动应用程序图标【下篇】
在这个移动程序流行的时代,持续增长的应用程序经济充满了商业机遇.任何对应用程序设计感兴趣的人,将会喜欢上这里的50个独特的 Android 应用程序图标.这些例子中的图标能够让应用程序的设计更具吸引力 ...
- [deviceone开发]-cnodejs论坛移动端App
一. 简介 这个App是利用cnodejs.net的API来实现论坛的移动端,使用了deviceone的官方的js库(github.com/do-js). 从而使代码非常简洁,便于阅读和参考,值得推荐 ...
- JavaScript 使用反斜杠对代码行进行折行
JavaScript对代码行进行折行-使用反斜杠 您可以在文本字符串中使用反斜杠对代码行进行换行.下面的例子会正确地显示: <html> <head> <script t ...
- Atitit.阿里云c盘 系统盘爆满解决方案
Atitit.阿里云c盘 系统盘爆满解决方案 Use disk parse tool to scan then C:\widnow/soursce /install.wim 迁移 3g 显示在 ...
- myeclipse 2015 CI 16发布【附下载】
2015升级版再次来袭! 更新日志: Slack Integration 新版本集成了Slack,你只需要注册一个Slack帐号然后就可以发送和接收代码片段.你甚至不需要登录Slack就可以直接在Ec ...
- Atitit.url 汉字中文路径 404 resin4 resin 解决 v2 q329
Atitit.url 汉字中文路径 404 resin4 resin 解决 v2 q329 1. Pluginx机制1 2. Code1 3. 参考4 1. 原理 过滤器 ,,拦截jpg w ...
- Android 开源框架Universal-Image-Loader完全解析(三)---源代码解读
转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/39057201),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...
- Ida双开定位android so文件
Ida双开定位的意思是先用ida静态分析so文件,然后再开一个ida动态调试so文件.因为在动态调试中ida并不会对整个动态加载的so文件进行详细的分析,所以很多函数并无法识别出来.比如静态分析中有很 ...
- 改变Android ProgressBar样式颜色
地址: http://blog.csdn.net/lvxiangan/article/details/9110121
- tomcat WEB-INF中的结构
tomcat中 WEB-INF中结构包含3个东西:web.xml,classes文件夹,lib文件夹 web.xml用来配置web中服务调用的uri和对应服务指定的是哪个class文件 classes ...