Villages: Landlines

问题描述

The game development team NIO Tech releases its debut game, Villages: Landlines. In this game, players develop their villages and enjoy their peaceful country life.
The game Villages: Landlines contains a one-dimensional power system. Initially, there are several power stations and buildings. The goal of the game is to make every power stations and buildings connected together in the power system, forming a connected graph. Players can build some power towers and wires in the game, which are the tools to connect power stations and buildings.
Power stations or buildings can connect to power towers without wires, while power towers must connect to each other by wires. What's more, power stations and buildings can't connect to each other directly, they must connect through power towers.
Assuming that the coordinate of a building is xi and its receiving radius is ri, all the power towers whose distance from the building is no greater than ri are directly connected to it without wires. That is to say, for the power tower located at x, it is directly connected to the building at xi without wires if and only if ∣x−xi∣≤ri. Similarly, assuming that the power supply radius of a power station is rs, all the power towers whose distance from the power station is no more than rs are directly connected to it without wires. Supposing that the coordinates of two power towers are xA and xB, players can connect them with the wire, and the length of wire required is ∣xA−xB. A power tower can be connected to any number (possibly zero) of power towers, buildings and power stations.
In order to make the game more friendly to the rookies, Nostalgia, a member of NIO Tech, decides to develop the power distribution recommendation function. In the case of using any number of power towers, players can choose exactly one power station and several buildings to get an optimal power supply scheme, which uses the shortest total length of wires to complete the power system. However, Nostalgia is not sure whether her scheme is correct, so she needs your help to calculate the total length of wires used in the optimal scheme to determine the correctness of her scheme.
Note that the players can build a new power tower at coordinate x even if there exists a power station or a building at x. There might be more than one power station or building at the same coordinate.

输入格式

The first line contains a single integer n (1≤n≤2×105).

The second line contains two integers xs,rs(−109≤xs≤109,1≤rs≤109) — the coordinate of the power station and its power supply radius.

The iii-th line of the next n1 lines contains two integers xi,ri (−109≤xi≤109,1≤ri≤109) — the coordinate of iii-th building and its receiving radius.

输出格式

Print an integer in a single line, denoting the total length of wires used in the optimal scheme.

样例1输入

5
0 1
0 3
5 1
6 1
9 2

样例1输出

1

样例2输入

2
-1000000000 1000000000
1000000000 1000000000

样例2输出

0

提示

For the first sample, one possible optimal scheme is building power towers at 1,3,4,6,7 and connect the power towers at 3 and 4 with the wire of length 1.

题解

在x轴上有一个发电站和若干建筑物,你可以在x轴上建一些电力塔,当电力塔在发电站或建筑物的信号范围内时,就视为发电站或建筑物和电力塔相连接,而电力塔之间需要通过电线来连接。

现在给出发电站和建筑物的坐标,你需要在坐标上建立若干个电力塔,使所有的发电站和建筑物相连通,求需要的最短的电线长度

以样例为例,我们可以用线段表示建筑物或发电站的信号范围

 

 

 

由于对电力塔的个数没有限制,我们可以假设每个整点坐标上都有一个电力塔

如上图中线段重合的部分的电力塔可以覆盖经过该点的所有线段

例如 x=0的电力塔可以覆盖第1、2条线段(即发电站和第1个建筑物)

   x=5的电力塔可以覆盖第3、4条线段(即第2、3个建筑物)

   x=7的电力塔可以覆盖第4、5条线段(即第3、4个建筑物)

如果把有线段覆盖的区域打上标记,那么显然有连续标记的区域内不需要额外连接电线(如-3~3,4~11)

所以需要电线的部分就是中间没有标记断层的部分(3~4)

于是问题转化为:

在x轴上一定范围内有一些线段,求没有被线段覆盖的长度

考虑对线段按左端点从小到大排序,从左到右扫描线段,若当前覆盖的最右端点(不一定是当前线段的右端点)在下一线段的左端点之前(即当前覆盖区域和下一线段没有重合),则将没有重合的这段用电线补上

 

 

 1 #include <algorithm>
 2 #include <cstdio>
 3 int n,x[200005],r[200005],ans;
 4 struct node{
 5     int l,r;
 6 }a[200005];
 7 bool cmp(node x,node y)
 8 {
 9     return x.l<y.l || (x.l==y.l && x.r<y.r);
10 }
11 int main()
12 {
13     int i,t;
14     scanf("%d",&n);
15     for (i=1;i<=n;i++)
16       scanf("%d%d",&x[i],&r[i]),
17       a[i].l=x[i]-r[i],
18       a[i].r=x[i]+r[i];
19     std::sort(a+1,a+n+1,cmp);
20     t=a[1].r;
21     for (i=2;i<=n;i++)
22     {
23         if (a[i].l>t)
24           ans+=(a[i].l-t);
25         if (a[i].r>t)
26           t=a[i].r;
27     }
28     printf("%d",ans);
29     return 0;
30 }

 

posted @ 2022-07-22 04:32  SAKURA12  阅读(28)  评论(0编辑  收藏  举报