UPC T-net 贪心

问题 H: T-net

时间限制: 1 Sec  内存限制: 128 MB
提交: 415  解决: 38
[提交] [状态] [命题人:admin]

题目描述

T-net which is a new telecommunications company, plans to install its base stations in the city. The places where base stations must be installed have been already specified. T-net has two types of antennas to be used in the base stations: (i)antennas with transmission radius a, and (ii) antennas with transmission radius b. Two antennas can communicate with each other if and only if both are inside the coverage area of each other. Antenna with smaller transmission radius of course is cheaper. T-net plans to minimize its cost while keeping the whole network connected. Precisely, T-net plans to
minimize its cost which is the sum of the transmission radii of all antennas. Interestingly, all base-station places are on a line. Help T-net construct a connected network with the minimum cost.

 

输入

The first line of the input contains three positive integers n, a and b (1 ⩽ n ⩽ 105 and 1 ⩽ a, b ⩽ 105 ) where n is the number of base stations, and a and b are radii as defined in the problem statement. The second line contains n distinct coordinates of base stations on the line with respect to an origin on the line. All coordinates are positive integers not more than 105 .

 

输出

If it is possible to construct a connected network, print the minimum cost in the output. Otherwise, print -1 .

 

样例输入

复制样例数据

3 1 3
1 4 3

样例输出

7

 

[提交][状态]

题意:有两种基站,每个位置都需要安装一个,保证所有的基站都可以通讯,x与y可以通讯的条件是x与y初安装的基站的范围>=x与y之间的距离

一开始都觉得是个非常非常水的题,感觉好像顺序连接就可以,结果当然是wa的自闭

后来发现原来可以绕着连

就比如存在 a b c d四个位置,可能a与d相连,然后b连c,c连d,可能这样是花费最小的方案

如果把这种条件叫环的话,那么也有可能出现环套环套环

那么,出乎意料的还是贪心处理

只要找到必须花费大的位置,假设大花费是b,就从这个点开始找离他最远但距离<=b的点

接着就在这里面开始寻找环,可以证明环里面只可能出现一个点可能断掉一个边,利用这个条件寻找

描述的不太清楚,具体还是看代码理解

代码:

#include <bits/stdc++.h>
 
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 100;
int s[maxn];
int ans[maxn];
 
int main() {
    int n, a, b;
    cin >> n >> a >> b;
    if (a > b) swap(a, b);
    for (int i = 1; i <= n; i++)
        scanf("%d", &s[i]);
    sort(s + 1, s + n + 1);
    s[0] = s[1];
    s[n + 1] = s[n];
    for (int i = 1; i <= n; i++) {
        if (s[i] - s[i - 1] <= a && s[i + 1] - s[i] <= a) {
            ans[i] = a;
            continue;
        }
        if (s[i] - s[i - 1] > b || s[i + 1] - s[i] > b) {
            puts("-1");
            return 0;
        }
        ans[i] = b;
        while (ans[i] == b) {
            int j = i;
            while (s[j] - s[i] <= b && j<=n) j++; //找到距i点最远的距离<=b的点
            j--;
            int f = 1;
            for (int o = i + 1; o <= j; o++) {
                if (s[o] - s[o - 1] > a) {
                    f = 0;
                    break;
                }
            }
            if (f) break;   //距离都<a,不存在
            ans[j] = b;
            int l = i, r = j; //分别表示最左和最右出现距离>a的点
            for (l = i + 1; l < j; l++) {
                if (s[l] - s[l - 1] <= a)
                    ans[l] = a;
                else break;
            }
            for (r = j - 1; r > i; r--) {
                if (s[r + 1] - s[r] <= a)
                    ans[r] = a;
                else break;
            }
            while (l <= r) {
                ans[l] = b;
                for (l = l + 1; l <= r; l++) {
                    if (s[l] - s[l - 1] <= a) {
                        ans[l] = a;
                    } else break;
                }
            }
            i = j;
        }
    }
    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum += ans[i];
    printf("%d\n", sum);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值