upc组队赛3 T-net【贪心】
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【贪心】的更多相关文章
- upc 组队赛18 STRENGTH【贪心模拟】
STRENGTH 题目链接 题目描述 Strength gives you the confidence within yourself to overcome any fears, challeng ...
- upc组队赛6 Canonical Coin Systems【完全背包+贪心】
Canonical Coin Systems 题目描述 A coin system S is a finite (nonempty) set of distinct positive integers ...
- upc组队赛3 Chaarshanbegaan at Cafebazaar
Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...
- upc组队赛2 Super-palindrome【暴力枚举】
Super-palindrome 题目描述 You are given a string that is consisted of lowercase English alphabet. You ar ...
- upc组队赛1 过分的谜题【找规律】
过分的谜题 题目描述 2060年是云南中医学院的百年校庆,于是学生会的同学们搞了一个连续猜谜活动:共有10个谜题,现在告诉所有人第一个谜题,每个谜题的答案就是下一个谜题的线索....成功破解最后一个谜 ...
- upc组队赛1 不存在的泳池【GCD】
不存在的泳池 题目描述 小w是云南中医学院的同学,有一天他看到了学校的百度百科介绍: 截止到2014年5月,云南中医学院图书馆纸本藏书74.8457万册,纸质期刊388种,馆藏线装古籍图书1.8万册, ...
- upc组队赛1 黑暗意志【stl-map】
黑暗意志 题目描述 在数千年前潘达利亚从卡利姆多分离之时,迷雾笼罩着这块新形成的大陆,使它不被外来者发现.迷雾同样遮蔽着这片大陆古老邪恶的要塞--雷神的雷电王座.在雷神统治时期,他的要塞就是雷电之王力 ...
- upc组队赛1 闪闪发光 【优先队列】
闪闪发光 题目描述 一所位于云南昆明的中医药本科院校--云南中医学院. 因为报考某专业的人数骤减,正面临着停招的危机. 其中有九名少女想到一条妙计--成为偶像, 只要她们成为偶像,学校的名气便会增加, ...
- upc组队赛1 流连人间的苏苏
流连人间的苏苏 题目描述 苏苏在做红尘仙的任务时,发现坐落于风景秀丽.四季如春的昆明市的云南中医学院. 没过多久,苏苏就喜欢上了这个学校.以致于苏苏忘了回涂山的时间,现在她只剩下d天的时间待在云南中医 ...
随机推荐
- Java 时间相关
java的时间主要关注这几个类,查看Java API 1.6 java.util.Calendar Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MON ...
- vscode 配置 golang开发环境
如果你使用golang,那么强烈建议你采用vscode作为IDE. 1. 首先在vscode 当中安装go插件,如上图 2. 配置 %AppData%\Code\User\settings.json ...
- js记住密码
$(function () { if (getCookie("rmbUser") == "true") { $("#xuanzong") ...
- 哈希算法和字典类的定义,DataSet中数据遍历的几种方法
哈希算法的基本操作: 1. 哈希表(HashTable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似 ...
- springCloud的使用04-----熔断器hystrix的使用
1. restTemplate+ribbon使用hystrix 1.1 引入依赖 <!-- 配置hystrix断路器 --> <dependency> <groupId& ...
- 修改bug 提交出错:操作失败: 无法更改关系,因为一个或多个外键属性不可以为 null
提交出错:操作失败: 无法更改关系,因为一个或多个外键属性不可以为 null.对关系作出更改后,会将相关的外键属性设置为 null 值.如果外键不支持 null 值,则必须定义新的关系,必须向外键属性 ...
- JS面向对象——构造函数模型
ECMAScript中的构造函数可用来创建特定类型的对象.我们可以创建自定义构造函数,从而定义对象类型的属性和方法,解决工厂模型中对象识别的问题. <!DOCTYPE html> < ...
- 2018-2-13-关于Host(主机)
title author date CreateTime categories 关于Host(主机) lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 ...
- Ubuntu 16.04 修改状态栏位置
修改状态栏位置: gsettings set com.canonical.Unity.Launcher launcher-position Bottom #底部(苹果风格) gsettings set ...
- ajax+node实现图片上传
利用formData实现ajax上传图片后,保存图片到指定收藏夹,然后展示新重命名后的图片 html: <input type="file" id="uploadI ...