CF - Preparing for Mergesort

Author: Andi Qu

Table of Contents

Code
Edit on Github

Simply simulating the process described is potentially , so we can't just do that. What if instead of finding the sequences one at a time, we find them all at the same time?

Imagine that we already know the sequences for the first numbers and we would like to insert the -th number into one of the sequences.

Which sequence should we insert this number into? We should insert it into the first sequence where the last number of this sequence is less than the -th number.

The key observation in this problem is that the last numbers of the existing sequences form a non-increasing sequence. (To prove this, use a proof by contradiction - what if there is an increase at some point?)

This means that we can simply binary search for the sequence that we should insert the -th number into!

This algorithm runs in time.

Code

1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 ios_base::sync_with_stdio(0);
6 cin.tie(0);
7 int n;
8 cin >> n;
9 vector<vector<int>> ans;
10 while (n--) {

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

Give Us Feedback on CF - Preparing for Mergesort!