1 条题解

  • 0
    @ 2023-3-10 11:35:18

    这题与字符串哈希关系倒不大,主要是说一下哈希表。

    • C加加十四真是好
    • 不用再写哈希表

    当然,这题离散化也能过。目前 unordered_map/set 可以随便用,你也可以期望它有常数的时间复杂度。但心里需要知道,这个自带的哈希是可能会被卡的:详细情况点击这里

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 1000000;
    int n, x;
    int a[MAXN + 5];
    unordered_map<int, int> m;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        m[a[1]] = 1;
        int l = 1, r = 1; // 当前区间
        int ans = 1;      // 最多的雪花数量
        int now = 1;      // 当前区间雪花数量
        for (int i = 2; i <= n; i++)
        {
            r = i;
            if (m[a[i]] == 0)
            {
                // 当前雪花可以直接容纳
                m[a[i]] = i;
                now++;
            }
            else
            {
                // l~m[a[i]] 区间的雪花都要删除,才能容纳当前雪花
                int nxtL = m[a[i]] + 1;
                for (int j = l; j <= nxtL - 1; j++)
                {
                    m[a[j]] = 0;
                    now--;
                }
                l = nxtL;
                m[a[i]] = i;
                now++;
            }
            ans = max(ans, now);
        }
        cout << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    684
    时间
    1000ms
    内存
    512MiB
    难度
    6
    标签
    递交数
    51
    已通过
    18
    上传者