Title Description
Sunday, Xiao Ming finished his homework on his classmates about sport.
They came to a clearing, drew N consecutive squares, each square on a random fill a number, we start from the first grid, every time you can jump back no more than a few of the squares of the current step number, we start this game, the minimum number of steps to see who jump to the last grid.
Xiao Ming as leader of the obviously wanted to win, so he wants you to help him.
Input and output formats
Input formats:
The first line contains an integer N (N <5000), is the number of grid painted;
second line contains N integers representing the number of ai (ai≤1000) on each grid.
Output formats:
Output line, represents the minimum number of hops.
Sample input and output
Sample input:
5
2 3 1 1 1
Sample output:
2
This question is at first glance seems to be back
then timeout
and then found to reverse thinking
From the back to find
From the back to find
After looking forward!
With code:
#include<iostream>
using namespace std;
int n,a[5005],now,step;
int main()
{
cin>>n;
for(register int i=1;i<=n;++i) cin>>a[i];
now=n;
while(now!=1)
{
for(register int i=1;i<=now;++i)
{
if(now-i<=a[i])
{
now=i;
++step;
break;
}
}
}
cout<<step;
}