A1138 | [COCI-2007_2008-olympiad]#3 TAMNICA
来源COCI
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
Brave Sir Robin has been thrown in the dungeon by the evil king. The dungeon consists of an infinite number of cube-shaped rooms with big stone walls. Rooms are connected by passages so that the entire dungeon, when viewed from above, looks like a spiral. The rooms are numbered as follows:
After a big earthquake some of the walls collapsed, and new passages were formed between adjacent rooms.
Sir Robin is initially in room
1. Sir Robin knows that the exit from the dungeon is located in room N, and wants to escape while everyone is distracted by the earthquake. Because the evil dragon is guarding the dungeon, Sir Robin wants to use the fastest way out of the dungeon.
Write a program that, given the location of the exit N and the list of new passages, determines the smallest number of passages that Sir Robin must go through before he can exit the dungeon.
After a big earthquake some of the walls collapsed, and new passages were formed between adjacent rooms.
Sir Robin is initially in room
1. Sir Robin knows that the exit from the dungeon is located in room N, and wants to escape while everyone is distracted by the earthquake. Because the evil dragon is guarding the dungeon, Sir Robin wants to use the fastest way out of the dungeon.
Write a program that, given the location of the exit N and the list of new passages, determines the smallest number of passages that Sir Robin must go through before he can exit the dungeon.
输入格式
The first line of input contains an integer N (1 ≤ N ≤ 10^15), the room in which the exit is located.
The second line of input contains an integer K (1 ≤ K ≤ 100 000), the number of new passages.
Each of the following K lines contains one integer B (4 ≤ B ≤ 10^15), meaning that a new passage now connects adjacent rooms A and B, where A<B. The number A is not given explicitly, but it can be uniquely determined from B (for example, if B is 20, then A must be 7). Also, some rooms can never be room B (rooms 2, 3, 5, 7, 10, 13 etc.).
The second line of input contains an integer K (1 ≤ K ≤ 100 000), the number of new passages.
Each of the following K lines contains one integer B (4 ≤ B ≤ 10^15), meaning that a new passage now connects adjacent rooms A and B, where A<B. The number A is not given explicitly, but it can be uniquely determined from B (for example, if B is 20, then A must be 7). Also, some rooms can never be room B (rooms 2, 3, 5, 7, 10, 13 etc.).
输出格式
Output should consist of a single integer, the smallest number of passages that Sir Robin must go through before he can exit the dungeon.
输入输出样例
输入 #1
31 9 15 25 30 6 9 19 24 27 4
输出 #1
6
输入 #2
10000 5 52 4 9 25 27
输出 #2
9953
In a number of test cases, worth a total of 50 points, N will be at most 10^6.
Clarification of first example. This is the layout of the dungeon after the earthquake:
Mirko can use the route 1-4-15-14-13-30-31, using only 6 hallways to exit the dungeon.
Clarification of first example. This is the layout of the dungeon after the earthquake:
Mirko can use the route 1-4-15-14-13-30-31, using only 6 hallways to exit the dungeon.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted