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 ...
随机推荐
- Codeforces Round #402 (Div. 2) D String Game —— 二分法
D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- hdu1015 Safecracker —— 回溯
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1015 代码1: #include<stdio.h>//hdu1015 #include&l ...
- servlet从jsp到jsp实现例子
已登录页面为例子: 1.login.jsp <%@ page language="java" contentType="text/html; charset=UTF ...
- laravel基础课程---3、路由(Laravel中的常见路由有哪几种)
laravel基础课程---3.路由(Laravel中的常见路由有哪几种) 一.总结 一句话总结: 6种:post,get,put,patch,delete,options Route::get($u ...
- 随应潮流-基于ABP+Angularjs现代化应用软件开发框架(1)-总体介绍
系列文章目录 随应潮流-基于ABP+Angulsrjs现代化应用软件开发框架(1)-总体说明 随应潮流-基于ABP+Angulsrjs现代化应用软件开发框架(2)-abp说明 随应潮流-基于ABP+A ...
- codevs 1048石子归并
传送门 1048 石子归并 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], ...
- js some和filter用法和区别
some方法 array1.some(callbackfn[, thisArg]) 对数组array1中的每个元素调用回调函数callbackfn,当回调函数返回true或者遍历完所有数组后,some ...
- SSIS 事务
本文摘自:http://www.cnblogs.com/tylerdonet/archive/2011/09/23/2186579.html 在这一个随笔中将介绍在package中如何使用事务来保证数 ...
- java集合框架之HashMap和Hashtable的区别
参考http://how2j.cn/k/collection/collection-hashmap-vs-hashtable/692.html#nowhere HashMap和Hashtable的区别 ...
- PHP实用小程序(四)
<HTML> <HEAD> <TITLE>访问文件时间属性</TITLE> </HEAD> <BODY> <? $Last ...