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. WPF自定义控件(三)

    今天我们开始制作我们的按钮,主要的效果就是一个按钮正常状态.鼠标滑过.按下三态显示不同的图片. 首先我们需要给扩展按钮添加三个属性,分别是正常状态图片,鼠标滑过图片,按钮按下图片. 先贴出Button ...

  2. PHP+JS的信息提示弹窗

    基于PHP函数的Msg信息提示框 1.可以设置弹出信息,跳转地址,跳转的时间,跳转的信息标题提示: 2.代码实例: <?php function ShowMsg($msg, $gourl,$ti ...

  3. Goals ? Ideals ?

    Why is it important to set goals ? Because goal can help you do , be , and experience anything you w ...

  4. C#用new和override来实现抽象类的重写区别

    一,抽象的实现 using System; using System.Collections.Generic; using System.Linq; using System.Text; namesp ...

  5. js转换成布尔类型boolean

    /** * js转换成布尔值 * a.转换方法:Boolean(var) * b.数字转换成布尔,除了0与NaN,其余都是true * c.字符串转换成布尔,除了空串"",其余都是 ...

  6. js高级编程思想

    js惰性思想: 能够执行一次就搞定绝对不会执行第二次 function createXHR(){ var xhr=null, falg=false, ary=[ function(){ return ...

  7. InnoDB不支持contains等

    并非所有引擎都支持全文本搜索MySQL.与所有其他的DBMS一样,MySQL具有一个具体管理和处理数据的内部引擎.在你使用CREATE TABLE语句时,该引擎具体创建表,而在你使用SELECT语句或 ...

  8. 服务器修改静态ip

    ifconfig,看当前网卡名字 设置 ip地址,子网掩码 ,广播地址 ifconfig eth1 172.16.11.25 netmask 255.255.255.0 broadcast 172.1 ...

  9. 入门phantomjs

    前言 phantomjs是一个无界面浏览器,用来操作web页面的一个工具,比如登录,提交表单等等. 语法框 //创建一个浏览器对象 var page = require('webpage').crea ...

  10. 异常:Error response from daemon: conflict: unable to delete 6fa48e047721 (cannot be forced) - image has dependent child images

    在删除镜像之前要先用 docker rm 删掉依赖于这个镜像的所有容器(哪怕是已经停止的容器),否则无法删除该镜像. 停止容器 # docker stop $(docker ps -a | grep ...