# 双指针算法总结

for (i = 0, j = 0; i < n; i++)
{
while (j < i && check(i, j)) j++;

// 每道题目的具体逻辑
}

https://www.acwing.com/problem/content/801/

#include <iostream>

using namespace std;

const int N = 100010;

int n;
int a[N], s[N];

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;
}

https://www.acwing.com/problem/content/802/

#include <iostream>

using namespace std;

const int N = 100010;

int n, m, x;
int a[N], b[N];

int main()
{
scanf("%d%d%d", &n, &m, &x);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < m; i++) scanf("%d", &b[i]);

for (int i = 0, j = m - 1; i < n; i++)
{
while (j >= 0 && a[i] + b[j] > x) j--;
if (a[i] + b[j] == x)
{
printf("%d %d\n", i, j);
break;
}
}

return 0;
}

https://www.acwing.com/problem/content/2818/

#include <iostream>

using namespace std;

const int N = 100010;

int n, m;
int a[N], b[N];

int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < m; i++) scanf("%d", &b[i]);

int i = 0, j = 0;
while (i < n && j < m)
{
if (a[i] == b[j]) i++;
j++;
}

if (i == n) puts("Yes");
else puts("No");

return 0;
}

posted @ 2023-11-30 22:31  ykycode  阅读(27)  评论(0编辑  收藏  举报