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.
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.
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.
3 0 2 0
1
5 4 2 3 0 1
3
7 0 3 1 0 5 2 6
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.
看翻译都没有看懂什么意思,找了大牛才知道,汗= =
大意:一个数组,来回扫,每次只能扫到第一个比它小或者和它相等的数,要让所有的数都被找到,求最少转弯的次数
思路:贪心,在输入的时候寻找最小值,然后以最小值为初始开始扫,刚开始是从左边开始扫的,左边扫到右边,然后再从右边扫到左边,一直循环,直到全部扫完,扫的过程中记录转弯次数。
ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define MAXN 1010
#define INF 0xfffffff
#define MAX(a,b) a>b?a:b
using namespace std;
int a[MAXN];
int v[MAXN];
int main()
{
int i,n;
while(scanf("%d",&n)!=EOF)
{
int mi=INF;
memset(v,0,sizeof(v));
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]<mi)
mi=a[i];
}
int cnt=0;//记录扫过的数目
int k=mi;//从最小值开始扫
int dir=-1;//转弯次数
while(cnt<n)
{
dir++;
if(dir%2==0)
{
for(i=0;i<n;i++)
{
if(a[i]<=k&&!v[i])
{
v[i]=1;
cnt++;
k++;//找到一个最小值就+1,开始没想到
}
}
}
else
{
for(i=n-1;i>=0;i--)
{
if(a[i]<=k&&!v[i])
{
v[i]=1;
cnt++;
k++;
}
}
}
}
printf("%d\n",dir);
}
return 0;
}