演算法›Ch7 貪婪演算法
第 10 題/共 12 題
◀ AL 10/12
10. Greedy Algorithm、Time Complexity
#AL-07-010中Greedy AlgorithmTime Complexity
題組題幹(本題:10,共 3 小題)點擊展開

Consider a UAV (Unmanned Aerial Vehicle) with F unit of fuel travels from NCU to a destination "target" km away. There are n gas stations along the way. When the UAV refuels at a gas station, all the fuel of the gas station is transferred into the UAV.

Suppose that the UAV consumes one unit of fuel for every kilometer it travels. Let the position and fuel of a gas station indicate the distance between the gas station and NCU and its volume of fuel, respectively. Return the minimum number of refueling stops the UAV must make in order to reach its destination.

typedef struct {
    int position;
    int fuel;
} stop_info;

int Refuel(int target, int F, int n, stop_info s[]) {
    int N_R = 0;    // number of refuel
    int i;  //gas stations ID
    stop_info X;

    while (F < target) {
        for (i=0;  L1  ; ++i)
            Q.push(s[i].fuel);
        if (Q.empty()) return -1;
        X=Q.pop();
        F += X.fuel;
        N_R++;
    }
    return N_R;
}

Consider Refuel algorithm above. Which of the following statements is true?

📄 中央114
跳轉到第題
▤完整推導請見《WH 資工筆記 · 演算法》Ch7 貪婪演算法
本章題號 · 1–12 / 12