Robot's Task(模拟)

机器人Doc位于一排有n台电脑的走廊上,每台电脑包含一条Doc想要获取的信息。破解第i台电脑需要先收集至少ai条其他电脑上的信息。机器人可以沿两个方向移动,但改变方向会消耗大量资源。求从第1台电脑开始,收集所有n条信息所需的最小方向改变次数。已确保存在一种可行的操作顺序。输入包含电脑数量n和每台电脑的破解需求a1, a2, ..., an,输出是机器人收集所有信息所需的最小方向改变次数。" 102794311,7930109,Android蓝牙OPP文件传输实现解析,"['Android开发', '蓝牙通信', 'OPP传输']
摘要由CSDN通过智能技术生成
B. Robot's Task
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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 containsexactly 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 test(s)
input
3
0 2 0
output
1
input
5
4 2 3 0 1
output
3
input
7
0 3 1 0 5 2 6
output
2
Note

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.


题意:
这题的题目根本看不懂(英语差你懂的),但是看了别人的代码后就懂了题意了。就是问你要走完这些数(A1,。。。,An)且这些数都是不超过n的,所以只要从左到右找<=(cnt=0)的,找到就加cnt++,这样子就能保证到cnt不超过n,且能够遍历一遍全部数了。若左边找完后cnt还是小于n,即转个方向去右边找,转到cnt==n为止。


AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
typedef __int64 ll;
#define T 1005
int a[T],b[T];
int main(){
    /*freopen("input.txt","r",stdin);*/
	int n,m,i,cnt,now;
	while(~scanf("%d",&n))
	{
		CRL(b);
		for(i=0;i<n&&scanf("%d",&a[i]);++i);
		cnt = now = 0;
		while(now<n)
		{
			if(cnt&1){
				for(i=n-1;i>=0;--i){
					if(a[i]<=now&&!b[i]){
						b[i]=1;
						now++;
					}
				}
			}
			else
			{
				for(i=0;i<n;++i){
					if(a[i]<=now&&!b[i]){
						now++;
						b[i]=1;
					}
				}
			}
			cnt++;
		}
		printf("%d\n",cnt-1);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值