sourchakcf's blog

By sourchakcf, history, 2 years ago, In English
#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))?

Full text and comments »

  • Vote: I like it
  • -3
  • Vote: I do not like it