21039_Hermes

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

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

Pro.ID

21039

Title

Hermes

Title链接

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

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 1000/500 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    In a modern city for Greek gods, the streets are geometrically arranged as a grid with integer coordinates with streets parallel to the x and y axes. For each integer value Z, there is a horizontal street at y=Z and a vertical street at x=Z. This way, integer coordinate pairs represent the street junctions. During the hot days, the gods rest in cafeterias at street junctions. Messenger Hermes is to send photon messages to gods resting in the cafeterias by only moving along the city streets. Each message is for a single god, and it does not matter if the other gods see the message.

    The messages are to be sent in a given order, and Hermes is provided the coordinates of the cafeterias in that order. Hermes starts from (0, 0). To send a message to a cafeteria at (Xi, Yi), Hermes only needs to visit some point on the same horizontal street (with y-coordinate Yi) or on the same vertical street (with x-coordinate Xi). Having sent all of the messages, Hermes stops.

    You are to write a program that, given a sequence of cafeterias, finds the minimum total distance Hermes needs to travel to send the messages.

    输入

    The first line contains one integer N: the number of messages to be sent. The following N lines contain the coordinates of the N street junctions where the messages are to be sent. These N lines are in the order in which the messages are to be sent. Each of these N lines contains two integers: first the x-coordinate and then the y-coordinate of the street junction.

    1 ≤ N ≤ 20000, -1000 ≤ Xi, Yi ≤ 1000

    输出

    Description

    In a modern city for Greek gods, the streets are geometrically arranged as a grid with integer coordinates with streets parallel to the x and y axes. For each integer value Z, there is a horizontal street at y=Z and a vertical street at x=Z. This way, integer coordinate pairs represent the street junctions. During the hot days, the gods rest in cafeterias at street junctions. Messenger Hermes is to send photon messages to gods resting in the cafeterias by only moving along the city streets. Each message is for a single god, and it does not matter if the other gods see the message.

    The messages are to be sent in a given order, and Hermes is provided the coordinates of the cafeterias in that order. Hermes starts from (0, 0). To send a message to a cafeteria at (Xi, Yi), Hermes only needs to visit some point on the same horizontal street (with y-coordinate Yi) or on the same vertical street (with x-coordinate Xi). Having sent all of the messages, Hermes stops.

    You are to write a program that, given a sequence of cafeterias, finds the minimum total distance Hermes needs to travel to send the messages.

    Input

    The first line contains one integer N: the number of messages to be sent. The following N lines contain the coordinates of the N street junctions where the messages are to be sent. These N lines are in the order in which the messages are to be sent. Each of these N lines contains two integers: first the x-coordinate and then the y-coordinate of the street junction.

    1 ≤ N ≤ 20000, -1000 ≤ Xi, Yi ≤ 1000

    Output

    Output a single line containing one integer: the minimum total distance Hermes needs to travel to send the messages.

    Sample Input

    5
    8 3
    7 -7
    8 1
    -2 1
    -2 1
    6 -5

    Sample Output

    11

    Source

    样例输入

    5
    8 3
    7 -7
    8 1
    -2 1
    -2 1
    6 -5

    样例输出

    11

    提示

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部