Codeforces Round #542 1130B Two Cakes

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⋅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 aiai (1≤ai≤n1≤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: 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.

Input

The first line of the input contains an integer number nn — the number of tiers in each cake (1≤n≤1051≤n≤105).

The second line contains 2⋅n2⋅n integers a1,a2,…,a2na1,a2,…,a2n (1≤ai≤n1≤ai≤n), where aiai 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.

Output

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.

Examples

Input

3
1 1 2 2 3 3

Output

9

Input

2
2 1 1 2

Output

5

Input

4
4 1 3 2 2 3 1 4

Output

17

题意:有两个人Sasha和 Dima 。。他们各自要做一个n层的蛋糕。蛋糕的上层必须比下层的小。现在有2n个蛋糕房,每个蛋糕房只有一个尺寸为ai的蛋糕(一个蛋糕房只卖一个蛋糕)。输入的第一行是整数n,第二行给2n个数字,第i个数字ai表示尺寸为ai的蛋糕出现在第i个位置。一开始两个人都出现在第一个位置,每两个相邻的位置距离是1。问他们都做好n层的蛋糕以后他们走的距离的和的最小值是多少。数据保证1,2.....n 这些数字每个数都出现两次。 总而言之,就是两个人从左边第一个位置出发,两个人都要从第一个位置走到数字1,2,....n。问他们都走到n这个数字以后,最短走了多少距离。。。

 

题解:首先两个人都先走到1这个数字,然后从1走到2。。两个人从两个1走到两个2这个过程,有两种走法。这时候取距离小的那种走法。然后两个人从两个2走到两个3.....以此类推。。

#include<cstdio>
#include<cmath>
struct Num{
    int cnt,pos[2];
    Num(){cnt = 0;}
public:
    void Push(int x){
        pos[cnt++] = x;
    }
}num[101000];
int min(int a,int b){
    return a>b?b:a;
}
int main(){
    int n,x;
    scanf("%d",&n);
    for(int i=1;i<=2*n;i++){
        scanf("%d",&x);
        num[x].Push(i);
    }
    long long ans = num[1].pos[0] + num[1].pos[1] - 2;
    for(int i=2;i<=n;i++){
        int d1 = abs(num[i].pos[0]-num[i-1].pos[0]);
        d1 += abs(num[i].pos[1]-num[i-1].pos[1]);
        
        int d2 = abs(num[i].pos[1]-num[i-1].pos[0]);
        d2 += abs(num[i].pos[0]-num[i-1].pos[1]);
        ans += min(d1,d2);
    } 
    printf("%I64d\n",ans);
    return 0; 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值