You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/orders.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,9 @@ placed into a total order (e.g. rock > paper > scissors) due to their cyclic nat
15
15
> [!info] Tasks and Deadlines
16
16
> ([CSES](https://cses.fi/problemset/task/1630)) You have to process $n$ tasks. Each task has a duration and a deadline, and you will process the tasks in some order one after another. Your reward for a task is $d−f$ where $d$ is its deadline and $f$ is your finishing time. (The starting time is $0$, and you have to process all tasks even if a task would yield negative reward.)
17
17
18
-
The first sneaky observation is that the $+d$ term doesn't actually matter, since we can rewrite the total reward as $sum (d - f)$ = $sum d - sum f$, and note that since we have to complete all the tasks, $sum d$ is constant over all possible orderings. Therefore, we can reduce this problem to maximizing $sum f$ instead.
18
+
The first sneaky observation is that the $+d$ term doesn't actually matter, since we can rewrite the total reward as $sum (d - f)$ = $sum d - sum f$, and note that since we have to complete all the tasks, $sum d$ is constant over all possible orderings. Therefore, we can reduce this problem to minimizing $sum f$ instead.
19
19
20
-
As it turns out, if we want to maximize this quantity, we should always do tasks in order of high to low duration! There are several ways to show this, but one particularly nice way is the _exchange argument_.
20
+
As it turns out, if we want to minimizing this quantity, we should always do tasks in order of low to high duration! There are several ways to show this, but one particularly nice way is the _exchange argument_.
21
21
### Exchange Argument
22
22
This idea is an example of [[local-analysis|local analysis]]. Here's the idea:
23
23
@@ -29,10 +29,10 @@ Consider swapping the 1st and 2nd tasks. Then, the first observation we should m
29
29
30
30
Going back to our problem, if our order is optimal, then the following must hold:
31
31
$$
32
-
sum f >= sum f prime &=> f_1 >= f_1 prime \
33
-
&=> d_1 >= d_2
32
+
sum f <= sum f prime &=> f_1 <= f_1 prime \
33
+
&=> d_1 <= d_2
34
34
$$
35
-
If we repeat this argument for other indices, we can indeed show that in an optimal order, $d_i >= d_(i + 1)$ for all $i$, as desired.
35
+
If we repeat this argument for other indices, we can indeed show that in an optimal order, $d_i <= d_(i + 1)$ for all $i$, as desired.
36
36
37
37
Note that in this problem, this condition ends up being both necessary and _sufficient_: since there's only one way for this condition to be satisfied, there's no need to worry about finding a suboptimal local minimum.
38
38
However, [this blog](https://codeforces.com/blog/entry/72525) has some educational examples of situations in which we need to be slightly more careful. In particular, it considers the following problem:
0 commit comments