CodeForces 689B - Mike and Shortcuts

HDRP shader 获取阴影(Custom Pass) Unity:2021.3.0f1 HDRP:12.1.6 阅读详情

题目

B. Mike and Shortcuts
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city.

City consists of n intersections numbered from 1 to n. Mike starts walking from his house located at the intersection number 1 and goes along some sequence of intersections. Walking from intersection number i to intersection j requires |i - j| units of energy. The total energy spent by Mike to visit a sequence of intersections p1 = 1, p2, ..., pk is equal to units of energy.

Of course, walking would be boring if there were no shortcuts. A shortcut is a special path that allows Mike walking from one intersection to another requiring only 1 unit of energy. There are exactly n shortcuts in Mike's city, the ith of them allows walking from intersection i to intersection ai (i ≤ ai ≤ ai + 1) (but not in the opposite direction), thus there is exactly one shortcut starting at each intersection. Formally, if Mike chooses a sequence p1 = 1, p2, ..., pk then for each 1 ≤ i < k satisfying pi + 1 = api and api ≠ pi Mike will spend only 1 unit of energy instead of |pi - pi + 1| walking from the intersection pi to intersection pi + 1. For example, if Mike chooses a sequence p1 = 1, p2 = ap1, p3 = ap2, ..., pk = apk - 1, he spends exactly k - 1 units of total energy walking around them.

Before going on his adventure, Mike asks you to find the minimum amount of energy required to reach each of the intersections from his home. Formally, for each 1 ≤ i ≤ n Mike is interested in finding minimum possible total energy of some sequence p1 = 1, p2, ..., pk = i.

Input

The first line contains an integer n (1 ≤ n ≤ 200 000) — the number of Mike's city intersection.

The second line contains n integers a1, a2, ..., an (i ≤ ai ≤ n , , describing shortcuts of Mike's city, allowing to walk from intersection i to intersection ai using only 1 unit of energy. Please note that the shortcuts don't allow walking in opposite directions (from ai to i).

Output

In the only line print n integers m1, m2, ..., mn, where mi denotes the least amount of total energy required to walk from intersection 1 to intersection i.

Examples
Input
3
2 2 3
Output
0 1 2 
Input
5
1 2 3 4 5
Output
0 1 2 3 4 
Input
7
4 4 4 4 7 7 7
Output
0 1 2 1 2 3 3 
Note

In the first sample case desired sequences are:

1: 1; m1 = 0;

2: 1, 2; m2 = 1;

3: 1, 3; m3 = |3 - 1| = 2.

In the second sample case the sequence for any intersection 1 < i is always 1, i and mi = |1 - i|.

In the third sample case — consider the following intersection sequences:

1: 1; m1 = 0;

2: 1, 2; m2 = |2 - 1| = 1;

3: 1, 4, 3; m3 = 1 + |4 - 3| = 2;

4: 1, 4; m4 = 1;

5: 1, 4, 5; m5 = 1 + |4 - 5| = 2;

6: 1, 4, 6; m6 = 1 + |4 - 6| = 3;

7: 1, 4, 5, 7; m7 = 1 + |4 - 5| + 1 = 3.

题意

n个点标号为1到n,每个点都有一条有向边连向某个点,边权为1,各个点之间都有一条无向边相连,权值为标号的差值,问1号点到其他点的最短路。

解法

虽然边很多,但并不是每条边都会用到,只需保留权值为1的边,怎样就只有40w条边,用spfa或dijkstra都可以。

代码

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 200005
struct edge
{
	int to,next,cost;
}e[N*4];
struct node
{
	int id,dis;
	friend bool operator <(node a,node b)
	{
		return a.dis>b.dis;
	}
};
priority_queue <node> q;
int head[N],cnt,dis[N],vis[N];
void addedge(int u,int v,int c)
{
	e[cnt].cost=c;
	e[cnt].to=v;
	e[cnt].next=head[u];
	head[u]=cnt++;
}
int main()
{
	int n,i,x;
	scanf("%d",&n);
	memset(head,-1,sizeof(head));
	cnt=0;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&x);
		if(x!=i)
			addedge(i,x,1);
		if(i!=n)
		{
			addedge(i,i+1,1);
			addedge(i+1,i,1);
		}
	}
	dis[1]=0;
	node now,tmp;
	tmp.id=1;
	tmp.dis=0;
	q.push(tmp);
	int num=0;
	while(!q.empty())
	{
		now=q.top();
		q.pop();
		if(vis[now.id])
			continue;
		vis[now.id]=1;
		num++;
		if(num==n)
			break;
		for(i=head[now.id];~i;i=e[i].next)
		{
			int v=e[i].to;
			if(vis[v])
				continue;
			if(dis[v]==0||dis[now.id]+e[i].cost<dis[v])
			{
				dis[v]=dis[now.id]+e[i].cost;
				tmp.id=v;
				tmp.dis=dis[v];
				q.push(tmp);
			}
		}
	}
	for(i=1;i<=n;i++)
		printf("%d ",dis[i]);
	puts("");
	return 0;
}


基于 STM32 与压电薄膜的婴儿睡眠呼吸暂停监测床垫设计 本设计以 STM32F103C8T6 为主控,结合 PVDF 压电薄膜传感器,通过模拟信号调理与数字峰值检测算法,实现了对婴儿睡眠呼吸状态的实时监测。系统结构简单、成本低廉、无接触无束缚,当检测到 20 秒以上无呼吸信号时自动触发声光报警,可有效降低婴儿睡眠呼吸暂停带来的安全风险。后续可扩展蓝牙 / WiFi 模块,将报警信息推送至家长手机端,实现远程监护。 阅读详情

相关推荐

jason-2卫星介绍及其数据下载

两台仪器发射脉冲宽度均为105.6μs,带宽320MHz(C频段)和320/100MHz(Ku频段),脉冲重复频率1800Hz(Ku频段)和300Hz(C频段),功率78W,数据传输速率22.5kbit/s。共发射了3颗卫星(Jason - 1/2/3),星上有效载荷均主要为雷达高度计和微波辐射计。Jason - 1卫星主要有效载荷为波塞冬 - 2和“贾森微波辐射计”,Jason - 2卫星进行了改进,主要携带了波塞冬 - 3和先进微波辐射计,此外,Jason - 2卫星还携带了“多里斯”系统。

beautyaaaa的博客 1605

Mike and Shortcuts【Codeforces 689 B】【最短路】

Codeforces Round #361 (Div. 2) B 题意:有N个十字路口,我们是从十字路口1号出发,问到1~N这几号十字路口的最短举例,然后,对于每个十字路口,都有一个近路可以走,路径长度都是1,并且是单向边。最后输出从1出发到1~N这些点的最短举例。 思路:我们可以直接建边,但是别忘了考虑,我们可以从i点回到i-1这个点的可能性,因为有可能回溯回来再走是更优的解。 ...

既然弱小,就只顾变强就是了 239

【气候极端指数】MATLAB计算各种气候极端指数

简介各类气候极端指数,及其MATLAB计算代码

WW、forever的博客 4917

CodeForces 689B Mike and Shortcuts(SPFA)

题目链接:http://codeforces.com/problemset/problem/689/B 题意:设从i到j需要 |i-j| 点体力,从i有一条捷径到Ai,只需1点体力。输入A1-An,输出从1到i的最少花费。 思路:1->3若没有捷径则相当于1->2->3,所以对于i点只需要建立三条边。i -> Ai、i -> i-1、i -> i+1,然后用SPFA求最短路即可。

JOY 酷酷的专栏 302

从 30 天试用到企业级落地,2026 年学习和试用 SAP AI 的完整路线图

摘要:SAP AI产品体系存在试用版本与商业版本的边界模糊问题,需区分业务嵌入AI(如Joule)、开发者工具(SAP Build Code)和AI基础平台(Generative AI Hub)三个层级。建议学习路径:1)通过SAP Learning专区掌握理论框架;2)优先使用30天Generative AI Hub基础试用体验模型编排;3)结合90天BTP试用账户开发完整应用。特别注意免费计划已陆续停止,需以最新官方文档为准。开发者应重点关注AI如何与SAP技术栈(CAP、Fiori等)集成,而非单纯功

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 112

【全局路径规划模块全覆盖算法】(三)代码实现:Boustrophedon Cellular Decomposition Path Planning用珊格地图生成每个cell的覆盖路径

认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长!本文先对Boustrophedon Cellular Decomposition Path Planning用珊格地图生成每个cell的覆盖路径做个简单的介绍,具体内容后续再更,其他模块可以参考去我其他文章提示:以下是本篇文章正文内容。

qq_35635374的博客 1832

【CodeForces - 689B】Mike and Shortcuts(Dijkstra最短路,或者bfs跑状态类似spfa)

题干: Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city. City consists of n intersections numbered from 1 to n. M...

xuanweiace的博客 708

SpringBoot 教程:SpringBoot多种方式实现静态Html页面跳转

本教程介绍了 Spring Boot 中实现静态 Html 页面跳转的常用方法,包括重定向跳转、直接返回 ModelAndView 对象以及使用 Thymeleaf 模板引擎。如果你的项目中使用了 Thymeleaf 模板引擎,那么可以利用它来实现静态页面的跳转。对于静态的 Html 页面,我们可以使用多种方式来实现跳转功能。重定向是一种常见的页面跳转方式,它通过发送 HTTP 重定向响应头来告知浏览器跳转到指定的页面。在 Spring Boot 中,我们可以将视图名称设置为静态页面的路径。

2301_79325657的博客 2448

Mike and Shortcuts (CF bfs)

问题描述: B. Mike and Shortcuts time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Recently, Mike was very busy with studying for exams and cont

ww的博客 365

python实现Markowitz模型 (附完整源码)

python实现Markowitz模型 (附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 402

漏检率从23%降至1.3%:YOLOv8交通车辆计数与违章识别完整落地方案

本文分享了基于YOLOv8+ByteTrack的交通视频分析系统实战经验,针对车辆计数和违章识别两大核心功能,提出了完整的工程化解决方案。系统采用边缘计算架构,单张GTX 1660即可稳定处理8路1080P视频流,实现了白天98.7%、夜间96%以上的车辆计数准确率。文章详细剖析了交通视频分析的四大落地痛点,系统架构设计思路,以及遮挡处理、光照适应、多路并发等关键技术优化方案,重点介绍了车辆计数三重校验机制和三类违章识别的工程实现细节。通过ROI裁剪、TensorRT加速、动态抽帧等优化手段,显著提升了系统

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 340

第二次训练赛 B. Mike and Shortcuts-bfs

题目传送门:https://codeforces.com/problemset/problem/689/B 题目大意:有n个城市在一排,两两间距为1,每个城市可以用1的距离到一个给定的城市,问城市1到其他所有城市的最短距离。 思路:bfs。 比赛时脑抽写了个dijkstra,还wa了。。。 bfs的时候一定要搜到一个标记一个。。。 代码: #include &lt;bits/stdc++.h&gt...

weixin_30149299的博客 235

MFC Windows 程序设计[105]之彩色滚动条(附源码)

MFC是微软公司提供的一个类库(class libraries),用以减少应用程序开发人员的工作量。其中包含大量Windows句柄封装类和很多Windows的内建控件和组件的封装类。有很多初学MFC的小伙伴们都觉得MFC比较难学,看书吧,看了就忘了,网上的一些教程又是写的含糊不清,故而学习起来很吃力...... 故此,本专栏亲自研究,实践,由易到难,层层深入,将调试过程中所遭所遇进行详细讲解,注意事项进行一一列举,希望能够帮助到各位初学MFC的小伙伴,避免走弯路,费时费力。

hnjzfwy的博客 518

CodeForces 689B BFS-Mike and Shortcuts

DescriptionRecently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city.City consists of n intersections numbered from 1 to n

geekword的专栏 432

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Codeforces 689B. Mike and Shortcuts(BFS)

题目链接简单题意有n个路口,从i到j路口之间的消耗是|i-j|,同时路口可以直接以1的耗费到达另外一个指定的路口。问从1到n的最小消耗。思路直接BFS搜图就可以了,每个点到他上一个点,下一个点,捷径直达的点建权为1的边。然后直接BFS搜即可代码#include <cstdio> #include <algorithm> #include <queue>using namespace std; int

我为刀俎_题为鱼肉 557

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

(CodeForces - 689B)Mike and Shortcuts

(CodeForces - 689B)Mike and Shortcuts Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city. City consists of n intersections

The wind of freedom blows 508

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

CodeForces - 689B Mike and Shortcuts (BFS)

Mike and ShortcutsRecently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city. City consists of n intersections numbered fr

WwyY9的博客 570

CodeForces 689B Mike and Shortcuts

B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Recently, Mike was very busy with stu

Calm微笑 572

codeforces 689B Mike and Shortcuts (bfs)

B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Recently, Mike was very busy with studying for

nare123的博客 419

Codeforces 689B - Mike and Shortcuts

题意:求从1开始的到所有点的最短路。 分析:设图中的两个点u,v, 则dis[u][v] = abs(u-v) 或 dis[u][v] = 1。 其实换个角度想就可以发现每次运动消耗的能量都是1。由于每一单位时间运动消耗的能量都是1。 状态的转移有三个:1、向前走一步。2、先后走一步。3、走捷径。 /*********************...

bannesi6888的博客 129

Mike and Shortcuts CodeForces - 689B BFS

Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city.City consists of n intersections numbered from 1 to n. Mike star

青铜时代 284

CodeForces 689B Mike and Shortcuts spfa最短路

CF我做的不是很多,但最好的一点是官网上可以看到评测点,方便找问题。这道题一看就是最短路,因为n有些大,dijkstra可能会T,就用了spfa(不用判负环)。一开始写的不是TLE就是MLE,怎么都想不通哪里有问题,后来看了大佬博客Orzhttp://www.21ic.com/tougao/article/13224.html,猛然发现边加多了。如果不算捷径,除1以外的其他节点直接到节点1的距离和...

A confused programmer 460

CodeForces 689B Mike and Shortcuts (bfs or 最短路)

Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by do...

dituonian8372的博客 119

【打CF,学算法——三星级】CodeForces 689B Mike and Shortcuts (最短路+spfa)

题目链接:CF 689B 题面: B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Recently, Mike

David_Jett 1961

CodeForces 689 B. Mike and Shortcuts(SPFA)

Description n个点,任意两点距离为其编号差的绝对值,同时有n条捷径,i到a[i]之间距离为1,问点1到点i的最短距离 Input 第一行一整数n表示点数,之后n个整数a[i] (1<=n<=200000,i<=a[i]<=n,a[i]<=a[i+1]) Output 输出n个数,第i个数表示点1到点i的最短距离 Sample Input 3 2 2 3 Sample O

ZSQ 551

Codeforces 689B Mike and Shortcuts DFS or BFS

B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Recently, Mike was very busy with stu

你那么爱笑,家里人知道吗? 414
上一篇: CodeForces 689D-Friends and Subsequences
下一篇: CodeForces 455C-Civilization
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值