貪心演算法
貪心演算法的所謂“貪心”,就是將問題轉化為多個小問題,並求得這多個子問題的最優解,最終解的最優解便是這多個小問題最優解的串聯。
在做貪心演算法時,有兩點需要考慮:1,如何將問題分解為一個個子問題。2,尋求所有子問題的最優解。
這裡先舉兩個例子:
一,
[1 , 5] ,[2 , 3],[4 , 5],[7 , 8],[4 , 8],[2 , 7],[2 , 6] 為一間教室中的課表時間,[上課時間,下課時間],問該教室最多可以安排幾節課
子問題:下課越早,可以安排的課程就越多 。 最優解:將課程結束時間排序,每次安排結束時間最早的課程。
二,
有100,50,20,10,5,2,1的錢幣若干張,現在需要以最少張數來找零
很簡單每次都用面值最大的來找零就ok
1033 To Fill or Not to Fill (25 分)
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive numbers: C m a x ( ≤ 100), the maximum capacity of the tank;
Output Specification:
For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, printThe maximum travel distance = X
whereX
is the maximum possible distance the car can run, accurate up to 2 decimal places.
Sample Input 1:
50 1300 12 8 6.00 1250 7.00 600 7.00 150 7.10 0 7.20 200 7.50 400 7.30 1000 6.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2 7.10 0 7.00 600
Sample Output 2:
The maximum travel distance = 1200.00 這道題目要求我們判斷能否從杭州到達目的地,以及尋找從杭州到目的地最少的加油費用 這道題目使用貪心演算法, 每次尋找加滿油之後可以到達的加油站中: 1.若是有比當前加油站便宜的加油站,則將汽油加到剛好可以到達第一個比該加油站便宜的站點,為什麼不選擇最便宜的那一個,而是選擇第一個 比當前加油站便宜的站點呢?我們可以考慮一種特殊情況:若是當前的加油站的汽油特別特別貴,而下一個緊挨著他的加油站便宜得多,而他可以 可以到達的最便宜的站點要加滿油才過得去,顯然這種情況下尋找最便宜的站點是不合適的。 2.若是所有可以到達的汽油站的價格都高於當前的加油站,這時我們要判斷:是否可以直接開到終點呢?如果可以,則加油到可以開到終點的油量 就歐克。如果不可以,那麼尋找可以到達的加油站中最便宜的加油站,加滿油開過去。 具體程式碼實現如下:
import java.util.*; public class Main { public static void main(String args[]) { Scanner scanner = new Scanner(System.in) ; float tank = scanner.nextFloat() ; float dis = scanner.nextFloat() ; float rundis = scanner.nextFloat() ; float num = scanner.nextFloat() ; scanner.nextLine() ; Map<Float,Float> map = new HashMap<>() ; List<Float> list = new ArrayList<>() ; for(int i = 0 ; i < num ; i ++) { float money = scanner.nextFloat() ; float distance = scanner.nextFloat() ; map.put(distance, money) ; list.add(distance) ; } scanner.close(); Collections.sort(list); float gas = 0 ; float sumdis = 0 ; float summoney = 0 ; for(int i = 0 ; i < num - 1 ; i ++) { float d1 = list.get(i) ; float d2 = list.get(i+1) ; if(d2 - d1 > tank*rundis) { System.out.print("The maximum travel distance = "); System.out.printf("%.2f",sumdis+rundis*tank); return ; } float min = 100 ; for(int j = i+1 ; j < num ; j ++) { if(map.get(list.get(j)) <= min && list.get(j) - d1 <= tank*rundis) { i = j - 1; d2 = list.get(j) ; min = map.get(d2) ; if(min < map.get(d1)) break ; } } float needgas = (d2 - d1)/rundis ; if(map.get(d2) <= map.get(d1)) { if(gas > needgas) gas -= needgas ; else { summoney += (needgas-gas)*map.get(d1) ; gas = 0 ; } } else { if(dis - d1 <= tank*rundis) { needgas = (dis - d1)/rundis ; if(gas >= needgas) { System.out.printf("%.2f",summoney); return ; } else { summoney += (needgas - gas)*map.get(d1) ; System.out.printf("%.2f",summoney); return ; } } summoney += (tank-gas)*map.get(d1) ; gas = tank - needgas ; } sumdis = d2 ; } if(dis - sumdis > tank*rundis) { System.out.print("The maximum travel distance = "); float diss = sumdis+rundis*tank ; System.out.printf("%.2f",diss); return ; } else { float needgas = (dis - sumdis)/rundis ; if(gas >= needgas) System.out.printf("%.2f",summoney); else { summoney += (needgas-gas)*map.get(sumdis) ; System.out.printf("%.2f",summoney); } } } }