EyeSpirit's blog

By EyeSpirit, history, 2 weeks ago, In English

Hey everyone! I’m relatively new to competitive programming and have been diving into platforms like Codeforces lately. It’s been an exciting but humbling experience, and I’m constantly learning. If you have any advice for improving problem-solving skills or tackling contests efficiently, I’d love to hear it!

Now, let me share a recent puzzling experience I had while solving a problem on Codeforces. The question was about array cloning (a common problem type involving arrays). I spent a lot of time debugging and perfecting my solution in C++17, only to have it rejected 10 times in a row. To my surprise, when I switched to C++20, the very same code passed without issues. The code is given down below you can check for your self.

#include <bits/stdc++.h>
#define ll long long
#define pp pair<int, int>
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> v(n);
        int maxf = 0;
        unordered_map<int, int> mp;
        for (int i = 0; i < n; i++) {
            cin >> v[i];
            mp[v[i]]++;
            maxf = max(maxf, mp[v[i]]);
        }
        int req = n - maxf;
        int step = 0;
        while (maxf < n) {
            maxf *= 2;
            step++;
        }
        step += req;
        cout << step << endl;
    }
}
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
2 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by EyeSpirit (previous revision, new revision, compare).

»
2 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by EyeSpirit (previous revision, new revision, compare).

»
2 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by EyeSpirit (previous revision, new revision, compare).

»
2 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

I believe the issue is test cases were made to TLE default hash of C++ 17 and not C++ 20. Don't use unordered_map or that might happen quite often, use map instead and for this scenario you will get accepted no matter the version of C++ you use.