Codeforces Round #542 B Two Cakes
1 second
256 megabytes
standard input
standard output
Sasha and Dima want to buy two nn -tier cakes. Each cake should consist of nn different tiers: from the size of 11 to the size of nn . Tiers should go in order from the smallest to the biggest (from top to bottom).
They live on the same street, there are 2⋅n2⋅n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the ii -th house you can buy a tier of the size aiai (1≤ai≤n1≤ai≤n ).
Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 11 , then 22 , then 33 and so on up to nn .
Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 11 .
The first line of the input contains an integer number nn — the number of tiers in each cake (1≤n≤1051≤n≤105 ).
The second line contains 2⋅n2⋅n integers a1,a2,…,a2na1,a2,…,a2n (1≤ai≤n1≤ai≤n ), where aiai is equal to the size of the tier, which can be bought in the ii -th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 11 to nn occurs in aa exactly two times.
Print one number — the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy nn tiers in ascending order of their sizes.
3
1 1 2 2 3 3
9
2
2 1 1 2
5
4
4 1 3 2 2 3 1 4
17
In the first example, the possible optimal sequence of actions is:
- Sasha buys a tier of size 11 near the 11 -st house (a1=1a1=1 );
- Dima goes to the house 22 ;
- Dima buys a tier of size 11 near the 22 -nd house (a2=1a2=1 );
- Sasha goes to the house 44 ;
- Sasha buys a tier of size 22 near the 44 -th house (a4=2a4=2 );
- Sasha goes to the house 55 ;
- Sasha buys a tier of size 33 near the 55 -th house (a5=3a5=3 );
- Dima goes to the house 33 ;
- Dima buys a tier of size 22 near the 33 -rd house (a3=2a3=2 );
- Dima goes to the house 66 ;
- Dima buys a tier of size 33 near the 66 -th house (a6=3a6=3 ).
So, Sasha goes the distance 3+1=43+1=4 , and Dima goes the distance 1+1+3=51+1+3=5 . In total, they cover a distance of 4+5=94+5=9 . You can make sure that with any other sequence of actions they will walk no less distance.
这是一个贪心题目,隐隐约约意识到了,但是我贪心并没有学好,所以自己没有写对,然后看了别人的代码,觉得写的真好。
这个我之前认为每一个数字的处理,会有后效性,也就是会对后面的结果产生影响,所以就写的畏畏缩缩的,然后就错了,而且我很喜欢用结构体,所以就用结构体来储存所有的数,这个不太好。
然后就是要消去后效性,那就用贪心,对待每一层,从1开始贪心,具体看代码,这里其实没有后效性。
然后再贴另一个代码,我不知道为什么AC了的,别人写的。
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int l[maxn], r[maxn];
int a[maxn]; int main()
{
int n;
cin >> n;
for(int i=1;i<=2*n;i++)
{
scanf("%d", &a[i]);
if (!l[a[i]]) l[a[i]] = i;
else r[a[i]] = i;
}
ll ans=l[1]-1+r[1]-1;
for(int i=1;i<n;i++)
{
ans += min(abs(l[i + 1] - l[i]) + abs(r[i + 1] - r[i]), abs(l[i + 1] - r[i]) + abs(l[i] - r[i + 1]));
}
printf("%I64d\n", ans);
return 0;
}
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int l[maxn], r[maxn];
int a[maxn]; int main()
{
int n;
cin >> n;
for(int i=1;i<=2*n;i++)
{
scanf("%d", &a[i]);
if (!l[a[i]]) l[a[i]] = i;
else r[a[i]] = i;
}
ll ans = 0;
int L = 1, R = 1;
for(int i=1;i<=n;i++)
{
ans += abs(l[i] - L);
ans += abs(r[i] - R);
L = l[i];
R = r[i];
}
printf("%I64d\n", ans);
return 0;
}
Codeforces Round #542 B Two Cakes的更多相关文章
- 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 #542 题解
Codeforces Round #542 abstract I决策中的独立性, II联通块染色板子 IIIVoronoi diagram O(N^2 logN) VI环上距离分类讨论加取模,最值中的 ...
- Codeforces Round 542 (Div. 2)
layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #542(Div. 2) B.Two Cakes
链接:https://codeforces.com/contest/1130/problem/B 题意: 给定n和 2 * n个数,表示i位置卖ai层蛋糕, 有两个人在1号,必须严格按照1-n的顺序买 ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) A - D2
A. Be Positive 链接:http://codeforces.com/contest/1130/problem/A 题意: 给一段序列,这段序列每个数都除一个d(−1e3≤d≤1e3)除完后 ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)
A. Be Positive 题意:给出一个数组 每个树去除以d(d!=0)使得数组中大于0的数 大于ceil(n/2) 求任意d 思路:数据小 直接暴力就完事了 #include<bits/s ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1) C(二分+KMP)
http://codeforces.com/contest/1129/problem/C #include<bits/stdc++.h> #define fi first #define ...
- Codeforces Round #542(Div. 2) CDE 思维场
C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...
- Codeforces Round #413 A. Carrot Cakes
A. Carrot Cakes time limit per test 1 second memory limit per test 256 megabytes In some game ...
随机推荐
- BurpSuiteProxy安装使用
第一步先安装jdk https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html ...
- ASP.NET MVC 学习笔记-2.Razor语法
1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“:”结尾.代码块中定义的变量可能会被同一个域中的其他块使用. ...
- Java基础IO流(五)RandomAccessFile
RandomAccessFile java提供的对文件内容的访问,既可以读文件也可以写文件.RandomAccessFile支持随机访问文件,可以访问文件的任意位置 (1)java文件模型: 在 ...
- nginx比apache处理静态文件速度快,但是nginx处理大量并发的php请求时,容易出现502错误,频率大概是多少
首先要明确一点的是502是怎么出现的,为什么会出现502呢? 一般而言,出现502的错误是因为php-cgi连接数不够导致的.举个例子:php-cgi开10个进程,前端发20个请求,每个请求的脚本都s ...
- ASPxGridView 添加勾选列--全选 和 后端获取勾的行ID
一.HTML 代码 <table style="width: 100%;"> <tr> <td> <asp:Button ID=" ...
- Git命令使用小结
一.上传你的代码的基本方式 0.在github网站上登录你的账户cynthiawupore,然后新建一个仓库demo 1.初始化 $ git init 2.添加文件夹下所有文件到仓库 $ git ad ...
- 洛谷P4103 [HEOI2014]大工程(虚树 树形dp)
题意 链接 Sol 虚树. 首先建出虚树,然后直接树形dp就行了. 最大最小值直接维护子树内到该节点的最大值,然后合并两棵子树的时候更新一下答案. 任意两点的路径和可以考虑每条边两边的贡献,\(d[x ...
- Suricata规则编写——常用关键字
本篇转载自:http://blog.csdn.net/wuyangbotianshi/article/details/44775181 1.简介 现在的NIDS领域snort一枝独秀,而suricat ...
- linux连接数过多,导致ping包丢包的问题解析
1.首先要明确,无论是tcp, udp, raw等这些都要占用socket, 那么就涉及到连接数的问题. 所以,linux连接数的问题,不仅仅是tcp连接数. 2.查看当前系统中所有的socket 连 ...
- phpcms中content主要使用的详情列表关系
从首页(index.html)中点开的内容网页叫单网页(page.html) 从列表(list.html)中点开的网页叫内容页(show.html) 从导航栏的栏目中下拉的列表栏目叫栏目列表页(cat ...