Gas Stations

第216周

题目1 : Gas Stations

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

There are N gas stations on a straight, M kilo-meters long highway. The i-th gas station is Ai kilo-meters away from the beginning of the highway. (It is guaruanteed that there is one gas station at each end of the highway)

Now the mayor can build K more gas stations. He wants to minimize the maximum distance between two adjacent gas station. Can you help him?

输入

The first line contains 3 integer N, M, k. (2 <= N <= 1000, 1 <= M, K <= 100000)  

The second line contains N integer, A1, A2, ... AN. (0 = A1 <= A2 <= ... <= AN = M)

输出

The minimized maximum distance rounded to one decimal place.

样例输入

3 10 2  
0 2 10

样例输出

2.7

贪心或二分

import java.util.*;

public class Main{


    static Scanner scan;
    static int m,n,k;
    static Record[] rs ;
    static void insert(int k){
        rs[k].add();
    }
    static int findMx(){
        int cs = -1;
        double cur = 0;
        for(int i=0;i!=n-1;++i){
            if(cur<rs[i].dv){
                cs = i;
                cur = rs[i].dv;
            }
        }
        return cs;
    }
    public static void main(String[] args){
        scan = new Scanner(System.in);
        n = scan.nextInt();
        m = scan.nextInt();
        k = scan.nextInt();

        rs = new Record[n-1];
        int pre =  scan.nextInt();
        for(int i=1;i!=n;++i){
            int next =  scan.nextInt();
            rs[i-1]=new Record(next-pre);
            pre=next;

        }
        for(int i=0;i!=k;++i){
            int t = findMx();
            insert(t);
        }
        int t = findMx();
        double rst = rs[t].dv;
        System.out.printf("%.1f",rst);
    }
    public static void p(Object o){
        System.out.println(o);
    }
}

class Record{
    int insert;
    double len;
    double dv;
    public Record(int _len){
        len = _len;
        insert=0;
        dv=len;
    }
    void add(){
        this.insert++;
        dv=len/(this.insert+1);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值