# 最长连续不重复 - 双指针
#include <iostream>
const int N = 1e6 + 10;
int n;
int a[N], s[N];
using namespace std;
int main(){
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
int res = 0;
for(int i = 0, j = 0; i < n; i++){
s[a[i]]++;
while(s[a[i]] > 1){
s[a[j]] --;
j++;
}
res = max(res, i - j + 1);
}
cout << res << endl;
return 0;
}
归并排序也算一种双指针算法
# 原算法模板
for(i = 0, j = 0; i < n; i++){
while(j < i && check(i, j)) j++;
//check(i, j)指满足某一种性质
//下面是每道题目的具体逻辑
}
#include <iostream>
const int N = 1e6 + 10;
int n;
int a[N], s[N];
using namespace std;
int main(){
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
int res = 0;
for(int i = 0, j = 0; i < n; i++){
s[a[i]]++;
while(s[a[i]] > 1){
s[a[j]]--;
j++;
}
res = max(res, i - j + 1);
}
cout << res << endl;
return 0;
}