Problem solution · C++

CCC 2025 S3 - Pretty Pens

CCC 2025 S3 - Pretty Pens: a C++ solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
125 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

Direct simulation

For CCC 2025 S3 - Pretty Pens, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 125 lines of C++ from the credited upstream file ccc25s3.cpp.
  • The implementation visibly relies on ordered lookup.
  • 4 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

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

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2025 S3 - Pretty Pens · C++C++
Use this to learn the idea, then write your own version.
// This solution was submitted in-contest. // However, it uses multisets which is exclusive to C++.// Please upload a solution in the forum if you have a more elegant way to solve this problem.  #include <bits/stdc++.h> #define int long long#define pii pair<long long, long long> using namespace std; int n, m, q; set<pair<int, int>, greater<pair<int, int>>> colors[200001]; int where[200001], pretty[200001]; //where[i] is which set the ith pen is in int totalsum = 0; set<pair<int, int>> row1, row2; pii colfirst, colsecond;  void changeColor(int id, int c) {    int loc = where[id];    int score = pretty[id];      colfirst = *colors[loc].begin();    colsecond = *(++colors[loc].begin());    row1.erase(colfirst);    row2.erase(colsecond);    totalsum -= colfirst.first;     colfirst = *colors[c].begin();    colsecond = *(++colors[c].begin());    row1.erase(colfirst);    row2.erase(colsecond);    totalsum -= colfirst.first;        colors[loc].erase({score, id});    colors[c].insert({score, id});    where[id] = c;       colfirst = *colors[loc].begin();    colsecond = *(++colors[loc].begin());    row1.insert(colfirst);    row2.insert(colsecond);    totalsum += colfirst.first;     colfirst = *colors[c].begin();    colsecond = *(++colors[c].begin());    row1.insert(colfirst);    row2.insert(colsecond);    totalsum += colfirst.first;} void changePretty(int id, int p) {    int loc = where[id];    int score = pretty[id];      colfirst = *colors[loc].begin();    colsecond = *(++colors[loc].begin());    row1.erase(colfirst);    row2.erase(colsecond);    totalsum -= colfirst.first;      colors[loc].erase({score, id});    colors[loc].insert({p, id});    pretty[id] = p;     colfirst = *colors[loc].begin();    colsecond = *(++colors[loc].begin());    row1.insert(colfirst);    row2.insert(colsecond);    totalsum += colfirst.first;}  signed main() {    cin >> n >> m >> q;    for (int i=1; i<=m; i++) {        colors[i].insert({0, 0}); //out of bounds    }     for (int i=1; i<=n; i++) {        int c, p; cin >> c >> p;        where[i] = c;        pretty[i] = p;        colors[c].insert({p, i});    }     for (int i=1; i<=m; i++) {        totalsum += colors[i].begin()->first;        row1.insert(*colors[i].begin());        row2.insert(*(++colors[i].begin()));    }     cout << max(totalsum, totalsum + (--row2.end())->first - row1.begin()->first) << endl;      while (q--) {        int typ; cin >> typ;        int id, x; cin >> id >> x;          if (typ == 1) {            changeColor(id, x);        }        else {            changePretty(id, x);        }         cout << max(totalsum, totalsum + (--row2.end())->first - row1.begin()->first) << 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 ↗