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
    评论
下面是一个C语言的实现,包含菜单和输入功能: ```c #include <stdio.h> #include <stdlib.h> int select_gas_stations(int N, int num_stations, int stations[]) { int current_gas = N; int gas_stations[num_stations]; int num_refills = 0; int current_position = 0; while (current_position < num_stations) { if (current_gas >= stations[current_position]) { current_gas -= stations[current_position]; current_position++; } else { gas_stations[num_refills] = current_position - 1; num_refills++; current_gas = N; } } printf("Selected gas stations: "); for (int i = 0; i < num_refills; i++) { printf("%d ", gas_stations[i]); } printf("\n"); return num_refills; } int main() { int choice, N, num_stations; printf("Enter the value of N (maximum distance after refueling): "); scanf("%d", &N); printf("Enter the number of gas stations: "); scanf("%d", &num_stations); int stations[num_stations]; printf("Enter the distances between gas stations: "); for (int i = 0; i < num_stations; i++) { scanf("%d", &stations[i]); } do { printf("\n1. Find minimum number of refills\n"); printf("2. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: select_gas_stations(N, num_stations, stations); break; case 2: printf("Exiting program...\n"); exit(0); default: printf("Invalid choice. Try again.\n"); break; } } while (choice != 2); return 0; } ``` 该程序使用了一个菜单,让用户选择要执行的操作。用户可以输入N、加油站数量和加油站之间的距离,然后选择查找最小加油次数的操作。该算法的时间复杂度为O(n),其中n为加油站的数量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值