zoukankan      html  css  js  c++  java
  • codeforces 583B Robot's Task

    B. Robot's Task

     
     

    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.

    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.

     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     int i,j;
     8     int a[1005];
     9     bool used[1005];
    10     int n;
    11     while(scanf("%d",&n)!=EOF)
    12     {
    13         memset(used,0,sizeof(used));
    14         int have=0,ans=0;
    15         for(i=0;i<n;i++)
    16             scanf("%d",&a[i]);
    17         while(1)
    18         {
    19             for(i=0;i<n;i++)
    20                 if(have>=a[i]&&!used[i])
    21                 {
    22                     have++;
    23                     used[i]=1;
    24                 }
    25             for(j=0;j<n;j++)
    26                 if(!used[j])break;
    27             if(i==n&&j==n)break;
    28             ans++;
    29             for(i=n-1;i>=0;i--)
    30                 if(have>=a[i]&&!used[i])
    31                 {
    32                     have++;
    33                     used[i]=1;
    34                 }
    35             for(j=0;j<n;j++)
    36                 if(!used[j])break;
    37             if(i==-1&&j==n)break;
    38             ans++;
    39         }
    40         printf("%d
    ",ans);
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    python修改python unittest的运行顺序
    史上最强大的python selenium webdriver的包装
    第六种方式,python使用cached_property缓存装饰器和自定义cached_class_property装饰器,动态添加类属性(三),selnium webdriver类无限实例化控制成单浏览器。
    python带参装饰器的改良版
    第五种方式,python使用组合来添加类方法和属性(二),以selenium的webdriver为例
    python装饰器、继承、元类、mixin,四种給类动态添加类属性和方法的方式(一)
    linux添加PYTHONPATH环境变量
    linux 按照端口一句命令杀死进程,按照进程名称一句命令杀死进程
    python __all__用法
    使用pycharm,追求最优的代码。
  • 原文地址:https://www.cnblogs.com/homura/p/4991493.html
Copyright © 2011-2022 走看看