思路是树形DP+状态压缩。其实仅有2个状态,奇数次来到x或者偶数次来到x。(因为对x的更新不同)。
同时开辟visit数组,解决环。注意,一旦遇到环结果就是-1。DP数组存放第奇数/偶数次来到x时,对y的改变两。

 /* 283B */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 2e5+;
int a[maxn];
__int64 dp[maxn][];
bool visit[maxn][];
int n; __int64 dfs(int x, int d) {
if (x<= || x>n)
return ; if (dp[x][d] != )
return dp[x][d]; if (visit[x][d]) {
dp[x][d] = -;
return -;
} visit[x][d] = true;
int x_ = d ? x + a[x] : x - a[x];
int d_ = !d;
__int64 ret = dfs(x_, d_); ret = ret < ? - : ret + a[x];
dp[x][d] = ret; return ret;
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif __int64 x, y, tmp; scanf("%d", &n);
rep(i, , n+)
scanf("%d", &a[i]); memset(visit, false, sizeof(visit));
dp[][] = -;
dp[][] = ; rep(i, , n) {
y = i;
x = i + ;
tmp = dfs(x, );
y = tmp< ? - : y+tmp;
printf("%I64d\n", y);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【CF】174 Div.1 B Cow Program的更多相关文章

  1. 【CF】121 Div.1 C. Fools and Roads

    题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...

  2. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

  3. 【CF】110 Div.1 B. Suspects

    这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...

  4. 【CF】222 Div.1 B Preparing for the Contest

    这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心.对时间二分, 对费用采用贪心. /* 377B */ #include <iostream> #include ...

  5. 【CF】207 Div.1 B.Xenia and Hamming

    这题目一看很牛逼,其实非常easy.求求最小公倍数,最大公约数,均摊复杂度其实就是O(n). /* 356B */ #include <iostream> #include <str ...

  6. 【CF】142 Div.1 B. Planes

    SPFA.注意状态转移条件,ans的求解需要在bfs中间求解.因为只要到了地点n,则无需等待其他tourist.还是蛮简单的,注意细节. /* 229B */ #include <iostrea ...

  7. 【CF】196 Div.2 D. Book of Evil

    显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的).但是看了一下数据大小,应该会TLE.然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径.结果灵感就来 ...

  8. 【CF】223 Div.1 C Sereja and Brackets

    水线段树. /* 380C */ #include <iostream> #include <string> #include <map> #include < ...

  9. 【CF】259 Div.1 B Little Pony and Harmony Chest

    还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...

随机推荐

  1. DI(依赖注入)简单理解 NO1

    依赖注入:目的削减程序的耦合度,达到高内聚/低耦合 常用形式:Interface Driven Design接口驱动,接口驱动有很多好处,可以提供不同灵活的子类实现,增加代码稳定和健壮性等等.通过Io ...

  2. Ajax返回数据类型

    MVC中,如果从controller返回的不是一个html,而是一个文本,使用AJAX中如何获取? 后台代码: public ActionResult UploadPicture() { return ...

  3. ubuntu 安装 fcitx

    安装fcitx (1)添加ppa源 sudo add-apt-repository ppa:fcitx-team/nightly 或 sudo add-apt-repository ppa:fcitx ...

  4. APP评价(星星点赞)很简单

    1.用代码或者storyboard创建5个button(现在一般都是5个星星) 我用的是storyboard 记得一定要设置button的tag值 在.h中 @property (weak, nona ...

  5. call与apply的区别

    /** * 用 add 来替换 sub,add.call(sub,3,1) == add(3,1) ,所以运行结果为:alert(4); */ function add(a,b){ alert(a+b ...

  6. UIView-图层方法

    // // ViewController.m // UIView-图层概念 // // Created by wangtouwang on 15/5/5. // Copyright (c) 2015年 ...

  7. ActiveMQ基本配置与示例演示

    一.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 二.运行ActiveMQ 将apache-activemq-5.11.1-bin.zip解压,由于本系 ...

  8. 疯狂学习java web

    因工作需要,疯狂学习java web,只是这么多年一直从事C++开发,突然之间要接手同事的那么一大堆代码,真有无从下手的感觉,首先是要学习html,然后是js, 然后是jsp,当然还有各种框架,想想就 ...

  9. webstorm快捷方式

    刚开始在使用webstrom的时候,不知道快捷方式,感觉自己把webstorm当做记事本使用,真的挺傻的,在朋友的指导下,原来webstorm有快捷方式 一.界面操作 快捷键 说明 ctrl+shif ...

  10. J2EE中的HttpSession

    J2EE中的HttpSession总结: ①什么是session? session是服务器端技术,利用这个技术,服务器在运行时可以为每一个浏览器创建一个共享的session对象,由于 session为 ...