Codeforces Round #542(Div. 2) B.Two Cakes

链接:https://codeforces.com/contest/1130/problem/B

题意:

给定n和 2 * n个数,表示i位置卖ai层蛋糕,

有两个人在1号,必须严格按照1-n的顺序买蛋糕,同时每个店只买一个蛋糕 。

求所需的最短时间。

思路:

将每种蛋糕对应位置记录在二维数组。

从1-n挨个买,根据上一次的位置算出消耗。

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int MAXN = 1e5 + 10;

int a[MAXN][2];

int main()
{
    int n, x;
    cin >> n;
    for (int i = 1;i <= n * 2;i++)
    {
        cin >> x;
        if (a[x][0])
            a[x][1] = i;
        else
            a[x][0] = i;
    }
    int p1 = 1, p2 = 1;
    LL res = 0;
    for (int i = 1;i <= n;i++)
    {
        res += abs(a[i][0] - p1) + abs(a[i][1] - p2);
        p1 = a[i][0];
        p2 = a[i][1];
    }
    cout << res << endl;

    return 0;
}

  

原文地址:https://www.cnblogs.com/YDDDD/p/10447784.html