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 ...
随机推荐
- iframe实用操作
iframe高度设置为子页面高度 //需要使用Jquery $(document).ready(function () { parent.document.getEleme ...
- Python_简单三级菜单制作
一:制作要求 1.三级菜单 2.可依次选择进入各子菜单 3.所需新知识点:字典,列表 *本文通过三种方法完成,第一种:只使用循环,第二种:使用列表,第三种:使用字典 二:FlowChart流程图 与上 ...
- Java实现Base64加密
阅读本文约“2.5分钟” Java加解密系列,介绍Java加密解密的基础知识,并使用Base64算法实现加解密. 对于加密在企业中是非常常见的,就如邮件的传输,每个企业都会有自己设置安全方式,设置加密 ...
- webpack4 系列教程(十二):处理第三方JavaScript库
教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步<webpack4 系列教程(十二):处理第三方 JavaScript 库>原文地址.或者来我的小站看更多内容:godbm ...
- HTML 请求头,响应头和 HTTP状态码
请求头 选项 说明 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 告诉服务器,当前客户端可以接收的文档类型 ...
- css3 简易时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 2018-12-14 JavaScript实现ZLOGO: 前进方向和速度
系列前文: JavaScript实现ZLOGO子集: 前进+转向 JavaScript实现ZLOGO子集: 单层循环功能 JavaScript实现ZLOGO子集: 测试用例 JavaScript实现Z ...
- Tars 服务调服务
1,创建一个 tars 服务工程 2,把需要调用的服务的 客户端接口文件 拷贝到当前服务 3,构建通信器 CommunicatorConfig 调用,如果是 springboot 开发客户端可以使用注 ...
- iOS---------如何搭建ipv6环境
第一步:首先打开共享 第二步:点击互联网共享,然后按option键.会出现创建NAT64网络 第三步:点击Wi-Fi共享,设置网络名称,频段:11.安全性:WPA2个人级.密码设置8位就可以了.然后在 ...
- Android为TV端助力 http下载视频到指定目录
public void httpget(String uri){ HttpURLConnection connection = null; FileOutputStream fos = null; F ...