https://jungol.co.kr/problem/8565?cursor=MjcsMSwxMw==
idx[x]를 x중에서 먼저 나온것의 위치라고 생각합시다. 앞에서부터 보면서 idx[x]가 0이면 idx[x]에 현재 인덱스를 넣어주고, 아니면 (현재 인덱스-idx[x]-1)의 최댓값을 구해주면 됩니다.
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
ll N,i,x,idx[2020]={0},res=0;
cin>>N;
for (i=1;i<=2*N;i++)
{
cin>>x;
if (idx[x]==0) idx[x]=i;
else res=max(res,i-idx[x]-1);
}
cout<<res;
}

| Alternating String (CF Edu R 189 Div.2 - B) (0) | 2026.05.17 |
|---|---|
| A Number Between Two Others (CF Edu R 189 - A) (0) | 2026.05.17 |
| 팬미팅 (JUNGOL 5768) (0) | 2026.05.16 |
| 행복해지려면 몇 개? (JUNGOL 11187) (0) | 2026.05.15 |
| 잔디밭 가꾸기 (JUNGOL 3621) (0) | 2026.05.15 |