链接: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;
}

  

Codeforces Round #542(Div. 2) B.Two Cakes的更多相关文章

  1. Codeforces Round 542 (Div. 2)

    layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  2. Codeforces Round #542(Div. 2) CDE 思维场

    C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...

  3. Codeforces Round #542(Div. 2) A.Be Positive

    链接:https://codeforces.com/contest/1130/problem/A 题意: 给n个数,找出一个非0整数d,使所有n个数除以整数d后,数组中正数的数量>= n/2. ...

  4. Codeforces Round #542(Div. 2) D1.Toy Train

    链接:https://codeforces.com/contest/1130/problem/D1 题意: 给n个车站练成圈,给m个糖果,在车站上,要被运往某个位置,每到一个车站只能装一个糖果. 求从 ...

  5. Codeforces Round #542(Div. 2) C.Connect

    链接:https://codeforces.com/contest/1130/problem/C 题意: 给一个n*n的图,0表示地面,1表示水,给出起点和终点, 现要从起点到达终点,有一次在两个坐标 ...

  6. Codeforces Round #542 (Div. 1) 题解

    开学了住校了打不了深夜场 好难受啊QwQ A 显然对于每个起点,我们只需要贪心记录这个起点出发出去的糖果数量以及离自己最近的糖果 因为这个起点最后一次装载糖果一定是装载终点离自己最近的那个糖果 $ O ...

  7. Codeforces Round #542 Div. 1

    A:显然对于起点相同的糖果,应该按终点距离从大到小运.排个序对每个起点取max即可.读题花了一年还wa一发,自闭了. #include<iostream> #include<cstd ...

  8. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解

    Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...

  9. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

随机推荐

  1. Gym - 101147E E. Jumping —— bfs

    题目链接:http://codeforces.com/gym/101147/problem/E 题意:当人在第i个商店时,他可以向左或向右跳di段距离到达另一个商店(在范围之内),一个商店为一段距离. ...

  2. 关于git上传文件的一个小问题

    *** Please tell me who you are. Run git config --global user.email "you@example.com" git c ...

  3. ffmpeg 的一些学习网站

    http://blog.csdn.net/leixiaohua1020/article/category/1360795 雷霄骅(leixiaohua1020)的专栏 http://dranger.c ...

  4. llala js弹出层 颜色渐变

    网址:http://bbs.csdn.net/topics/370254842

  5. 精选Java面试题(二)

    Java中的方法覆盖重写(Overriding)和方法重载(Overloading)是什么意思? Java中的方法重载发生在同一个类里面两个或者是多个方法的方法名相同但是参数不同的情况.与此相对,方法 ...

  6. awk实现求和、平均、最大值和最小值的计算操作

    0.准备和数据文件 比如有一个数据文件,只有一列(在之前可以通过各种手段过滤出只有数字这一列),比如操作的响应时间 Txt代码  490898  1189235  20212  1494270  14 ...

  7. 阻止Eclipse一直building workspace

    Eclipse 一直不停 building workspace完美解决总结 一.产生这个问题的原因多种 1.自动升级 2.未正确关闭 3.maven下载lib挂起 等.. 二.解决总结 (1).解决方 ...

  8. [APIO 2015] 雅加达的摩天楼

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4070 [算法] 考虑将每个"Doge"向其所能到达的楼连边 直接 ...

  9. Watir RAutomation VS AutoIt to deal with popup

    Watir使用过程中,经常遇到的弹出框分为几类: 1. Alert 2. Confirm 3. Prompt 4. File select Watir 处理弹出框,有很多方法,可以应用 AutoIt来 ...

  10. 八、MyEclipse多次重装、删除注册表、重装系统激活都不成功,终极解决方法 - imsoft.cnblogs

    MyEclipse(2010,2014)激活不成功的结论: [问题原因]激活不成功时,主要是激活的密钥文件.myeclipse.properties不在指定的位置.(一般都在D.E.F.G等盘符根目录 ...