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);
}

upc组队赛3 T-net【贪心】的更多相关文章

  1. upc 组队赛18 STRENGTH【贪心模拟】

    STRENGTH 题目链接 题目描述 Strength gives you the confidence within yourself to overcome any fears, challeng ...

  2. upc组队赛6 Canonical Coin Systems【完全背包+贪心】

    Canonical Coin Systems 题目描述 A coin system S is a finite (nonempty) set of distinct positive integers ...

  3. upc组队赛3 Chaarshanbegaan at Cafebazaar

    Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...

  4. upc组队赛2 Super-palindrome【暴力枚举】

    Super-palindrome 题目描述 You are given a string that is consisted of lowercase English alphabet. You ar ...

  5. upc组队赛1 过分的谜题【找规律】

    过分的谜题 题目描述 2060年是云南中医学院的百年校庆,于是学生会的同学们搞了一个连续猜谜活动:共有10个谜题,现在告诉所有人第一个谜题,每个谜题的答案就是下一个谜题的线索....成功破解最后一个谜 ...

  6. upc组队赛1 不存在的泳池【GCD】

    不存在的泳池 题目描述 小w是云南中医学院的同学,有一天他看到了学校的百度百科介绍: 截止到2014年5月,云南中医学院图书馆纸本藏书74.8457万册,纸质期刊388种,馆藏线装古籍图书1.8万册, ...

  7. upc组队赛1 黑暗意志【stl-map】

    黑暗意志 题目描述 在数千年前潘达利亚从卡利姆多分离之时,迷雾笼罩着这块新形成的大陆,使它不被外来者发现.迷雾同样遮蔽着这片大陆古老邪恶的要塞--雷神的雷电王座.在雷神统治时期,他的要塞就是雷电之王力 ...

  8. upc组队赛1 闪闪发光 【优先队列】

    闪闪发光 题目描述 一所位于云南昆明的中医药本科院校--云南中医学院. 因为报考某专业的人数骤减,正面临着停招的危机. 其中有九名少女想到一条妙计--成为偶像, 只要她们成为偶像,学校的名气便会增加, ...

  9. upc组队赛1 流连人间的苏苏

    流连人间的苏苏 题目描述 苏苏在做红尘仙的任务时,发现坐落于风景秀丽.四季如春的昆明市的云南中医学院. 没过多久,苏苏就喜欢上了这个学校.以致于苏苏忘了回涂山的时间,现在她只剩下d天的时间待在云南中医 ...

随机推荐

  1. 【转】Selenium 加载Chrome/Firefox浏览器配置文件

    原文地址:https://www.cnblogs.com/eastonliu/p/9083982.html Selenium启动浏览器时,默认是打开一个新用户,不会加载原有的配置以及插件.但有些时候我 ...

  2. shell 删除隐藏文件.svn

    参考:https://blog.csdn.net/zhangxinrun/article/details/6409125 echo "recursively removing .svn fo ...

  3. String的replace导致内存溢出

    从一次内存溢出来看JDK的String应该怎么用 背景 JDK在String类中给我们提供的API,replace是个使用频率很高的的方法.因为他可以对字符串内容进行替换,只需要输入替换字符串和被替换 ...

  4. undefined,null,!,!=之间的关系

    1.!和!=的关系 2.null 和0的关系

  5. Codeforces 500B New Year Permutation( Floyd + 贪心 )

    B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. ASP.NET Core 2.1 JWT Token 使用 (二) - 简书

    原文:ASP.NET Core 2.1 JWT Token 使用 (二) - 简书 接上文,https://www.jianshu.com/p/c5f9ea3b4b65 ASP.NET Core 2. ...

  7. docker概述与安装及运行容器

    传统虚拟化 传统虚拟化步骤 1.安装虚拟化软件以及虚拟化的管理软件 2.创建虚拟机 3.给虚拟机安装os 4.在虚拟机内部不是应用(http.db之类的应用) 传统虚拟化的特点 1.VM与VM之间是完 ...

  8. elasticsearch 英文数字组合字符串模糊检索

    不分词,然后用wildcard查询 { "query": { "wildcard": { "字段名": "*123*" ...

  9. Docker基础(下)

    Docker基础(下) 链接:https://pan.baidu.com/s/1u8Tg5qB4ZZHEK6GqCJkjwg 提取码:u8hb 复制这段内容后打开百度网盘手机App,操作更方便哦 5. ...

  10. python数字图像处理(一)图像的常见操作

    首先导入必要的库,使用Opencv读入图像,避免复杂的图像解析,同时使用Opencv作为算法的对比,由于使用环境为jupyter使用matplotlib直接可视化 import cv2 import ...