---
id: 284
lang: en
translation_of: index.md
source_sha256: 764ba12a81099832bbde461fe85e65b0d199389652f046223ed8558758037916
title: "POJ2442: Sequence — Solution"
description: "Sort every sequence, then merge them two at a time with a priority queue, using a flag so that no candidate sum is generated twice."
tags: ["OI","Priority Queue","Heap"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The inline math $a[1]与b[1]$ contains the Chinese 与 (“and”) inside TeX; math is kept identical, so it stays untranslated."
  - "神奇操作 rendered as “magic trick” with no chip: 神奇 “magical” + 操作 “operation”, read literally."
  - "次次优解 rendered as “the third-best solution”."
---

## Approach and the Magic Trick

For this problem, the first thing we inevitably do is sort every sequence. The sum of the tops of all the sequences is the largest sum, and the second-largest sum is the sum of **the largest sum of $n-1$ sequences** and **the head value of the sequence with the smallest head value**. So let's narrow our view a bit and discuss merging two sequences.

<!--more-->

The optimal solution for two intervals must be $a[1]与b[1]$; of course, both sequences have already been sorted. Then the second-best solution is $max\{a[1]+b[2],a[2]+b[1]\}$. We can split two candidates off one optimal solution. But we find that if we carry this on to the third step, some weird problems come up.

Step three: the third-best solution is $max\{a[2]+b[2],a[2]+b[2],a[1]+b[3],a[3]+b[1]\}$. We find that two of the terms are duplicates, and a comparison can only compare two terms, so duplicates show up very easily. Here we can use the magic trick mentioned in the title: we define an ADT to store, for each solution, its offsets in the two sequences and its value, and we also define a boolean variable $flag$ to tell them apart. Because overlapping solutions appear with a certain period (the period is 2 operations), whenever an operation runs into $flag==true$, we add one fewer solution. If it runs into $flag==false$, we decisively derive two solutions from one.

For the details, take a look at the code:

```cpp
// POJ2442.cpp
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
int T, m, n, seq[110][2020], seqb[2020], tmp[2020];
struct node
{
    int val, l, r;
    bool flag;
    bool operator<(const node &nd) const
    {
        return val > nd.val;
    }
};

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &m, &n);
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
                scanf("%d", &seq[i][j]);
            sort(seq[i] + 1, seq[i] + n + 1);
        }
        for (int i = 1; i <= n; i++)
            seqb[i] = seq[1][i];
        for (int ln = 2; ln <= m; ln++)
        {
            priority_queue<node> npool;
            npool.push(node{seqb[1] + seq[ln][1], 1, 1, false});
            int cnt = 1;
            while (cnt <= n)
            {
                node curt = npool.top();
                npool.pop();
                tmp[cnt++] = curt.val;
                if (!curt.flag)
                    npool.push(node{seqb[curt.l + 1] + seq[ln][curt.r], curt.l + 1, curt.r, false});
                npool.push(node{seqb[curt.l] + seq[ln][curt.r + 1], curt.l, curt.r + 1, true});
            }
            for (int i = 1; i <= n; i++)
                seqb[i] = tmp[i];
        }
        for (int i = 1; i <= n; i++)
            printf("%d ", seqb[i]);
        printf("\n");
    }
    return 0;
}
```
