---
id: 1728
lang: en
translation_of: index.md
source_sha256: d6f5958fac1d9001436e491a13ed7384bdb7494b6cd1a3ac50f65794a98b746a
title: "Greedy: Special Topic"
description: "My greedy is way too weak, so I'm summarizing a few models here, in case it's GG for me in the contest."
tags: ["OI","Greedy"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "贪心专题 rendered as “Greedy: Special Topic”."
  - "扰动排序 rendered literally as “perturbation sorting” (sorting justified by an exchange argument)."
  - "右端点左右的同学 kept literal as “the students around the right endpoint”."
  - "Problem names translated: 国王游戏 King's Game, 元旦晚会 New Year's Party, 梦境 Dreamland, 数据备份 Backup, 种树 Planting Trees."
---

## Overview

My greedy is way too <span class="slang" data-zh="菜" data-gloss="cài, “vegetable”. Slang for being bad at something; weak.">weak</span>, so I'm summarizing a few models here, in case it's <span class="slang" data-zh="GG" data-gloss="Gamer shorthand for “good game”, typed when conceding a match. Here: it is over, I am done for.">GG</span> for me in the contest.

<!--more-->

## Perturbation Sorting

The classic example of perturbation sorting is King's Game. You analyze a known ordering of the sequence, and then find the rule for sorting.

## Johnson's Rule

This thing shows up in production scheduling problems. The problem description goes roughly like this (from Luogu):

> A factory receives orders for n products. The n products are processed in two workshops, A and B, and each must be processed in workshop A before it can go to workshop B.
>
> Product i takes Ai and Bi time to process in workshops A and B, respectively. How should the processing order of the n products be arranged so that the total processing time is minimized? The processing time here means the time from starting to process the first product until all products have finished processing in both workshops A and B.

For this problem, define a $d_i$ to help with sorting:

$$
d_i = \begin{cases} -1, a_i < b_i \\ 0, a_i = b_i \\ 1, a_i > b_i \end{cases}
$$

If the $d_i$ differ, sort by $d_i$ first; if they are the same, then for the case $d_i \leq 0$, sort by $a_i$ in ascending order; for the case $d_i > 0$, sort by $b_i$ in descending order.

The code:

```cpp
// P1248.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1010;

struct node
{
    int a_time, b_time, d, id;

    bool operator<(const node &nd) const { return d == nd.d ? min(a_time, nd.b_time) < min(b_time, nd.a_time) : d < nd.d; }
} nodes[MAX_N];

int n;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &nodes[i].a_time), nodes[i].id = i;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &nodes[i].b_time);
        nodes[i].d = nodes[i].a_time < nodes[i].b_time ? -1 : (nodes[i].a_time > nodes[i].b_time ? 1 : 0);
    }
    sort(nodes + 1, nodes + 1 + n);
    long long T_A = nodes[1].a_time, T_B = nodes[1].a_time + nodes[1].b_time;
    for (int i = 2; i <= n; i++)
        T_B = max(T_A + nodes[i].a_time, T_B) + nodes[i].b_time, T_A += nodes[i].a_time;
    printf("%lld\n", T_B);
    for (int i = 1; i <= n; i++)
        printf("%d ", nodes[i].id);
    return 0;
}
```

## Some Greedy Tricks for Segments & Intervals

**Example 1   New Year's Party**

Consider sorting all the segments by right endpoint, then coloring the students around the right endpoint first. This guarantees the largest intersection. (Greedy, man.)

**Example 2   JZOJ???? [Dreamland](../1554-fortuna-oj-aug-7th-group-a/index.en.html)**

Sort the points first, then push the intervals that can fit into a heap, and pop the invalid intervals out of it. At this point you find that choosing the rightmost one is obviously better.

## Some Greedy Using Heaps

**Example 1    [APIO2007] Backup**

This problem uses a linked list to merge the ones picked together, and then uses a heap to take choices back. Another problem like this is the classic example “Planting Trees”.

**Example 2   CF1158A The Party and Sweets**

I wrote a non-intended solution for this problem: namely, with a heap. Consider first putting down the minimum values provided by the boys, then consider assigning some of those minimums to the girls, changing them into the maximums. We can record how many times each boy has been changed, and then just special-case the large and small values.

```cpp
// CF1159C.cpp
#include <bits/stdc++.h>
#define ll long long
#define pr pair<int, int>

using namespace std;

const int MAX_N = 1e5 + 200;

int n, m, ai[MAX_N], bi[MAX_N], cnt[MAX_N];
ll ans;

int main()
{
    int mx_val = 0;
    scanf("%d%d", &n, &m);
    priority_queue<pr> pq;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &ai[i]), ans += 1LL * ai[i] * m, cnt[i] = m, mx_val = max(mx_val, ai[i]);
        pq.push(make_pair(ai[i], i));
    }
    for (int i = 1; i <= m; i++)
    {
        scanf("%d", &bi[i]);
        if (bi[i] < mx_val)
            puts("-1"), exit(0);
    }
    sort(bi + 1, bi + 1 + m);
    for (int i = m; i >= 1; i--)
    {
        while (!pq.empty() && cnt[pq.top().second] < 2 && pq.top().first < bi[i])
            pq.pop();
        if (pq.empty())
            puts("-1"), exit(0);
        pair<int, int> p = pq.top();
        cnt[p.second]--, ans += bi[i] - p.first, pq.pop(), pq.push(p);
    }
    printf("%lld", ans);
    return 0;
}
```
