Villages: Landlines(区间排序)

 链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网

题目描述

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 xix_ixi​ and its receiving radius is rir_iri​, all the power towers whose distance from the building is no greater than rir_iri​ are directly connected to it without wires. That is to say, for the power tower located at xxx, it is directly connected to the building at xix_ixi​ without wires if and only if ∣x−xi∣≤ri|x-x_i|\leq r_i∣x−xi​∣≤ri​. Similarly, assuming that the power supply radius of a power station is rsr_srs​, all the power towers whose distance from the power station is no more than rsr_srs​ are directly connected to it without wires. Supposing that the coordinates of two power towers are xAx_AxA​ and xBx_BxB​, players can connect them with the wire, and the length of wire required is ∣xA−xB∣|x_A-x_B|∣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 xxx even if there exists a power station or a building at xxx. There might be more than one power station or building at the same coordinate.

输入描述:

The first line contains a single integer nnn (1≤n≤2×1051\leq n\leq 2\times 10^51≤n≤2×105).

The second line contains two integers xs,rsx_s, r_sxs​,rs​ (−109≤xs≤109,1≤rs≤109-10^9 \leq x_s \leq 10^9, 1\leq r_s\leq 10^9−109≤xs​≤109,1≤rs​≤109) — the coordinate of the power station and its power supply radius.


The iii-th line of the next n−1n-1n−1 lines contains two integers xi,rix_i, r_ixi​,ri​ (−109≤xi≤109,1≤ri≤109-10^9 \leq x_i \leq 10^9, 1\leq r_i\leq 10^9−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

5
0 1
0 3
5 1
6 1
9 2

输出

复制

  1

1

示例2

输入

复制

2 -1000000000 1000000000 1000000000 1000000000

2
-1000000000 1000000000
1000000000 1000000000

输出

复制0

0

备注:

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

  题意:给n个区间,然后计算区间断开的长度和。

  对区间左端点排序,那么从前往后遍历,记录右侧最大值,如果右侧最大值小于下一个端点的左端点就说明左端点为当前值是最大值都无法连接上下一个左端点,那么就有了断开的长度,就是在等于当前左端点值并且右端点最大的值记作maxlen,下一个左端点的值是l,则断开的距离是l - maxlen以此迭代求解。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
vector<pair<ll,ll>> v;
typedef pair<ll,ll> PII;
ll const N = -1e10;
bool cmp(PII x,PII y)
{
    return x.first < y.first;
}
int main()
{
     int n;
     cin>>n;
     for(int i=1;i<=n;i++)
     {
         ll x,r;
         cin>>x>>r;
         v.push_back({x-r,x+r});
     }
    sort(v.begin(),v.end());
    ll sum = 0;
    ll r = 0 ;
    ll maxlen = N;
    for(int i=0;i<v.size()-1;i++)
    {     
          r = v[i].second;
          maxlen = max(maxlen,r);
          int nex = v[i+1].first;
          if(maxlen < nex)
          {
              sum = sum + nex - maxlen;
          }
    }
    cout<<sum;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值