Codeforces Round #323 (Div. 2) B. Robot's Task

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.

解题思路:

用一个sum值记录一下他所已知的电脑信息的数量,然后一直判断,从前往后,从后往前

一直判断知道sum = = m,用两个标记值分别标记从前往后是否转向和从后往前是否转向,

然后输出结果,(转向的次数)

上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))
#define MM1(a) memset(a, false, sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e3+5;
const int mod = 1000000007;
const double eps = 1e-7;
const double pi = 3.1415926;

int arr[maxn];
bool flag[maxn];
int main()
{
    int m, k;
    while(cin>>m)
    {
        for(int i=0; i<m; i++)
            cin>>arr[i];
        int sum = 0, ret = 0;
        MM1(flag);
        ///memset(flag, false, sizeof(false));
        bool ok = false, yes=false;
        while(sum < m)
        {
            ///注意需要在这里判断,
            if(ok)
                ret++;
            ///在初始化一次
            ok = false;
            for(int i=0; i<m; i++)
            {
                ///cout<<9;
                if(arr[i]<=sum && !flag[i])
                {
                    k = i;
                    ok = true;
                    sum++;
                    flag[i] = true;
                }
            }
            for(int j=k-1; j>=0; j--)
            {
                if(arr[j]<=sum && !flag[j])
                {
                    yes = true;
                    sum++;
                    flag[j] = true;
                }
            }
            if(yes)
                ret++;
            ///初始化为false
            yes = false;
        }
        cout<<ret<<endl;
    }
    return 0;
}
时间: 2024-08-29 05:04:20

Codeforces Round #323 (Div. 2) B. Robot&#39;s Task的相关文章

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

C. GCD Table The GCD table G of size n × n for an array of positive integers a of length n is defined by formula Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both

Codeforces Round #157 (Div. 1) C. Little Elephant and LCM (数学、dp)

C. Little Elephant and LCM time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output The Little Elephant loves the LCM (least common multiple) operation of a non-empty set of positive integers. The

Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)

Valera has array a, consisting of n integers a0,a1,...,an-1, and function f(x), taking an integer from 0 to 2n-1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number xconta

Codeforces Round #201 (Div. 1) / 346A Alice and Bob:数论&amp;amp;想法题

A. Alice and Bob http://codeforces.com/problemset/problem/346/A time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output It is so boring in the summer holiday, isn't it? So Alice and Bob have inven

Codeforces Round #311 (Div. 2) B. Pasha and Tea

题目链接:http://codeforces.com/contest/557/problem/B 题意:给你n个boy,n个girl ,然后w表示茶壶的最大容量,然后n个茶杯,每个都有不同的容量,要求boy的茶杯里的茶水是girl的两倍,且boy和boy的容量一样,girl和girl的容量一样,问如何倒茶,求最大的倒茶量 #include <iostream> #include <cstdio> #include <algorithm> using namespace

Codeforces Round #299 (Div. 2) A. Tavas and Nafas

题目链接:http://codeforces.com/problemset/problem/535/A #include <iostream> #include <string> using namespace std; int main() { string s1[10]={"zero","one","two","three","four","five",&qu

Codeforces Round #308 (Div. 2) A. Vanya and Table

题目链接:http://codeforces.com/contest/552/problem/A hint: 就是求几个矩形的面积 #include <iostream> #include <cmath> using namespace std; struct point { int x; int y; }a[2]; int main() { int m; while(cin>>m) { int sum=0; while(m--) { //注释的是方法一 cin>

Codeforces Round #311 (Div. 2) A. Ilya and Diplomas

题目链接:http://codeforces.com/contest/557/problem/A #include <iostream> using namespace std; int minn[3]; int maxx[3]; int main() { int m; while(cin>>m) { int ans[3]={0}; for(int i=0; i<3; i++) cin>>minn[i]>>maxx[i]; for(int i=0; i

Codeforces Round #308 (Div. 2) Vanya and Books

题目链接:http://codeforces.com/contest/552/problem/B 题意:就是求有几个数字: eg:13:1 2 3 4 5 6 4 7 8 9 1 0 1 1 1 2 1 3 一共17个数字 #include <iostream> using namespace std; long long a[12]={0,9,99,999,9999,99999,999999,9999999,99999999,999999999,9999999999}; int main()