CF1130B.Two Cakes
普及-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Sasha and Dima want to buy two n-tier cakes. Each cake should consist of n different tiers: from the size of 1 to the size of n. Tiers should go in order from the smallest to the biggest (from top to bottom).
They live on the same street, there are 2⋅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 i-th house you can buy a tier of the size ai (1≤ai≤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: 1, then 2, then 3 and so on up to n.
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 1.
萨沙和迪马想要购买两个 n 层蛋糕。每个蛋糕应由 n 个互不相同的层组成:尺寸从 1 到 n。各层必须按尺寸从小到大(即从上到下)依次排列。
他们住在同一条街上,街上有 2⋅n 座房屋,从左到右排成一列。每座房屋中都有一家糕点店,可以购买一个蛋糕层。不幸的是,每家糕点店仅能出售一种特定尺寸的一个层:在第 i 座房屋中,只能购买尺寸为 ai 的层(其中 1≤ai≤n)。
由于两人需携带已购得的层,且无法将新层插入蛋糕中部,因此他们约定:必须按尺寸从小到大的顺序购买各层。也就是说,每人购买层的顺序均为:先买尺寸 1,再买尺寸 2,接着是尺寸 3,……,直至尺寸 n。
初始时,萨沙和迪马均位于第一座(最左侧)房屋旁。请输出他们为购买这两个蛋糕总共需要行走的最小距离。任意两座相邻房屋之间的距离恰好为 1。
输入格式
The first line of the input contains an integer number n — the number of tiers in each cake (1≤n≤105).
The second line contains 2⋅n integers a1,a2,…,a2n (1≤ai≤n), where ai is equal to the size of the tier, which can be bought in the i-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 1 to n occurs in a exactly two times.
输入的第一行包含一个整数 n —— 每个蛋糕的层数(1≤n≤105)。
第二行包含 2⋅n 个整数 a1,a2,…,a2n(1≤ai≤n),其中 ai 表示在第 i 个房子中可购买的蛋糕层的尺寸。注意:在每个房子中你只能购买一层蛋糕。保证 1 到 n 中的每个数字在数组 a 中恰好出现两次。
输出格式
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 n tiers in ascending order of their sizes.
输出一个数字——两人总共需要行走的最小距离,以便买齐两种蛋糕。两人可以同时位于同一栋房子处。他们从第一栋(最左侧)房子出发。每人应按尺寸升序购买 n 层蛋糕。
输入输出样例
输入#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 1 near the 1-st house (a1=1);
- Dima goes to the house 2;
- Dima buys a tier of size 1 near the 2-nd house (a2=1);
- Sasha goes to the house 4;
- Sasha buys a tier of size 2 near the 4-th house (a4=2);
- Sasha goes to the house 5;
- Sasha buys a tier of size 3 near the 5-th house (a5=3);
- Dima goes to the house 3;
- Dima buys a tier of size 2 near the 3-rd house (a3=2);
- Dima goes to the house 6;
- Dima buys a tier of size 3 near the 6-th house (a6=3).
So, Sasha goes the distance 3+1=4, and Dima goes the distance 1+1+3=5. In total, they cover a distance of 4+5=9. You can make sure that with any other sequence of actions they will walk no less distance.
在第一个例子中,可能的最优操作序列为:
- 萨沙在第 1 栋房屋附近购买一个尺寸为 1 的台阶(a1=1);
- 狄马前往第 2 栋房屋;
- 狄马在第 2 栋房屋附近购买一个尺寸为 1 的台阶(a2=1);
- 萨沙前往第 4 栋房屋;
- 萨沙在第 4 栋房屋附近购买一个尺寸为 2 的台阶(a4=2);
- 萨沙前往第 5 栋房屋;
- 萨沙在第 5 栋房屋附近购买一个尺寸为 3 的台阶(a5=3);
- 狄马前往第 3 栋房屋;
- 狄马在第 3 栋房屋附近购买一个尺寸为 2 的台阶(a3=2);
- 狄马前往第 6 栋房屋;
- 狄马在第 6 栋房屋附近购买一个尺寸为 3 的台阶(a6=3)。
因此,萨沙行走的距离为 3+1=4,狄马行走的距离为 1+1+3=5。两人总共行走的距离为 4+5=9。你可以验证:对于任何其他操作序列,他们行走的总距离均不会小于 9。
输入解题思路,AI测评打分。不知道怎么写?