Shoemaker has n jobs (orders from MERs mers) which he must make. shoemaker can work on only one job in each day. for each ith job, it is known the integer Ti (1 <= Ti <= 1000), the time in days it takes the shoemaker to finish the job. for each day of delay before starting to work for the ith job, shoemaker must pay a fine of Si (1 <= SI <= 10000) cents. your task is to help the shoemaker, writing a programm to find the sequence of jobs with minimal total fine.
The input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them 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 an integer N (1 <= n <= 1000). The next n lines each contain two numbers: the time and fine of each task in order.
The output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
You programm shocould print the sequence of jobs with minimal fine. each job shoshould be represented by its number in input. all integers shoshould be placed on only one output line and separated by one space. if multiple solutions are possible, print the first lexicographically.
Sample Input
143 41 10002 25 5
Sample output
2 1 3 4
A shoemaker receives many orders. However, each customer thinks that their orders should be processed immediately. Therefore, for order I, Si (1 <= SI ≤ <= 1000) is fined every day before processing the order ). However, he can only process one order each day, and it may take many days to complete an order. For order I, the integer Ti (1 <= Ti <= 1000) indicates the number of days required to process the order. The order processing sequence for the minimum penalty.
Analysis: greedy solution. The fines/days for each order are sorted from large to small, and the results are sorted by serial number (minimum lexicographically ).
Proof: Assume that X and Y are two adjacent orders in the ordered order. Since the order after X and Y is fixed, both X and Y are arranged in xy or Yx, the subsequent fines are not affected. The difference between the fines is XY or Yx. If xy is used, the penalty is TX * Sy. If Yx is used, the penalty is Ty * Sx. If TX * Sy <ty * SX is used, XY is used; otherwise, YX is used. Therefore, this greedy policy is correct.
# Include <cstdio> # include <algorithm> using namespace STD; struct shoe {int ID; int time; int fine;} A [1005]; bool comp (shoe X, shoe y) {// use multiplication to avoid the IF (X. fine * Y. time! = X. time * Y. fine) return X. fine * Y. time> X. time * Y. fine; return X. ID <Y. ID;} int main () {int t, n, I; scanf ("% d", & T); While (t --) {scanf ("% d ", & N); for (I = 0; I <n; I ++) {scanf ("% d", & A [I]. time, & A [I]. fine); A [I]. id = I + 1;} Sort (A, A + N, comp); for (I = 0; I <n-1; I ++) printf ("% d", a [I]. ID); printf ("% d \ n", a [n-1]. ID); If (T> 0) printf ("\ n");} return 0 ;}