简体   繁体   中英

How to prove greedy is correct to uva 10026

The original program description is:

A shoemaker has N orders from customers which he must satisfy. The shoemaker can work on only one job in each day, and jobs usually take several days. For the ith job, the integer Ti ( 1$ \\le$Ti$ \\le$1, 000) denotes the number of days it takes the shoemaker to finish the job.

But popularity has its price. For each day of delay before starting to work on the ith job, the shoemaker has agreed to pay a fine of Si ( 1$ \\le$Si$ \\le$10, 000) cents per day. Help the shoemaker by writing a program to find the sequence of jobs with minimum total fine.

Input

The input begins with a single positive integer on a line by itself indicating the number of the test cases, followed by a blank line. There is also a blank line between two consecutive cases.

The first line of each case contains an integer reporting the number of jobs N, where 1$ \\le$N$ \\le$1, 000. The ith subsequent line contains the completion time Ti and daily penalty Si for the ith job.

Output

For each test case, your program should print the sequence of jobs with minimal fine. Each job should be represented by its position in the input. All integers should be placed on only one output line and each pair separated by one space. If multiple solutions are possible, print the first one in lexicographic order.

The output of two consecutive cases must be separated by a blank line.

Sample Input

1

4 3 4 1 1000 2 2 5 5 Sample Output

2 1 3 4

On Internet, I see the best solution is greedy on job's ratio of time and fine. But I don't think it is as simple as self-explanatory, so how to prove it strictly? Thanks!

Thanks, @mrmcgrep. I got the answer as this:

Let's say we have job 1, 2, ..., n, and they have time and fine as t1, f1, t2, f2, ..., tn, fn

and they are in the order of t1/f1 <= t2/f2 <= t3/f3 <= ... <= tn/fn

So this is the objective schedule. Now we change 1 with m (1 < m <= n)

By the original order, we need pay fine as much as: F1 = t1 * (f2 + ... + fn) + t2 * (f3 + ... + fn) + ... + tm * (fm+1 + ... + fn) + R

By the new order, we need pay fine as much as: F2 = tm * (f1 + ... + fm-1 + fm+1 + ... + fn) + t1 * (f2 + ... + fm-1 + fm+1 + ... + fn) + ... + fm-1 * fm+1 + ... + fn) + R

F1 - F2 = (t1 + t2 + ... + tm-1) * fm - (tm * f1 + tm * f2 + ... + tm * fm-1)

As t1 * fm <= tm * f1, t2 * fm <= tm * f2, ..., tm-1 * fm <= tm * fm-1 F1 - F2 <= 0

So the original order is the best order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM