欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1872B.The Corridor or There and Back Again
The Corridor or There and Back Again
You are in a corridor that extends infinitely to the right, divided into square rooms. You start in room , proceed to room , and then return to room . You can choose the value of . Moving to an adjacent room takes second.
Additionally, there are traps in the corridor: the -th trap is located in room and will be activated seconds after you enter the room . Once a trap is activated, you cannot enter or exit a room with that trap.
A schematic representation of a possible corridor and your path to room and back.
Determine the maximum value of that allows you to travel from room to room and then return to room safely.
For instance, if and , you can proceed to room and return safely (the trap will activate at the moment , it can't prevent you to return back). But if you attempt to reach room , the trap will activate at the moment , preventing your return (you would attempt to enter room on your way back at second , but the activated trap would block you). Any larger value for is also not feasible. Thus, the answer is .
Input
The first line of the input contains an integer () — the number of test cases.
The descriptions of the test cases follow.
The first line of each test case description contains an integer () — the number of traps.
The following lines of each test case description present two integers and () — the parameters of a trap (you must leave room strictly before seconds have passed since entering this room). It's possible for multiple traps to occupy a single room (the values of can be repeated).
Output
For each test case, print the maximum value of that allows you to travel to room and return to room without encountering an active trap.
Note
The first test case is explained in the problem statement above.
In the second test case, the second trap prevents you from achieving . If , the second trap will activate at the moment (the time you enter room plus ). In the case of , you will return to room at time or later. The trap will be active at that time. It can be shown that room can be reached without encountering an active trap.
In the third test case, you can make it to room and then immediately return to room .
Samples
7
1
2 2
3
2 8
4 3
5 2
1
200 200
4
1 20
5 9
3 179
100 1
2
10 1
1 18
2
1 1
1 2
3
1 3
1 1
1 3
2
5
299
9
9
1
1
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |