zoukankan      html  css  js  c++  java
  • [贪心]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

    #include<bits/stdc++.h>
    using namespace std;
    
    int s[100005];
    int d[100005];
    
    int main()
    {
        int n,a,b;
        scanf("%d%d%d",&n,&a,&b);
        if(a>b) swap(a,b);
        for(int i=1;i<=n;i++) scanf("%d",&s[i]);
        sort(s+1,s+1+n);
        for(int i=2;i<=n;i++) if(s[i]-s[i-1]>b) return 0*puts("-1");
        s[0]=s[1],s[n+1]=s[n];
        for(int i=1;i<=n;i++){
            if(d[i]==a) continue;
            if(!d[i]){
                if(s[i]-s[i-1]<=a&&s[i+1]-s[i]<=a) {d[i]=a;continue;}
                else d[i]=b;
            }
            int j=i;
            while(s[i]+b>=s[j]&&j<=n) j++;
            j--;
            int flag=0;
            for(int k=i+1;k<=j-1;j++){
                if(s[k]-s[k-1]>a||s[k+1]-s[k]>a){
                    flag=1;
                    break;
                }
            }
            if(!flag) continue;
            d[j]=b;
            for(int k=i+1;k<=j-1;k++){
                if(s[k]-s[k-1]<=a) d[k]=a;
                else break;
            }
            for(int k=j-1;k>=i+1;k--){
                if(s[k+1]-s[k]<=a) d[k]=a;
                else break;
            }
        }
        long long ans=0;
        for(int i=1;i<=n;i++) ans+=d[i];
        cout<<ans<<endl;
    }
    View Code
  • 相关阅读:
    开发3dMax插件的方法和应用
    实现X*N
    fork与vfork的区别
    使用递归算法写一段程序
    宏和函数的区别
    多线程学习(一)
    [算法天天练]堆排序
    [转][算法天天练]堆与堆排序
    [转]快速排序 挖坑讲解方法
    [算法天天练]选择排序法
  • 原文地址:https://www.cnblogs.com/lllxq/p/10691535.html
Copyright © 2011-2022 走看看