简体   繁体   English

如何证明贪婪对uva 10026是正确的

[英]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. 一家鞋匠必须满足客户的N个订单。 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. 对于第i个工作,整数Ti(1 $ \\ le $ Ti $ \\ le $ 1,000)表示鞋匠完成工作所需的天数。

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. 对于开始进行第一项工作之前的每一天的延误,鞋匠已同意每天支付Si(1美元\\ le $ Si $ \\ le $ 10,000)美分的罚款。 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. 每个案例的第一行包含一个整数,该整数报告作业的数量N,其中1 $ \\ le $ N $ \\ le $ 1,000。第i个随后的行包含第i个作业的完成时间Ti和每日罚款Si。

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 1个

4 3 4 1 1000 2 2 5 5 Sample Output 4 3 4 1 1000 2 2 5 5样本输出

2 1 3 4 2 1 3 4

On Internet, I see the best solution is greedy on job's ratio of time and fine. 在Internet上,我认为最好的解决方案是贪婪地工作和工作时间比例。 But I don't think it is as simple as self-explanatory, so how to prove it strictly? 但是我不认为这很容易解释,那么如何严格证明呢? Thanks! 谢谢!

Thanks, @mrmcgrep. 谢谢,@ 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 假设我们有工作1、2,...,n,并且它们的时间和精细度为t1,f1,t2,f2,...,tn,fn

and they are in the order of t1/f1 <= t2/f2 <= t3/f3 <= ... <= tn/fn 它们的顺序为t1 / f1 <= t2 / f2 <= t3 / f3 <= ... <= tn / fn

So this is the objective schedule. 这就是客观的时间表。 Now we change 1 with m (1 < m <= n) 现在我们将m更改为1(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 按照原始顺序,我们需要支付的罚款额为: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 根据新订单,我们需要支付的罚款额为: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) 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 当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. 因此,原始订单是最佳订单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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