当前位置:网站首页>Zcmu--5066: dark corridor
Zcmu--5066: dark corridor
2022-07-28 21:02:00 【Little why】
Description
Late at night , Small Q Walking on a dark corridor . The corridor can be regarded as a number axis . There are n Lights , From left to right
The number is 1 To n, The first i The coordinates of the lamp are xi, It can illuminate the number axis [xi − ri, xi + ri] Part of ( Including both sides ). If one
The position is not illuminated by any lights , So afraid of the dark little Q I dare not go there .
Small Q You can go left or right on the corridor . If his position coincides with a light , He can choose to turn it on or off
The lamp . In limine , Small Q The position of is the same as that of 1 Lamp coincidence , The first 1 A light is on , All other lights are off . Small
Q I hope to walk to the... Under the light n Location of lights , And make the n A light is on , All other lights are off .
Please write a program , Help small Q Find the shortest route , Or tell him it's impossible .
Input
The first line contains a positive integer T(1 ≤ T ≤ 2000), Number of groups representing test data .
The first row of each set of data contains a positive integer n(2 ≤ n ≤ 100000), Indicates the number of lights .
Next n That's ok , Two positive integers per line xi, ri, Indicate the position and light radius of each lamp respectively , among
1 ≤ x1 < x2 < x3 < ::: < xn ≤ 109,1 ≤ ri ≤ 109.
Input data guarantee Σn ≤ 300000.
Output
For each group of data, output a line and an integer , That is, the shortest total distance , If there is no solution, please output “-1”.
Sample Input
4
5
1 5
3 1
4 9
7 8
8 4
2
1 1
10 10
2
1 10
10 8
2
1 1000000000
1000000000 999999999
Sample Output
21
-1
-1
2999999997
analysis : Through the example, we can find “ As if ” The shortest path is ( End - The starting point )*3, Because if we can get to the next point , There must be a light in front , If we go to a point every time and turn off the front immediately , It must be a long way to go back and forth , It should be the last time to go back and turn off all the other lights twice .
Conclusion : If we can reach the end and go back to the starting point , So the answer is ( End - The starting point )*3, Otherwise, it would be -1.
#include <stdio.h>
struct su
{
long long x,r; // Save the coordinates and lighting radius of each lamp
}a[100005];
int main()
{
long long t,m,s,n,i;
scanf("%lld",&t);
while(t--){
s=1; //s To record whether we can reach the end or the starting point
scanf("%lld%lld%lld",&n,&a[0].x,&a[0].r);
m=a[0].x+a[0].r; // The farthest place you can reach at the beginning
for(i=1;i<n;i++){ // Judge whether we can reach the end !!!!!!!!!!!
scanf("%lld%lld",&a[i].x,&a[i].r);
if(s==0) continue; // If you can't reach the next light , I.e. unable to reach
if(a[i].x<=m&&a[i].x+a[i].r>m) m=a[i].x+a[i].r; // Update the farthest place
else if(a[i].x>m) s=0; // Can't get to the next point
}
if(s==0){
printf("-1\n");
continue;
}
m=a[n-1].x-a[n-1].r; // Go back to the left , Is the cut radius , Other same as above
for(i=n-2;i>=0;i--){ // Judge whether you can return to the starting point !!!!!!!!!!!
if(s==0) continue;
if(a[i].x>=m&&a[i].x-a[i].r<m) m=a[i].x-a[i].r;
else if(a[i].x<m) s=0;
}
if(s==0) printf("-1\n");
else printf("%lld\n",(a[n-1].x-a[0].x)*3);
}
}边栏推荐
- C # basic 5-asynchronous
- High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
- [工具类] Map的util包, 常用 实体类转化为map等操作
- Unity foundation 2 editor expansion
- How to modify the ID of NetApp expansion enclosure disk shelf
- Space shooting Lesson 11: sound and music
- How can enterprises successfully complete cloud migration?
- Explain mesh Collider in unity
- Yyds dry inventory interview must brush top101: every k nodes in the linked list are turned over
- 全链路灰度在数据库上我们是怎么做的?
猜你喜欢

融合数据库生态:利用 EventBridge 构建 CDC 应用

JS drag and drop alert pop-up plug-in

阿里云 MSE 支持 Go 语言流量防护

NAT实验演示(Huawei交换机设备配置)

Explain the camera in unity and its application

Lvs+keepalived high availability deployment practical application
![Leetcode:2141. The longest time to run n computers at the same time [the maximum value is two points]](/img/33/05620dfff2f6ac67691d20c5256c5b.png)
Leetcode:2141. The longest time to run n computers at the same time [the maximum value is two points]

Integrating database Ecology: using eventbridge to build CDC applications

Redis 3.0 source code analysis - data structure and object SDS list Dict

作业 ce
随机推荐
Pl515 SOT23-5 single / Dual Port USB charging protocol port controller Parkson electronic agent
记一次Runtime.getRuntime().exce(“command“)报错
The 678th operation
取色器实战(Qt含源码)
Explain various coordinate systems in unity in detail
Nat experiment demonstration (Huawei switch equipment configuration)
How to turn on or off the disk LED of EMC Vmax
融合数据库生态:利用 EventBridge 构建 CDC 应用
查询oracle视图创建语句及如何向视图中插入数据[通俗易懂]
Unity foundation 6-rotation
阿里云 MSE 支持 Go 语言流量防护
Ask if you don't understand, and quickly become an advanced player of container service!
Two written interview questions about regular
Hangao database best practice configuration tool Hg_ BP log collection content
C foundation 2-encapsulation, inheritance, polymorphism
Algorithm interview high frequency problem solving guide [1]
两款吾爱破解优秀软件,批量查找文本,图像视频画质增强
C # basic 5-asynchronous
什么是“安全感”?沃尔沃用它自己独特的理解以及行动来告诉你
The average altitude is 4000 meters! We built a cloud on the roof of the world