CF1130B.Two Cakes

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Sasha and Dima want to buy two nn-tier cakes. Each cake should consist of nn different tiers: from the size of 11 to the size of nn. Tiers should go in order from the smallest to the biggest (from top to bottom).

They live on the same street, there are 2⋅n2 \cdot n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the ii-th house you can buy a tier of the size aia_i (1≤ai≤n1 \le a_i \le n).

Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 11, then 22, then 33 and so on up to nn.

Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 11.

萨沙和迪马想要购买两个 nn 层蛋糕。每个蛋糕应由 nn 个互不相同的层组成:尺寸从 11 到 nn。各层必须按尺寸从小到大(即从上到下)依次排列。

他们住在同一条街上,街上有 2⋅n2 \cdot n 座房屋,从左到右排成一列。每座房屋中都有一家糕点店,可以购买一个蛋糕层。不幸的是,每家糕点店仅能出售一种特定尺寸的一个层:在第 ii 座房屋中,只能购买尺寸为 aia_i 的层(其中 1≤ai≤n1 \le a_i \le n)。

由于两人需携带已购得的层,且无法将新层插入蛋糕中部,因此他们约定:必须按尺寸从小到大的顺序购买各层。也就是说,每人购买层的顺序均为:先买尺寸 11,再买尺寸 22,接着是尺寸 33,……,直至尺寸 nn。

初始时,萨沙和迪马均位于第一座(最左侧)房屋旁。请输出他们为购买这两个蛋糕总共需要行走的最小距离。任意两座相邻房屋之间的距离恰好为 11。

输入格式

The first line of the input contains an integer number nn — the number of tiers in each cake (1≤n≤1051 \le n \le 10^5).

The second line contains 2⋅n2 \cdot n integers a1,a2,…,a2na_1, a_2, \dots, a_{2n} (1≤ai≤n1 \le a_i \le n), where aia_i is equal to the size of the tier, which can be bought in the ii-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 11 to nn occurs in aa exactly two times.

输入的第一行包含一个整数 nn —— 每个蛋糕的层数(1≤n≤1051 \le n \le 10^5)。

第二行包含 2⋅n2 \cdot n 个整数 a1,a2,…,a2na_1, a_2, \dots, a_{2n}(1≤ai≤n1 \le a_i \le n),其中 aia_i 表示在第 ii 个房子中可购买的蛋糕层的尺寸。注意:在每个房子中你只能购买一层蛋糕。保证 11 到 nn 中的每个数字在数组 aa 中恰好出现两次。

输出格式

Print one number — the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy nn tiers in ascending order of their sizes.

输出一个数字——两人总共需要行走的最小距离,以便买齐两种蛋糕。两人可以同时位于同一栋房子处。他们从第一栋(最左侧)房子出发。每人应按尺寸升序购买 nn 层蛋糕。

输入输出样例

  • 输入#1

    3
    1 1 2 2 3 3

    输出#1

    9
  • 输入#2

    2
    2 1 1 2

    输出#2

    5
  • 输入#3

    4
    4 1 3 2 2 3 1 4

    输出#3

    17

说明/提示

In the first example, the possible optimal sequence of actions is:

  • Sasha buys a tier of size 11 near the 11-st house (a1=1a_1=1);
  • Dima goes to the house 22;
  • Dima buys a tier of size 11 near the 22-nd house (a2=1a_2=1);
  • Sasha goes to the house 44;
  • Sasha buys a tier of size 22 near the 44-th house (a4=2a_4=2);
  • Sasha goes to the house 55;
  • Sasha buys a tier of size 33 near the 55-th house (a5=3a_5=3);
  • Dima goes to the house 33;
  • Dima buys a tier of size 22 near the 33-rd house (a3=2a_3=2);
  • Dima goes to the house 66;
  • Dima buys a tier of size 33 near the 66-th house (a6=3a_6=3).

So, Sasha goes the distance 3+1=43+1=4, and Dima goes the distance 1+1+3=51+1+3=5. In total, they cover a distance of 4+5=94+5=9. You can make sure that with any other sequence of actions they will walk no less distance.

在第一个例子中,可能的最优操作序列为:

  • 萨沙在第 11 栋房屋附近购买一个尺寸为 11 的台阶(a1=1a_1=1);
  • 狄马前往第 22 栋房屋;
  • 狄马在第 22 栋房屋附近购买一个尺寸为 11 的台阶(a2=1a_2=1);
  • 萨沙前往第 44 栋房屋;
  • 萨沙在第 44 栋房屋附近购买一个尺寸为 22 的台阶(a4=2a_4=2);
  • 萨沙前往第 55 栋房屋;
  • 萨沙在第 55 栋房屋附近购买一个尺寸为 33 的台阶(a5=3a_5=3);
  • 狄马前往第 33 栋房屋;
  • 狄马在第 33 栋房屋附近购买一个尺寸为 22 的台阶(a3=2a_3=2);
  • 狄马前往第 66 栋房屋;
  • 狄马在第 66 栋房屋附近购买一个尺寸为 33 的台阶(a6=3a_6=3)。

因此,萨沙行走的距离为 3+1=43+1=4,狄马行走的距离为 1+1+3=51+1+3=5。两人总共行走的距离为 4+5=94+5=9。你可以验证:对于任何其他操作序列,他们行走的总距离均不会小于 99。

输入解题思路,AI测评打分。不知道怎么写?

首页