CodeForces 283B Cows and Program 记忆化搜索 推导

题意:你有一个正整数序列 {ai} ,大小为 n ,你还有2个变量x,y,执行以下程序:

x = 1; y = 0
while (1) {
    y += a[y]; x += a[x]
    if (x <= 0 || x > n) return
    y += a[y]; x -= a[x]
    if (x <= 0 || x > n) return
}

你的任务是得到一个序列 a2,a3,,an
枚举 i(1in1) ,令 a1 等于 i ,输出上面那个程序y的值,如果程序陷入死循环输出-1。

Input

第一行 n
第二行n1个整数 a2,a3,,an

Output

输出 n1 行,为枚举每个 i 得到的结果。

Sample

Input1

4
2 4 1

Output1

3 6 8

Input2

3
1 2

Output2

-1
-1

Explanation

In the firse sample:

  • For i=1, x becomes 120 and y becomes 1+2=3

    • For i =2,x becomes 131 and y becomes 2+4=6
    • For i =3,x becomes 1437 and y becomes 3+1+4=8
    • Hint

      40%: n1000
      100%: 2n105,1ai109

      Solution

      这种给程序求结果的题目肯定要重构程序(因为出题人肯定想着法子坑你)
      目前给的程序一次循环有两次退出,不方便表示状态,不妨把程序改成这样子

      for (x = 1, y = 0, j = 1; 0 < x && x <= n; j ^= 1) {
          y += a[x];
          if (j == 1) x += a[x]; else x -= a[x];
      }

      上面程序的意思(j表示当前是奇数次=1还是偶数次=0):

          inc(y, a[x]);
          如果是奇数次操作 inc(x, a[x])
          如果是偶数次操作 dec(x, a[x])
          如果x不在[1,n]内就退出,否则继续。

      发现, (x,j) 即可表示当前的状态,如果二元组(x,j)重复出现了,那么就会有死循环。
      因此本题是dp。。。令 fx,j 表示为奇数次或偶数次操作,到达 x 时的y值。
      按照上面的方法求解即可。
      而对于枚举 a1 ,相对于 a1=0 的情况,发现对于每一个二元组 (x,j) y 的值变大a1
      只需要把所有 (x,j) 二元组都处理出来即可。时间复杂度是 O(n)
      另外 y 的计算顺序没太大关系,如果我知道y要加哪一些ax,那么顺着逆着都可以。

      实现是记忆化搜索,40行。

      #include <cstdio>
      #include <algorithm>
      #include <cstring>
      #define FOR(i,j,k) for(i=j;i<=k;i++)
      using namespace std;
      typedef long long ll;
      ll read() {
          ll s = 0, f = 1; char ch = getchar();
          for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = -1;
          for (; '0' <= ch && ch <= '9'; ch = getchar()) s = s * 10 + ch - '0';
          return s * f;
      }
      
      const int N = 200005;
      bool vis[N][2];
      ll dp[N][2], a[N], n;
      
      void dfs(ll x, bool j) {
          if (vis[x][j]) return;
          vis[x][j] = 1;
          ll y = j ? (x - a[x]) : (x + a[x]);
          if (y <= 0 || y > n) dp[x][j] = a[x];
          else {
              dfs(y, j ^ 1);
              if (dp[y][j ^ 1] != -1)
                  dp[x][j] = dp[y][j ^ 1] + a[x];
          }
      }
      
      int main() {
          ll i;
          n = read();
          FOR(i,2,n) a[i] = read();
          memset(dp, -1, sizeof dp);
          memset(vis, 0, sizeof vis);
          vis[1][0] = 1; vis[1][1] = 1;
          dp[1][1] = 0;
          FOR(i,2,n) dfs(i, 1);
          FOR(i,2,n) if (dp[i][1] != -1) dp[i][1] += i - 1;
          FOR(i,2,n) printf("%I64d\n", dp[i][1]);
          return 0;
      }

      B. Cow Program

      time limit per test2 seconds
      memory limit per test256 megabytes
      inputstandard input
      outputstandard output
      Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1, a2, …, an of positive integers:

      Initially, x = 1 and y = 0. If, after any step, x ≤ 0 or x > n, the program immediately terminates.
      The program increases both x and y by a value equal to ax simultaneously.
      The program now increases y by ax while decreasing x by ax.
      The program executes steps 2 and 3 (first step 2, then step 3) repeatedly until it terminates (it may never terminate). So, the sequence of executed steps may start with: step 2, step 3, step 2, step 3, step 2 and so on.
      The cows are not very good at arithmetic though, and they want to see how the program works. Please help them!

      You are given the sequence a2, a3, …, an. Suppose for each i (1 ≤ i ≤ n - 1) we run the program on the sequence i, a2, a3, …, an. For each such run output the final value of y if the program terminates or -1 if it does not terminate.

      Input

      The first line contains a single integer, n (2 ≤ n ≤ 2·105). The next line contains n - 1 space separated integers, a2, a3, …, an (1 ≤ ai ≤ 109).

      Output

      Output n - 1 lines. On the i-th line, print the requested value when the program is run on the sequence i, a2, a3, …an.

      Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

      Sample test(s)

      input

      4
      2 4 1

      output

      3
      6
      8

      input

      3
      1 2

      output

      -1
      -1

      Note

      In the first sample

      For i = 1,  x becomes and y becomes 1 + 2 = 3.
      For i = 2,  x becomes and y becomes 2 + 4 = 6.
      For i = 3,  x becomes and y becomes 3 + 1 + 4 = 8.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值