zoukankan      html  css  js  c++  java
  • Codeforces Round #323 (Div. 2) B 贪心,暴力

    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)的循环可以解决。

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<queue>
     5 #include<stack>
     6 #include<vector>
     7 #include<map>
     8 #include<algorithm>
     9 #define ll __int64
    10 #define mod 1e9+7
    11 #define PI acos(-1.0)
    12 using namespace std;
    13 int n;
    14 int a[1005];
    15 int used[1005];
    16 int main()
    17 {
    18     scanf("%d",&n);
    19     for(int i=1; i<=n; i++)
    20         scanf("%d",&a[i]);
    21     int  flag=n;
    22     int have=0;
    23     int d=1;
    24     int ans=0;
    25     while(flag>0)
    26     {
    27         if(d)
    28         {
    29             for(int i=1; i<=n; i++)
    30             {
    31                 if(used[i]==0)
    32                 {
    33                     if(a[i]<=have)
    34                     {
    35                         have++;
    36                         used[i]=1;
    37                         flag--;
    38 
    39                     }
    40                 }
    41             }
    42             d=0;
    43             ans++;
    44         }
    45         else
    46         {
    47             for(int i=n; i>=1; i--)
    48             {
    49                 if(used[i]==0)
    50                 {
    51                     if(a[i]<=have)
    52                     {
    53                         have++;
    54                         used[i]=1;
    55                         flag--;
    56                     }
    57                 }
    58             }
    59             d=1;
    60             ans++;
    61         }
    62     }
    63     cout<<ans-1<<endl;
    64     return 0;
    65 }
  • 相关阅读:
    弱监督学习框架下的图像语义分割调研
    LeetCode(115):不同的子序列
    LeetCode(114): 二叉树展开为链表
    LeetCode(113):路径总和 II
    项目实战10.1—企业级自动化运维工具应用实战-ansible
    快收藏!高手Linux运维管理必备工具大全,你会吗?
    项目实战12.2—企业级监控工具应用实战-zabbix操作进阶
    项目实战12.1—企业级监控工具应用实战-zabbix安装与基础操作
    项目实战13—企业级虚拟化Virtualization
    计算机专用英语词汇1695个词汇表
  • 原文地址:https://www.cnblogs.com/hsd-/p/5683035.html
Copyright © 2011-2022 走看看