问题 H: T-net

时间限制: 1 Sec  内存限制: 128 MB

提交: 302  解决: 14

[提交] [状态] [命题人:admin]

题目描述

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

贪心只能过样例

照着大神的题解写的 tql

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

T-net 【贪心】的更多相关文章

  1. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  7. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  8. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  9. 【贪心】HDU 1257

    HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...

  10. hdu 2037简单贪心--活动安排问题

    活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...

随机推荐

  1. nginx配置-为没有后缀的文件(实际上是有html文件)以html形式打开

    location ~ index.php@ { add_header content-type "text/html"; }

  2. Nest.js 守卫

    Docs: https://docs.nestjs.com/guards 当调用者具有足够的权限时,特定路由才可用 // app.guard.ts import { CanActivate, Exec ...

  3. [Codeforces Round #221 (Div. 1)][D. Tree and Queries]

    题目链接:375D - Tree and Queries 题目大意:给你一个有n个点的树,每个点都有其对应的颜色,给出m次询问(v,k),问v的子树中有多少种颜色至少出现k次 题解:先对所有的询问进行 ...

  4. Web前端攻击方式及防御措施

    一.XSS [Cross Site Script]跨站脚本攻击 恶意攻击者往Web页面里插入恶意Script代码,当用户浏览该页之时,嵌入其中Web里面的Script代码会被执行,从而达到恶意攻击用户 ...

  5. Java开发岗位面试题归类

    一.Java基础 1. String类为什么是final的. ( 1.由于String类不能被继承,所以就不会被修改,这就避免了因为继承引起的安全隐患: 2.String类在程序中出现的频率比较高,如 ...

  6. PHP(层叠样式表,写法分类),选择器的种类)

    表单元素的取值怎么取  对应的属性值都有哪些? <span> 标签被用来组合文档中的行内元素. 注释:span 没有固定的格式表现.当对它应用样式时,它才会产生视觉上的变化. style ...

  7. Gym 101194C / UVALive 7899 - Mr. Panda and Strips - [set][2016 EC-Final Problem C]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  8. [Day11]接口、多态

    1.接口 (1)接口定义:interface关键字 ,所在文件仍然是.java文件,编译后仍产生.class文件.       定义格式 public interface 接口名{ 抽象方法1: 抽象 ...

  9. java学习笔记-连数据库JDBC

    查看JDK API文档,学习JDBC中类和接口的定义,分析其中的方法 ===============PreparedStatement ===================查询

  10. java框架之SpringCloud(3)-Eureka服务注册与发现

    在上一章节完成了一个简单的微服务案例,下面就通过在这个案例的基础上集成 Eureka 来学习 Eureka. 介绍 概述 Eureka 是 Netflix 的一个子模块,也是核心模块之一.Eureka ...