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
Output
Input
Output
Input
Output
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;
}
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;
}
相关文章推荐
- Codeforces 892B Wrath 【差分】
- Codeforces 892B Wrath (递推)
- codeforces-892B Wrath
- Codeforces 451C. Predict Outcome of the Game
- Codeforces 400 A. Inna and Choose Options 【Codeforces Round #234 (Div. 2)】
- 二分图 Codeforces - 862B Mahmoud and Ehab and the bipartiteness
- CodeForces 652B z-sort
- Codeforces 711C Coloring Trees
- Next Round_Codeforces
- CodeForces - 813C(最短路径水题)
- Codeforces 451B
- Codeforces 592A PawnChess 【水题】
- Codeforces-527c
- CodeForces 645D Robot Rapping Results Report(二分+拓扑排序)
- CodeForces 617B 2
- CodeForces 110A Nearly Lucky Number
- Codeforces 831D Office Keys【二分 】
- codeforces memsql Start[c]UP 2.0 C. Magic Trick
- codeforces 592D(树DP)
- codeforces 825B(DFS)