583B.md (2744B)
1 Source: https://codeforces.com/problemset/problem/583/B 2 3 B. Robot's Task 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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. 22 23 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. 24 25 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. 26 27 Input 28 29 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. 30 31 Output 32 33 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. 34 35 Examples 36 37 Input 38 39 ``` 40 3 41 0 2 0 42 ``` 43 44 Output 45 46 ``` 47 1 48 ``` 49 50 Input 51 52 ``` 53 5 54 4 2 3 0 1 55 ``` 56 57 Output 58 59 ``` 60 3 61 ``` 62 63 Input 64 65 ``` 66 7 67 0 3 1 0 5 2 6 68 ``` 69 70 Output 71 72 ``` 73 2 74 ``` 75 76 Note 77 78 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. 79 80 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. 81 82 In the third sample the optimal order of collecting parts from computers can look like that: 1->3->4->6->2->5->7.