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

贪心或二分

扫描二维码关注公众号,回复: 4731968 查看本文章
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);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38970751/article/details/85524514
gas