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.
Examples
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个人,然后会输入n个人的刀长,刀有多长就可以杀死自己左边几个人,假如第n个人的刀长是3,那么n-1和n-2和n-3都将会被他杀死,问按照这种操作,同时杀人(此时注意,如果一个人被杀死,那么他也会杀死自己改杀死的人),最后还活着几个人。
思路:悲惨的个人赛的第二道题,还是忽略了很多细节,而且当时可能心情太烦躁一直按着两重遍历的思想来斌给有想什么新的思路,以后会更加沉稳,这道题思路很简单,就是从后前遍历一遍,但是只能使用一层循环,每次杀人的更新是非常重要的一点,下面会给出我失败的代码和更改后的简单代码,希望自己以后思路会更加开阔。
超时代码:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=1e6+1000;
const int INF=0x3f3f3f3f;
const ll mod=1e6+7;
int a[maxn];
bool used[maxn];
int main()
{
// ios::sync_with_stdio(false);
// freopen("out.txt","w",stdout);
// freopen("in.txt","r",stdin);
memset(used,false,sizeof(used));
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
int cnt=n,l=n;
for(int i=n; i>=1; i--)
{
if(i-a[i]<l&&a[i]!=0)
{
int t=l-(i-a[i]),pos=l-1;
//cout<<"gg "<<t<<" "<<pos<<" "<<l<<" "<<i<<endl;
while(t--&&pos>=1)//每次都遍历所要杀的人十分的麻烦。思路不够简洁,不够开阔过于狭窄
{
if(used[pos]==false)
{
used[pos]=true;
cnt--;
}
pos--;
}
}
if(a[i]!=0) l=i-a[i];
else
{
if(l>i-a[i]) l=i-a[i]-1;
}
//cout<<"gg1 "<<l<<endl;
}
printf("%d\n",cnt);
return 0;
}
AC代码:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=1e6+1000;
const int INF=0x3f3f3f3f;
const ll mod=1e6+7;
int a[1000005];
int main()
{
// ios::sync_with_stdio(false);
// freopen("out.txt","w",stdout);
// freopen("in.txt","r",stdin);
int n; scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
int s=a[n-1],cnt=1;//cnt代表存活人数,最后一个人是无论如何都不会被杀死的
for(int i=n-2;i>=0;i--)
{
if(s==0) cnt++;//如果没人可杀则此人存活
s=max(s-1,a[i]);//看上一个人杀了人之后,下一个人杀的人数是否比他多
}
printf("%d",cnt);
return 0;
}