博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
阅读量:7108 次
发布时间:2019-06-28

本文共 961 字,大约阅读时间需要 3 分钟。

题目链接:

思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记忆化搜索记忆,vis数组标记那些已经访问过的状态。

#include 
#include
#include
#include
#define REP(i, a, b) for (int i = (a); i < (b); ++i)#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)using namespace std;const int MAX_N = (200000 + 100);long long dp[MAX_N][2];int vis[MAX_N][2];int N, a[MAX_N];long long dfs(int now, int dir){ if (now <= 0 || now > N) return 0; if (~dp[now][dir]) return dp[now][dir]; if (vis[now][dir]) return -1; vis[now][dir] = 1; long long res = dfs(dir ? now + a[now] : now - a[now], dir ^ 1); return dp[now][dir] = (res < 0 ? -1 : res + a[now]);}int main(){ while (cin >> N) { FOR(i, 2, N) cin >> a[i]; memset(dp, -1, sizeof(dp)); memset(vis, 0, sizeof(vis)); FOR(i, 1, N - 1) { long long ans = dfs(i + 1, 0); cout << (ans < 0 ? -1 : ans + i) << endl; } } return 0;}

转载地址:http://znlhl.baihongyu.com/

你可能感兴趣的文章
Thinkphp中eq,neq,gt,lt等表达式缩写
查看>>
开机就出现GRUB> 进不了系统?
查看>>
Windows Server 2008 R2 实现通过WEB方式修改域账号密码
查看>>
kvm--初步认识
查看>>
我的友情链接
查看>>
pacemaker搭建HTTP集群
查看>>
Linux时间同步
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
PHP获取目标路径的文件目录
查看>>
Teechart数据库图表介绍
查看>>
LNMP笔记:域名重定向、读写权限、显示WP主题、北京时间
查看>>
List of MIME Types by Content Type
查看>>
001—玩转Mysql的配置文件(my.ini)
查看>>
Crunch Bang配置Conky系统监视工具
查看>>
VR、AR與MR的區別
查看>>
大数 进制转换 10-16
查看>>
Windows 应用生态系统 (2)
查看>>
poj3624 0-1背包模板
查看>>
第四次作业
查看>>