您的位置:首页 > 其它

CodeForces 892B Wrath

2017-11-25 18:58 288 查看
Description:

Hands that shed innocent blood!

There are n guilty people in a line, the
i-th of them holds a claw with length
Li. The bell rings and every person kills some of people in front of him. All people kill others at the same time. Namely, the
i-th person kills the
j-th person if and only if j < i and
j ≥ i - Li.

You are given lengths of the claws. You need to find the total number of alive people after the bell rings.

Input

The first line contains one integer n (1 ≤ n ≤ 106) — the number of guilty people.

Second line contains n space-separated integers
L1, L2, ..., Ln (0 ≤ Li ≤ 109),
where Li is the length of the
i-th person's claw.

Output

Print one integer — the total number of alive people after the bell rings.

Example

Input
4
0 1 0 10


Output
1


Input
2
0 0


Output
2


Input
10
1 1 3 0 0 0 2 1 0 3


Output
3


Note

In first sample the last person kills everyone in front of him.

题目大意:

有n个人, 他们每个人都有个攻击范围而且他们只会ko位于他们前方的他们能够得到的人。 现在他们同时开始进行ko, 问最后活了多少人。

解题思路:

因为是同时开始ko并且每个人的攻击范围都不一样, 所以为了确保同时开始要从后往前遍历(他们只会ko他们前面的人)即可。 设一个临时变量去保存每次ko的最远距离是多少。

代码:

#include <iostream>

#include <sstream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <iomanip>

#include <utility>

#include <string>

#include <cmath>

#include <vector>

#include <bitset>

#include <stack>

#include <queue>

#include <deque>

#include <map>

#include <set>

using namespace std;

/*tools:

 *ios::sync_with_stdio(false);

 *freopen("input.txt", "r", stdin);

 */

typedef long long ll;

typedef unsigned long long ull;

const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};

const ll ll_inf = 0x7fffffff;

const int inf = 0x3f3f3f;

const int mod = 1000000;

const int Max = (int) 1e6 + 9;

int n, arr[Max];

int main() {

    //freopen("input.txt", "r", stdin);

    scanf("%d", &n);

    for (int i = 0; i < n; ++i) {

        scanf("%d", &arr[i]);

    }

    // definition

    int cur = n - 1, ans = 1;

    for (int i = n - 1; i >= 0; --i) {

        if (i - arr[i] <= 0) {

            printf("%d\n", ans);

            return 0;

        }

        if (i - arr[i] < cur) {

            cur = i - arr[i];

        }

        if (i == cur) {

            ans++;

        }

    }

    printf("%d\n", ans);

    //return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: