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 
 2 #include <set>
 3 #include 
 4 #include 
 5 #include 
 6 #include 
 7 #include <string>
 8 #include 
 9 #include 
10 #include 
11 #include 
12 #include 
13 #include 
14 #include 
15 using namespace std;
16 #define rep(i,a,n) for (int i=a;i17 #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 }

 

你可能感兴趣的:(Educational Codeforces Round 19 E. Array Queries 暴力)