Skip to content

Instantly share code, notes, and snippets.

@murilomaeda
Created August 15, 2023 21:10
Show Gist options
  • Save murilomaeda/bf104b9be39b0fddbbfab6587d349561 to your computer and use it in GitHub Desktop.
Save murilomaeda/bf104b9be39b0fddbbfab6587d349561 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
//Declaro o tamanho do vetor
const int MAXN = 1e5 + 10;
//Vetor que guarda os números na lista
int v[MAXN];
//Guardo quantas vezes um certo número já apareceu no segmento atual
int marc[MAXN];
int main()
{
cin.tie(0)->sync_with_stdio(0);
//Leio a entrada
int n; cin >> n;
for(int i = 0; i < n; i++)
cin >> v[i];
// inicializo os limites no primeiro elemento do vetor
int l = 0, r = 0;
marc[v[0]]++;
//guardo a melhor resposta até agora
int resp = 0;
while(l <= r && r < n-1)
{
//aumento o segmento para a direita
r++;
//aumento o número de aparições do número que adicionei
marc[v[r]]++;
while(marc[v[r]] > 1)
{
//vou tirando os elementos da esquerda
marc[v[l]]--;
l++;
}
//checo se o intervalo atual é melhor que o melhor até agora
resp = max(resp, r - l + 1);
}
cout << resp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment