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;
}
Codeforces Round #542(Div. 2) B.Two Cakes的更多相关文章
- Codeforces Round 542 (Div. 2)
layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #542(Div. 2) CDE 思维场
C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...
- Codeforces Round #542(Div. 2) A.Be Positive
链接:https://codeforces.com/contest/1130/problem/A 题意: 给n个数,找出一个非0整数d,使所有n个数除以整数d后,数组中正数的数量>= n/2. ...
- Codeforces Round #542(Div. 2) D1.Toy Train
链接:https://codeforces.com/contest/1130/problem/D1 题意: 给n个车站练成圈,给m个糖果,在车站上,要被运往某个位置,每到一个车站只能装一个糖果. 求从 ...
- Codeforces Round #542(Div. 2) C.Connect
链接:https://codeforces.com/contest/1130/problem/C 题意: 给一个n*n的图,0表示地面,1表示水,给出起点和终点, 现要从起点到达终点,有一次在两个坐标 ...
- Codeforces Round #542 (Div. 1) 题解
开学了住校了打不了深夜场 好难受啊QwQ A 显然对于每个起点,我们只需要贪心记录这个起点出发出去的糖果数量以及离自己最近的糖果 因为这个起点最后一次装载糖果一定是装载终点离自己最近的那个糖果 $ O ...
- Codeforces Round #542 Div. 1
A:显然对于起点相同的糖果,应该按终点距离从大到小运.排个序对每个起点取max即可.读题花了一年还wa一发,自闭了. #include<iostream> #include<cstd ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解
Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...
- 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 ...
随机推荐
- linux EXT文件系统
将一个硬盘分区之后如何创建文件系统(windows来讲就是如何针对分区来进行格式化,是采用FAT32的文件系统来格式化,还是采用NTFS的文件系统来格式化).Linux主要采用EXT2,EXT3分区格 ...
- 原来 Set 集合也可以排序
Java 集合类主要由两个接口派生而出: Collection 和 Map.在 Collection 集合中,我们经常用到的是 List 集合和 Map 集合,而 Set 集合出场的机会就相对比较的少 ...
- 一篇文章看懂iOS代码块Block
block.png iOS代码块Block 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返 ...
- 脚踏实地学C#5-扩展方法
扩展方法(Extension Method) MSDN定义:能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法须知: 1.扩展方法声明所在的类必须被声明为 ...
- android:textAppearance
Android之系统自带的文字外观设置及实际显示效果图 android:textAppearancexml布局里面设置文字的外观: 如“android:textAppearance=“?android ...
- 让Spinner中的文字居中
如果套用simple_spinner_item或是simple_spinner_dropdown_item,然后直接在Spinner中用 android:gravity="center&qu ...
- [Balkan 2007] Mokia
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1176 [算法] CDQ分治 + 树状数组即可 时间复杂度 : O(Nlog^2N) ...
- bzoj 4711 小奇挖矿 ——“承诺”类树形dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4711 对“承诺”有了更深的了解. 向外和向内要区分,所以 f [ i ][ j ] 表示根向 ...
- CF 1009 F Dominant Indices —— 长链剖分+指针
题目:http://codeforces.com/contest/1009/problem/F 也可以用 dsu on tree 的做法,全局记录一个 dep,然后放进堆里,因为字典序要最小,所以再记 ...
- 【原】putty配置下载
文章出自:http://www.cnblogs.com/david-zhang-index/p/3205354.html putty配置下载,已经配置好了,颜色非常不错 PUTTY.zip