Problem solution · C++

ABC326 C — Peak

ABC326 C — Peak: a C++ solution using sorting and greedy selection. Learn the idea, check the complexity, and read the full code, with credit to michimani AtCoder Solutions.

Technique
Sorting and greedy selection
Source
michimani AtCoder Solutions
Length
65 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sorting and greedy selection

For ABC326 C — Peak, the implementation first exposes a useful order, then scans that order while making locally justified choices.

  1. Choose the key that reveals the greedy or grouping structure.
  2. Sort the relevant records by that key.
  3. Scan in order, maintaining the invariant that makes each local choice safe.

Code notes

  • 65 lines of C++ from the credited upstream file abc326_c.cpp.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • 4 loop blocks detected.

Complexity

Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from michimani AtCoder Solutions by michimani and is used under the MIT licence.

Full codeABC326 C — Peak · C++C++
Use this to learn the idea, then write your own version.
#include <algorithm>#include <iostream>#include <map>#include <vector> using namespace std; int main() {    unsigned long n, m;    cin >> n >> m;    map<unsigned long, unsigned long> presents;    vector<unsigned long> pidx;     for (unsigned long i = 0; i < n; i++) {        unsigned long a = 0;        cin >> a;        if (presents.count(a) == 0) {            presents[a] = 1;            pidx.push_back(a);        } else {            presents[a]++;        }    }     sort(pidx.begin(), pidx.end());     unsigned long ans = 0;    unsigned long remain = n;    unsigned long prev = 0;    unsigned long last_pidx = 0;    for (unsigned long i = 0; i < pidx.size(); i++) {        if (remain < ans) {            break;        }         unsigned long ans_tmp = 0;        if (i == 0) {            for (unsigned long ii = i; ii < pidx.size(); ii++) {                if (pidx[ii] >= pidx[i] + m) {                    break;                }                ans_tmp += presents[pidx[ii]];                last_pidx = ii;            }        } else {            ans_tmp = prev - presents[pidx[i - 1]];            for (unsigned long ii = last_pidx + 1; ii < pidx.size(); ii++) {                if (pidx[ii] >= pidx[i] + m) {                    break;                }                ans_tmp += presents[pidx[ii]];                last_pidx = ii;            }        }         prev = ans_tmp;         if (ans_tmp > ans) {            ans = ans_tmp;        }        remain -= presents[pidx[i]];    }     cout << ans << endl;}

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗