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. vs2015“当前不会命中断点 还没有为该文档加载任何符号”的解决方法

    解决方法:工具-选项-调试 -(启用“仅我的代码”)勾去掉

  2. common-fileupload组件实现java文件上传和下载

    简介:文件上传和下载是java web中常见的操作,文件上传主要是将文件通过IO流传放到服务器的某一个特定的文件夹下,而文件下载则是与文件上传相反,将文件从服务器的特定的文件夹下的文件通过IO流下载到 ...

  3. 三十三、MySQL 导入数据

    MySQL 导入数据 本章节我们为大家介绍几种简单的 MySQL 导出的数据的命令. 1.mysql 命令导入 使用 mysql 命令导入语法格式为: mysql -u用户名 -p密码 < 要导 ...

  4. mybatis的优缺点及应用场合

    mybatis框架的优点 与jdbc相比,减少了50%以上的代码量 mybatis是最简单的持久化框架,小巧简单且易学 mybatis想到灵活,不会对应用程序或者数据库的现有设计强加任何影响,SQL写 ...

  5. 【CSS】非常简单的css实现div悬浮页面底部

    <div id="demo_div"></div> <style> #demo_div{ left:; position: fixed; bot ...

  6. 使用U盘给笔记本重做系统

    **一.戴尔 Vostro 14 3000 Series **1. 开机时快速按F12进入BIOS界面 **2. 按照下图进行一系列的处理,把U盘被设置为第一启动项 **3. 插入U盘后进入老毛桃PE ...

  7. [译]The Python Tutorial#1. Whetting Your Appetite

    [译]The Python Tutorial#Whetting Your Appetite 1. Whetting Your Appetite 如果你需要使用计算机做很多工作,最终会发现很多任务需要自 ...

  8. scrapy框架简介和基础使用

    概念 为了爬取网站数据而编写的一款应用框架,出名,强大.所谓的框架其实就是一个集成了相应的功能且具有很强通用性的项目模板.(高性能的异步下载,解析,持久化……) 安装 linux mac os:pip ...

  9. Special Segments of Permutation - CodeForces - 1156E (笛卡尔树上的启发式合并)

    题意 给定一个全排列\(a\). 定义子区间\([l,r]\),当且仅当\(a_l + a_r = Max[l,r]\). 求\(a\)序列中子区间的个数. 题解 笛卡尔树上的启发式合并. \(200 ...

  10. WPF实现QQ群文件列表动画(二)

    上篇(WPF实现QQ群文件列表动画(一))介绍了WPF实现QQ群文件列表动画的大致思路,结合我之前讲过的WPF里ItemsControl的分组实现,实现起来问题不大,以下是效果图: 其实就是个List ...