Description
Robot Doc is located in the hall, with n computers stand in a line, numbered from left to right from 1 to n. Each computer contains exactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the i-th of them, the robot needs to collect at least ai any pieces of information from the other computers. Doc can hack the computer only if he is right next to it.
The robot is assembled using modern technologies and can move along the line of computers in either of the two possible directions, but the change of direction requires a large amount of resources from Doc. Tell the minimum number of changes of direction, which the robot will have to make to collect all n parts of information if initially it is next to computer with number 1.
It is guaranteed that there exists at least one sequence of the robot's actions, which leads to the collection of all information. Initially Doc doesn't have any pieces of information.
Input
The first line contains number n (1 ≤ n ≤ 1000). The second line contains n non-negative integers a1, a2, ..., an (0 ≤ ai < n), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information.
Output
Print a single number — the minimum number of changes in direction that the robot will have to make in order to collect all n parts of information.
Sample Input
3 0 2 0
1
5 4 2 3 0 1
3
7 0 3 1 0 5 2 6
2
Sample Output
Hint
打表,来回的循环,已经获取的标为1,没有的标为0;#include<string.h>
int main()
{
int i,n,a[1000]= {0},b[1000],zi=0,zhuan=0;
scanf("%d",&n);
for(i=0; i<=n-1; i++)
scanf("%d",&b[i]);
for(;;)
{
if(zi==n)
break;
for(i=0; i<=n-1; i++)
{
if(b[i]<=zi&&a[i]==0)
{
a[i]=1;
zi+=1;
}
}
zhuan+=1;
if(zi==n)
break;
for(i=n-1;i>=0;i--)
{
if(b[i]<=zi&&a[i]==0)
{
a[i]=1;
zi+=1;
}
}
zhuan+=1;
if(zi==n)
break;
}
printf("%d\n",zhuan-1);
return 0;
}
3682

被折叠的 条评论
为什么被折叠?



