# [CodeForces] Dirty Deeds Done Dirt Cheap

Problem

First I thought about modelling this problem as a directed graph, where between each pair of nodes, an edge represents a valid < > or > < transition. Then do a dfs with dp to compute the longest valid path. But there can be as many as O(N^2) edges.

By drawing some examples, I made the following observations:

1. there are 2 types of integer pairs, either (a > b) or (a < b).

2. we will never mix these 2 types together in any valid path. For (a < b), the next pair must be (c, d) such that b > c,  c < d. So we separate these 2 types and consider them separately.

3. For each type, greedy works: For type a < b, pick max(b) first; for type a > b, pick min(b) first. Doing this allows us to use all pairs of the same type.

Proof: For type a < b, say we have (L1, R1), (L2, R2), (L3, R3), without the loss of generality, let's assume R1 > R2 > R3. So R1 is the max, R2 is the second max and R3 is the third max and we have R1 > R2 > L2, R2 > R3 > L3, we can connect all 3 pairs in this order.

posted @ 2022-01-20 01:05  Review->Improve  阅读(37)  评论(0编辑  收藏  举报