21513_Juggler

2022-5-16 18:19| 发布者: Hocassian| 查看: 32| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-4-89505361611200-Problem List-采集的数据-后羿采集器.html

Pro.ID

21513

Title

Juggler

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21513

AC

3

Submit

10

Ratio

30.00%

时间&空间限制

  • Time Limit: 6000/3000 MS (Java/Others)     Memory Limit: 65536/32768 K (Java/Others)
  • 描述

    As part of my magical juggling act, I am currently juggling a number of objects in a circular path with one hand. However, as my rather elaborate act ends, I wish to drop all of the objects in a specific order, in a minimal amount of time. On each move, I can either rotate all of the objects counterclockwise by one, clockwise by one, or drop the object currently in my hand. If I drop the object currently in my hand, the next object (clockwise) will fall into my hand. What's the minimum number of moves it takes to drop all of the balls I'm juggling?

    输入

    There will be several test cases in the input. Each test case begins with an integer n, (1 ≤ n ≤ 100,000) on its own line, indicating the total number of balls begin juggled. Each of the next n lines consists of a single integer, ki (1 ≤ kin), which describes a single ball: i is the position of the ball starting clockwise from the juggler's hand, and ki is the order in which the ball should be dropped. The set of numbers {k1, k2, …, kn} is guaranteed to be a permutation of the numbers 1..n. The input will terminate with a line containing a single 0.

    输出

    Description

    As part of my magical juggling act, I am currently juggling a number of objects in a circular path with one hand. However, as my rather elaborate act ends, I wish to drop all of the objects in a specific order, in a minimal amount of time. On each move, I can either rotate all of the objects counterclockwise by one, clockwise by one, or drop the object currently in my hand. If I drop the object currently in my hand, the next object (clockwise) will fall into my hand. What's the minimum number of moves it takes to drop all of the balls I'm juggling?

    Input

    There will be several test cases in the input. Each test case begins with an integer n, (1 ≤ n ≤ 100,000) on its own line, indicating the total number of balls begin juggled. Each of the next n lines consists of a single integer, ki (1 ≤ kin), which describes a single ball: i is the position of the ball starting clockwise from the juggler's hand, and ki is the order in which the ball should be dropped. The set of numbers {k1, k2, …, kn} is guaranteed to be a permutation of the numbers 1..n. The input will terminate with a line containing a single 0.

    Output

    For each test case, output a single integer on its own line, indicating the minimum number of moves I need to drop all of the balls in the desired order. Output no extra spaces, and do not separate answers with blank lines. All possible inputs yield answers which will fit in a signed 64-bit integer.

    Sample Input

    3
    3
    2
    1
    0

    Sample Output

    5

    Hint

    Explanation of the sample input: The first ball is in the juggler's hand and should be dropped third; the second ball is immediately clockwise from the first ball and should be dropped second; the third ball is immediately clockwise from the second ball and should be dropped last.

    Source

    样例输入

    3
    3
    2
    1
    0

    样例输出

    5

    提示

    Explanation of the sample input: The first ball is in the juggler's hand and should be dropped third; the second ball is immediately clockwise from the first ball and should be dropped second; the third ball is immediately clockwise from the second ball and should be dropped last.


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部