Educational Codeforces Round 19 E. Array Queries 暴力

连接:

http://codeforces.com/contest/797/problem/E

题意:

给你一个a数组,q次询问,每次给你个p,k   每次操作使p=p+a[p]+k,直到p大于n为止

问一共操作了多少次

题解:

先预处理k不超过325时候的答案,当k大于325的时候就暴力算

代码:

 1 #include <map>
 2 #include <set>
 3 #include <cmath>
 4 #include <queue>
 5 #include <stack>
 6 #include <cstdio>
 7 #include <string>
 8 #include <vector>
 9 #include <cstdlib>
10 #include <cstring>
11 #include <sstream>
12 #include <iostream>
13 #include <algorithm>
14 #include <functional>
15 using namespace std;
16 #define rep(i,a,n) for (int i=a;i<n;i++)
17 #define per(i,a,n) for (int i=n-1;i>=a;i--)
18 #define all(x) (x).begin(),(x).end()
19 #define pb push_back
20 #define mp make_pair
21 #define lson l,m,rt<<1  
22 #define rson m+1,r,rt<<1|1 
23 typedef long long ll;
24 typedef vector<int> VI;
25 typedef pair<int, int> PII;
26 const ll MOD = 1e9 + 7;
27 const int INF = 0x3f3f3f3f;
28 const int MAXN = 1e5 + 7;
29 // head
30 
31 const int BLK = 325;
32 int a[MAXN], dp[BLK][MAXN];
33 
34 int main() {
35     ios::sync_with_stdio(false);
36     int n;
37     cin >> n;
38     rep(i, 1, n + 1) cin >> a[i];
39     rep(k, 1, BLK) per(p, 1, n + 1) {
40         int q = p + a[p] + k;
41         if (q > n) dp[k][p] = 1;
42         else dp[k][p] = dp[k][q] + 1;
43     }
44     int q;
45     cin >> q;
46     while (q--) {
47         int p, k;
48         cin >> p >> k;
49         if (k < BLK) cout << dp[k][p] << endl;
50         else {
51             int sum = 0;
52             while (p <= n) p = p + a[p] + k, sum++;
53             cout << sum << endl;
54         }
55     }
56     return 0;
57 }

 

posted @ 2017-04-20 17:31  Flowersea  阅读(157)  评论(0编辑  收藏  举报