zoukankan      html  css  js  c++  java
  • 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
    ",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
    ");
            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
    ",ans);
    }
    
  • 相关阅读:
    [转]软件产品质量和代码质量
    FF,IE8 正确, IE7 报错, 加载不上JS文件 的错误.
    转 让eval()全局作用域执行的方法深入研究(javascript)
    集群、分布式、负载均衡区别与联系
    Microsoft.Jet.OLEDB.4.0”提供程序不支持 ITransactionLocal 接口。本地事务不可用于当前提供程序
    C#调用COM组件的几个步骤
    sql server中分布式查询随笔(sp_addlinkedserver、sp_addlinkedsrvlogin)
    全文检索定义
    浅谈c#中使用lock的是与非
    使用SQL SERVER 2000的全文检索功能
  • 原文地址:https://www.cnblogs.com/llke/p/10800054.html
Copyright © 2011-2022 走看看