-->
当前位置:首页 > 题库

PROGRAMMING:Two cakes

Luz5年前 (2021-05-10)题库424
Today is Bobo's and Coco's birthday. They both want to buy an n-layer cake. The size of each layer of the cake varies from 1 to n from top to bottom. They live on the same street, and there are 2n pastry shops from left to right, where they can buy cake layers. Unfortunately, only one layer of a specific size can be purchased at each bakery: size a can be purchased at cake shop I$$_{ i}$$(1≤a$$_{ i} $$(≤ n). Since it is not possible to insert a new layer in the middle of the cake, each layer can only be purchased from the smallest to the largest. That is to say, each of them has to buy 1 first, then 2, then 3, and so on to n.
Initially, Bobo and coco were located near the first (far left) house. Output the minimum distance they have to walk to buy two cakes. The distance between any two pastry shops is exactly 1.
###Input format:
In the first line, enter a positive integer n (1 ≤ n ≤ 10 $$^ {5} $$), representing the number of layers of the cake. On the next line, enter 2n positive integers a$$_{ 1}$$,a$$_{ 2}$$…,a$$_{ 2n}$$(1≤a$$_{ i}$$≤n),a$$_{ i} $$represents the size of the cake layer that can be bought in the ith cake shop. Note that only one layer can be bought in a cake shop. The title input ensures that n cake layers can be bought in order of size.
###Output format:
Output the minimum distance they have to walk to buy two cakes. At first they were both in the leftmost cake shop.
###Input sample 1:
```in
three
1 1 2 2 3 3
```
###Output sample 1:
```out
nine
```
###Input sample 2:
```in
two
2 1 1 2
```
###Output sample 2:
```out
five
```
###Input sample 3:
```in
four
4 1 3 2 2 3 1 4
```
###Output sample 3:
```out
seventeen
```






answer:If there is no answer, please comment