题意:你有一个正整数序列 {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(1≤i≤n−1) ,令 a1 等于 i ,输出上面那个程序 y 的值,如果程序陷入死循环输出-1。
第一行 n 。
第二行 n−1 个整数 a2,a3,⋯,an 。
输出 n−1 行,为枚举每个 i 得到的结果。
4
2 4 1
3 6 8
3
1 2
-1 -1
In the firse sample:
40%: n≤1000
100%: 2≤n≤105,1≤ai≤109
这种给程序求结果的题目肯定要重构程序(因为出题人肯定想着法子坑你)
目前给的程序一次循环有两次退出,不方便表示状态,不妨把程序改成这样子
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;
}
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.
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 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.
4
2 4 1
3
6
8
3
1 2
-1
-1
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.