Need help with the problem 1642B. Power Walking

Revision en1, by sourchakcf, 2022-04-28 10:20:28
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long int ln;
 
int main ()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
 
    int t = 0;
    cin >> t;
 
    while (t--)
    {
        int n = 0;
        cin >> n;
 
        unordered_map <int, int> u;
 
        for (int i = 0; i < n; i++)
        {
            int val = 0;
            cin >> val;
 
            u[val] += 1;
        }
 
        int count = u.size();
 
        for (int i = 1; i <= n; i++)
        {
            cout << max (i, count) << " ";
        }
 
        cout << endl;
    }
 
    return 0;
}

The above code is giving TLE for Test case 7.

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long int ln;
 
int main ()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
 
    int t = 0;
    cin >> t;
 
    while (t--)
    {
        int n = 0, count = 0;
        cin >> n;
 
        map <int, int> u;
 
        for (int i = 0; i < n; i++)
        {
            int val = 0;
            cin >> val;
 
            u[val] += 1;
        }
 
        for (auto x : u)
        {
            count += 1;
        }
 
        for (int i = 1; i <= n; i++)
        {
            cout << max (i, count) << " ";
        }
 
        cout << endl;
    }
 
    return 0;
}

But the above code is accepted.

Doesn't an unordered map take amortized o(1) whereas a map takes o(log(n))?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English sourchakcf 2022-04-28 10:20:28 1865 Initial revision (published)