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 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.

Examples
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.

题意:在一条直线上有n (1 ≤ n ≤ 1000)台计算机,依次标号为1,2,3,……,每台计算机分别有a1, a2, ..., an (0 ≤ ai < n)的信息收集难度。一个机器人负责收集每台计算机的信息。他从1号计算机开始,初试收集0个信息,每收集一台计算机的信息机器人就可以升一级,机器人只能收集到信息收集难度小于等于自己等级的计算机信息,一台计算机的信息只能被收集一次。机器人在放置计算机的直线上来回行走以便收集信息,但是机器人转身十分困难,问机器人最少转身多少次可以收集完所有计算机的信息。输出保证有解。

题解:询问最少的转身次数。贪心策略是每次走到末尾转身,保证每次转身前都可以尽可能多的收集信息。最多转身n次必能收集完所有信息,所以O(N*N)的循环可以解决。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<algorithm>
#define ll __int64
#define mod 1e9+7
#define PI acos(-1.0)
using namespace std;
int n;
int a[];
int used[];
int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%d",&a[i]);
int flag=n;
int have=;
int d=;
int ans=;
while(flag>)
{
if(d)
{
for(int i=; i<=n; i++)
{
if(used[i]==)
{
if(a[i]<=have)
{
have++;
used[i]=;
flag--; }
}
}
d=;
ans++;
}
else
{
for(int i=n; i>=; i--)
{
if(used[i]==)
{
if(a[i]<=have)
{
have++;
used[i]=;
flag--;
}
}
}
d=;
ans++;
}
}
cout<<ans-<<endl;
return ;
}

Codeforces Round #323 (Div. 2) B 贪心,暴力的更多相关文章

  1. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  2. Codeforces Round #404 (Div. 2)(A.水,暴力,B,排序,贪心)

    A. Anton and Polyhedrons time limit per test:2 seconds memory limit per test:256 megabytes input:sta ...

  3. Codeforces Round #323 (Div. 1) B. Once Again... 暴力

    B. Once Again... Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/582/probl ...

  4. Codeforces Round #323 (Div. 2) C. GCD Table 暴力

    C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C ...

  5. Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)

    A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  6. Codeforces Round #323 (Div. 2) C 无敌gcd 数学/贪心

    C. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列

                                                                                  D. Once Again... You a ...

  8. Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)

    对角线上的元素就是a[i],而且在所在行和列中最大, 首先可以确定的是最大的元素一定是a[i]之一,这让人想到到了排序. 经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是a[i] ...

  9. Codeforces Round #307 (Div. 2) B. ZgukistringZ 暴力

    B. ZgukistringZ Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/probl ...

随机推荐

  1. Docker容器学习--1

    Docker是PaaS 提供商 dotCloud 开源的一个基于 LXC 的高级容器引擎,源代码托管在 Github 上, 基于go语言并遵从Apache2.0协议开源.Docker是通过内核虚拟化技 ...

  2. 多页应用 Webpack4 配置优化与踩坑记录

    前言 最近新起了一个多页项目,之前都未使用 webpack4 ,于是准备上手实践一下.这篇文章主要就是一些配置介绍,对于正准备使用 webpack4 的同学,可以做一些参考. webpack4 相比之 ...

  3. 【Linux】安装mysql之设置远程访问权限

    最近重装了云主机,又要安装各种东西,其中一个就要设置mysql权限 出于学习方便,我在自己的云主机上安装的是phpstudy集成环境,所以要进入mysql控制台不能直接用“mysql -u root ...

  4. MongDB之各种删除操作

    接口IMongDaoDelete: package com.net.test.mongdb.dao; public interface IMongDaoDelete { public void del ...

  5. STM32串口中断实例二

    int main(void) { uint8_t a=;//LED高低电压控制 /* System Clocks Configuration */ RCC_Configuration(); //系统时 ...

  6. PAT乙级1088

    1088 三人行 (20 分) 子曰:“三人行,必有我师焉.择其善者而从之,其不善者而改之.” 本题给定甲.乙.丙三个人的能力值关系为:甲的能力值确定是 2 位正整数:把甲的能力值的 2 个数字调换位 ...

  7. DiyCode开源项目 TopicActivity 分析

    1.首先看看TopActivity效果.    2.TopicActivity是一个继承BaseActivity的.前面分析过BaseActivity了.主要有一个标题栏,有返回的图标. 3.贴一下T ...

  8. James Bach Rapid Test的感受

    前阶段拜读过James大神的快速测试,英文水平有限,阅读起来有点吃力,虽然想亲自参加大神的培训,一直没有机会,不过阿里牛人参加大神的培训,并总结的不错,现在谈谈自己的感想和看法. 进入测试行业不少年了 ...

  9. 当我们在讨论CQRS时,我们在讨论些神马?

    当我写下这个标题的时候,我就有些后悔了,题目有点大,不太好控制.但我还是打算尝试一下,通过这篇内容来说清楚CQRS模式,以及和这个模式关联的其它东西.希望我能说得清楚,你能看得明白,如果觉得不错,右下 ...

  10. MySQL时间字段究竟使用INT还是DateTime

    今天解析DEDECMS时发现deder的MYSQL时间字段,都是用 `senddata` ) unsigned '; 随后又在网上找到这篇文章,看来如果时间字段有参与运算,用int更好,一来检索时不用 ...