Cod sursa(job #3255584)

Utilizator daryufDaryuF daryuf Data 11 noiembrie 2024 12:11:01
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.68 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("3secv.in");
ofstream out("3secv.out");
//#define cin in
//#define cout out
int main()
{
    int t;
    cin >> t;

    for(; t; t--)
    {
        int n;
        cin >> n;

        // dist[i] = care e a i-a valoare distincta
        // lst[i] = unde a aparut ultima oara

        int dist[3];
        int lst[3];
        for(int i = 0; i <= 2; i++)
        {
            dist[i] = -1;
            lst[i] = -1;
        }

        int smallest = 0;
        int ans = 0;
        for(int i = 1; i <= n; i++)
        {
            int x;
            cin >> x;
            bool ok = 0;
            for(int j = 0; j <= 2; j++)
                if(dist[j] == x)
                {
                    ok = 1;
                    lst[j] = i;
                }
            if(ok == 0)
            {
                for(int j = 0; j <= 2; j++)
                    if(dist[j] == -1)
                    {
                        dist[j] = x;
                        lst[j] = i;
                        ok = 1;
                        break;
                    }
                if(ok == 0)
                {
                    int smallest_last = 0;
                    for(int j = 1; j <= 2; j++)
                        if(lst[j] < lst[smallest_last])
                            smallest_last = j;
                    smallest = lst[smallest_last];
                    dist[smallest_last] = x;
                    lst[smallest_last] = i;
                    ok = 1;
                }
            }
            ans = max(ans, i - smallest);
        }

        cout << ans << '\n';
    }

    return 0;
}