貪心法
鞋匠有 項工作要完成,他知道自己每項工作需要多少工作天能夠完成
以及每個工作延誤一天需要繳多少延遲的費用
此外,延遲天數計算是從當天算到該工作開始的那天(意味著第一件工作沒罰金)
對了! 他每天只能做一件工作,不是多工的哦!
題目有給一項測試資料 以這個測資來說
如果工作順序為 則罰金: 反之,如果工作順序為 則罰金: 這個問題的目標是幫助鞋匠找出能使罰金最少的工作順序
Source: 題目傳送門
Shoemaker has jobs (orders from customers) which he must make. Shoemaker can work on only one job in each day.
For each job, it is known the integer , the time in days it takes the shoemaker to finish the job.
For each day of delay before starting to work for the job, shoemaker must pay a fine of cents.
Your task is to help the shoemaker, writing a program to find the sequence of jobs with minimal total fine.
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of thme as described below.
This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
First line of input contains as integer .
The next lines each contain two numbers:
- time (needs)
- fine (fine / day)
For each test case, the output must follow the description below.
The outputs of two consecutive cases will be separated by a blank line.
Your program should print the sequence of jobs with minimal fine. Each job should be represented by its number in input.
All integers should be placed on only one output line and separated by one space.
If multiple solutions are possible, print the first lexicographically.
1
4
3 4
1 1000
2 2
5 5
2 1 3 4
像是機會成本的概念,我選擇先做哪一個工作,此時其它工作就會累計罰金,所以要找一個方式衡量目前選擇的重要性
對於適當排序的狀況,兩個相鄰的工作 ,若順序為 或 ,會注意到除了 以外,其餘工作的罰金並不會改變,也就是說關鍵在於選 , 選 哪個損失比較小
假設如下:
Time | Fine |
---|---|
In the first case, the inequality should be correct: 同除以 :
同上述結果,會發現兩兩比較時,只需要將 數值由小到大排序即可
字典序的問題則只需要選擇穩定的排序法,確保相同值時不會更動順序即可