T-net

题目描述

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

提示

In the first case, there are four lamp pieces, two of each type. The worst possible lamp has value 1 + 1 = 2,

while the second worst possible lamp has value 2 + 1 = 3.

题解

一个相当复杂的贪心,参考[大神的题解](https://blog.csdn.net/xiao__hei__hei/article/details/89066042)

代码

#include<bits/stdc++.h>
using namespace std;
#define memset(x,y) memset(x,y,sizeof(x))
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define print(x) printf("%d\n",x)
#define N 100001
typedef pair<int,int> P;
typedef long long ll;
const double eps=1e-8;
const double PI = acos(1.0);
const int inf = 0x3f3f3f3f;
const ll MOD = 998244353;
const int mod = 1e9+7;
const int maxn = 1e5 + 10;
ll ans ;
int flag ;
int n,a,b;
int pos[maxn];
int op[maxn];
int main()
{
read3(n,a,b);
if(a > b) swap(a,b);
for(int i = 1; i <= n ;i++)
read(pos[i]);
sort(pos,pos + n + 1);
pos[0] = pos[1] ;
pos[n + 1] = pos[n];
for(int i = 1 ; i <= n ; i++){
if(op[i]) continue;
if(pos[i] - pos[i - 1] > b && pos[i + 1] - pos[i] > b){
printf("-1\n");
return 0;
}
if(pos[i] - pos[i - 1] <= a && pos[i + 1] - pos[i] <= a){
op[i] = a;
continue;
}
op[i] = b;
while(op[i] == b){
int j = i;
while(j <= n){
if(pos[i] + b < pos[j]) break;
j++;
}
j--;
flag = 0;
for(int k = i + 1; k < j ; k++){
if(pos[k] - pos[k - 1] > a || pos[k + 1] - pos[k] > a){
flag = 1;
break;
}
}
if(!flag) break;
op[j] = b;
int l, r;
for(l = i + 1 ; l < j; l++){
if(pos[l] - pos[l - 1] <= a)
op[l] = a;
else
break;
}
for(r = j - 1; r > i; r--){
if(op[r]) break;
if(pos[r + 1] - pos[r] <= a)
op[r] = a;
else
break;
}
while(l <= r){
op[l++] = b;
while(l <= r && pos[l] - pos[l - 1] <= a){
op[l++] = a ;
}
}
i = j;
}
}
ans = 0;
for(int i = 1; i <= n; i++){
ans += op[i];
}
printf("%lld\n",ans);
}

最新文章

  1. Linux 基础命令
  2. MacBook下如何安装mysql-python
  3. 修改MySQL中字段的类型和长度
  4. 设置IE默认文本模式的方法
  5. CentOS升级MySQL到5.5
  6. Hadoop入门进阶课程3--Hadoop2.X64位环境搭建
  7. XPath 详解,总结
  8. JavaScript中的與和或的規則
  9. php isset和empty方法的区别
  10. bzoj 1189 紧急疏散 网络流
  11. gzy的摄影梦
  12. 阿里云服务器配置https(port443)后客户端 svn check out 失效解决办法
  13. css3 弹性效果上下翻转demo
  14. AtCoder Regular Contest 100 (ARC100) D - Equal Cut 二分
  15. 结对项目junit测试用例
  16. SpringBoot在IDEA中实现热部署
  17. 使用Redis-Dump 导出、导入redis数据
  18. java 获取局域网中的全部主机名和IP地址
  19. Mysql下SELECT的结果集作为UPDATE的WHERE条件时的SQL语法
  20. 2018用IDEA搭建SSM框架(Spring+SpringMVC+Mybatis)

热门文章

  1. Hive SQL语法总结
  2. python 装饰器 第七步:带有参数的装饰器
  3. pycharm格式化python代码快捷键Ctrl+Alt+L失效
  4. mybatis默认的数据源连接池(PooledDataSource和UnPooledDataSource)
  5. 看不懂源码?先来恶补一波Object原型吧
  6. 用户Bug修补报告
  7. 【记录】linux 文件权限的查看和修改
  8. 解决 Failed to load class &quot;org.slf4j.impl.StaticLoggerBinder&quot;
  9. 微信浏览器 video - android适配
  10. vue 项目中使用阿里巴巴矢量图标库iconfont