Skip to content

Commit 53344b2

Browse files
committed
Quartz sync: Oct 18, 2025, 3:40 PM
1 parent ec77d35 commit 53344b2

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

content/posts/flint-and-steel.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,6 @@ Now, the problem reduces to finding an _unordered_ subset of minimal size that s
7171
+ Together, the chosen detonation intervals must kill all creepers.
7272
+ No two detonation intervals can contain each others' centers.
7373

74-
This is easily done with #dp + #segment-tree in $cal(O)(n log n)$.
74+
This is easily done with #dp + #segment-tree in $cal(O)(n log n)$.
75+
76+
[Implementation](https://codeforces.com/contest/2133/submission/341419217)

content/posts/orders.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,38 @@ title: Orders
33
tags:
44
- cs
55
---
6-
A common problem archetype is the following: "given $n$ tasks, order them in some optimal way."
6+
A common problem archetype is the following: "given $n$ tasks, order them in some optimal way" or "find an order of $n$ tasks satisfying a certain condition". I will outline some common methods to solve these problems below.
7+
## Total Orders
8+
9+
In many cases, it will be possible to define a *total order* on the tasks; that is, it's possible to assign each task a "priority" such that it's always optimal to do high-priority tasks before
10+
low-priority ones.
11+
12+
An example of a non-total order is rock-paper-scissors, because the three moves can't be
13+
placed into a total order (e.g. rock > paper > scissors) due to their cyclic nature.
14+
15+
> [!info] Tasks and Deadlines
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+
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.
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 do this, but one particularly nice way is the _exchange argument_.
21+
#### Exchange Argument
22+
This idea is an example of [[content/thoughts/local-analysis|local analysis]]. Here's the idea:
23+
24+
Suppose we've found an optimal ordering. Then, swapping any two adjacent elements in this ordering certainly _cannot_ be better, otherwise our assumption of optimality would be contradicted. This simple, local condition can often help us greatly reduce the complexity of ordering problems.
25+
26+
Let's apply this idea to the CSES problem above. Say we've ordered the tasks in a way such that the $i$th task has duration $d_i$ and is completed at time $f_i$. Then, how does our exchange condition constrain the relationship between $d_1$ and $d_2$?
27+
28+
Consider swapping the 1st and 2nd tasks. Then, the first observation we should make is that $f_2, f_3,..., f_n$ all remain unchanged. Therefore, we only need to consider how $f_1$ changes! This is why considering adjacent swaps in particular is so useful: in many cases, it will leave the suffix unchanged, leading to a much simpler analysis
29+
30+
Going back to our problem, if our order is optimal, then the following must hold:
31+
$$
32+
sum f >= sum f prime &=> f_1 >= f_1 prime \
33+
&=> d_1 >= d_2
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.
36+
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.
738

839
## Problems
940
[[posts/flint-and-steel|D2F: Flint and Steel]]

content/thoughts/local-analysis.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Local Analysis
3+
---
4+
See "Local" section in [OTIS](https://web.evanchen.cc/textbooks/OTIS-Excerpts.pdf)

0 commit comments

Comments
 (0)