1 second

256 megabytes

standard input

standard output

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 *a*_{i} 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.

The first line contains number *n* (1 ≤ *n* ≤ 1000). The second line contains *n* non-negative integers *a*_{1}, *a*_{2}, ..., *a*_{n} (0 ≤ *a*_{i} < *n*), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information.

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.

1 2 |
3 0 2 0 |

1 |
1 |

1 2 |
5 4 2 3 0 1 |

1 |
3 |

1 2 |
7 0 3 1 0 5 2 6 |

1 |
2 |

In the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece.

In the second sample to collect all the pieces of information in the optimal manner, Doc can go to the fourth computer and get the piece of information, then go to the fifth computer with one piece and get another one, then go to the second computer in the same manner, then to the third one and finally, to the first one. Changes of direction will take place before moving from the fifth to the second computer, then from the second to the third computer, then from the third to the first computer.

In the third sample the optimal order of collecting parts from computers can look like that: 1->3->4->6->2->5->7.

题意：从左往右有n个电脑，要破解第i台电脑，需要先破解其他a[i]台电脑。现在机器人在最左端，问最少要调头几次，才能破解所有的电脑。

思路：从左往右跑一次，能破解的就破解，再从右往左，能破解的就破解，如此循环。

代码：

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<set> #include<vector> #include<map> #include<cstdlib> #include<string> #include<queue> using namespace std; typedef __int64 LL; const int maxn = 1005; bool f[maxn]; int a[maxn]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } int cur = 0; int cnt = 0; int ans = 0; while (cnt<n) { ans++; if (cur == 0) { for (int i = 1; i <= n; i++) if (cnt >= a[i] && !f[i]) { f[i] = 1; cnt++; } } else { for (int i = n; i >= 1; i--) if (cnt >= a[i] && !f[i]) { f[i] = 1; cnt++; } } cur ^= 1; } printf("%d\n", ans - 1); return 0; } |