问题 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. CareerCup All in One 题目汇总

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  2. [转] - xargs 分析

    原文出处:http://www.cnblogs.com/f-ck-need-u/p/5925923.html 学习这个xargs花了很长时间,在网上翻了很久也查了很多书关于xargs的介绍,都只是简单 ...

  3. mui 轮播

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  4. HTML、CSS知识点,面试开发都会需要--No.7 数据列表

    No.7 数据列表 1.无序列表Unordered List 无序列表用block-level元素ul(unordered list)表示,每个item单独使用li(list)标记.如下代码所示: & ...

  5. 悬线法 || BZOJ 1057: [ZJOI2007]棋盘制作 || Luogu P1169 [ZJOI2007]棋盘制作

    题面:P1169 [ZJOI2007]棋盘制作 题解: 基本是悬线法板子,只是建图判断时有一点点不同. 代码: #include<cstdio> #include<cstring&g ...

  6. php 延迟静态绑定: static关键字

    abstract class DomainObject { public static function create() { return new self(); } } class User ex ...

  7. AngularJS实现可伸缩的页面切换

    AngularJS实现可伸缩的页面切换 AngularJS 1.2 通过引入基于纯CSS class的切换和动画,在一个单页面应用创建页面到页面的切换变得更加的容易.只需要使用一个ng-view,让我 ...

  8. Python的类的组合

    python中,类的加载顺序 类是在文件加载时,会跟着加载 1  类名 2  类中的变量,从上到下,依次进行 3 加载到方法时,方法内的内容不执行,但是会开辟一个空间用来存储方法内的数据 4 当类内的 ...

  9. CentOS 6.8 安装TigerVNC 实现 Linux 远程桌面并安装火狐浏览器

    CentOS 6.8 安装TigerVNC 实现 Linux 远程桌面并安装火狐浏览器 vnc客户端地址:https://files.cnblogs.com/files/MYSQLZOUQI/vnc- ...

  10. linux下查看CPU、内存、磁盘信息

    1.查看CPU信息# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数cat /proc/c ...