链接: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. Vue的watch和computed属性

    Vue的watch属性 Vue的watch属性可以用来监听data属性中数据的变化 <!DOCTYPE html> <html> <head> <meta c ...

  2. 网站流量统计之PV和UV

    转自:http://blog.csdn.NET/webdesman/article/details/4062069 如果您是一个站长,或是一个SEO,您一定对于网站统计系统不会陌生,对于SEO新手来说 ...

  3. innerText和innerHTML

    起因 由于公司的项目以前不考虑浏览器的兼容性问题,当时只考虑ie8浏览器,封装的控件也只针对ie8,我后面的做的时候,也就针对ie8,最近发现,封装的日期控件,在firefox竟然没法显示出来,去看J ...

  4. maven中常用命令

    1. 更新本地仓库, 首先确认C:\users\pengqiong\ 路径下有相应的pom文件 mvn clean package install:

  5. python 基础之第十三天(xineted服务器,forking,多线程)

  6. Exceprtion:e createQuery is not valid without active transaction; nested exception is org.hibernate.HibernateException: createQuery is not valid without active transaction

    如果增加配置了current_session_context_class属性,查询的时候需要session.beginTrasaction()来开启事务

  7. Python interview_python

    https://github.com/taizilongxu/interview_python 1 Python的函数参数传递 strings, tuples, 和numbers是不可更改的对象,而l ...

  8. classpath路径指什么

    一.classpath路径指什么 只知道把配置文件如:mybatis.xml.spring-web.xml.applicationContext.xml等放到src目录(就是存放代码.java文件的目 ...

  9. python中panda的dateframe

    1 函数介绍 #to_csv函数的语法#函数原型# to_csv(filepath,sep=",",index=TRUE,header=TRUE)#参数介绍: #(1)导出的文件路 ...

  10. 剑指offer面试题:输入某二叉树的前序遍历和中序遍历,输出后序遍历

    二叉树的先序,中序,后序如何遍历,不在此多说了.直接看题目描述吧(题目摘自九度oj剑指offer面试题6): 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结 ...