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天的时间待在云南中医 ...
随机推荐
- python2.7.13标准库文件目录操作与文件操作
标准库的中文参考文档: http://python.usyiyi.cn/translate/python_278/library/index.html 官方标准库文档:https://docs.pyt ...
- Python异或加密字符串
import os import sys import struct def enc(path, key): path_ret = "" for i in range(0, len ...
- 平衡二叉搜索树AVL
package com.sunshine.AlgorithmTemplate; import com.sunshine.OFFER66_SECOND.BalanceTreeNode; import c ...
- Android深度探索-卷1第六章心得体会
这章主要介绍了第一个linux驱动程序:统计单词个数.Linux系统将每一个驱动都映射成一个文件,这些文件称为设备文件或驱动文件,都保存在/dev目录中.大多数Linux驱动都有与其对应的设备文件,因 ...
- 用select实际非阻塞I/O
非阻塞read/write 函数返回0表示可读或可写, -1表示select失败或超时 select返回0表示超时,-1表示读取失败,1表示可读或可写 int read_timeout(int fd, ...
- Cocos2d 之FlyBird开发---GameAbout类
| 版权声明:本文为博主原创文章,未经博主允许不得转载.(笔者才疏学浅,如有错误,请多多指教) 一般像游戏关于的这种界面中,主要显示的是游戏的玩法等. GameAbout.h #ifndef _G ...
- python基础----斐波那契数列
python实现斐波那契数列的三种方法 """ 斐波那契数列 0,1,1,2,3,5,8,13,21,... """ # 方法一:while ...
- 牛客网练习赛 2 烟花(概率dp)
题目传送门 烟花 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K Special Judge, 64bit IO Format: %lld 题目 ...
- gym/102253C Colorful Tree 树上计数
题意:有一颗数,每个点有一个颜色,定义两点之间的距离为两点路径之间不同颜色的数目,问所有路径的距离和是多少? 思路:每个颜色的贡献为路径中有这个颜色的路径数.先假设所有路径都会经过一种颜色,再减去不会 ...
- 五、Redis五种类型 - 字符串类型
1.介绍: 字符串类型是Redis中最基本的数据类型,可以存储任何形式的字符串数据,最大容量是512MB. key 和 value 都是区分大小写的. 2.命令介绍 (1).赋值: set key v ...