AcWing 799. 最长连续不重复子序列
Posted MangataTS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AcWing 799. 最长连续不重复子序列相关的知识,希望对你有一定的参考价值。
题目连接
https://www.acwing.com/problem/content/801/
思路
用一个map或者数组vis标记我们以及存在的元素,然后定义两个指针,如果当前这个元素没被访问过,那么直接让r指针往后移动,然后去一个最大值,然后移动l指针到a[r]这个值相同的位置即可
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000009
#define endl "\\n"
#define PII pair<int,int>
// 高精度乘高精 C = A + B, A >= 0, B >= 0
vector<int> Hadd(vector<int> &A, vector<int> &B)
if (A.size() < B.size()) return Hadd(B, A);
vector<int> C;
int t = 0;
for (int i = 0; i < A.size(); i ++ )
t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
if (t) C.push_back(t);
return C;
// 高精减高精 C = A - B, 满足A >= B, A >= 0, B >= 0
vector<int> Hsub(vector<int> &A, vector<int> &B)
vector<int> C;
for (int i = 0, t = 0; i < A.size(); i ++ )
t = A[i] - t;
if (i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if (t < 0) t = 1;
else t = 0;
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
// 高精乘第精 C = A * b, A >= 0, b >= 0
vector<int> Hmul(vector<int> &A, int b)
vector<int> C;
int t = 0;
for (int i = 0; i < A.size() || t; i ++ )
if (i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
// 高精除低精 A / b = C ... r, A >= 0, b > 0
vector<int> Hdiv(vector<int> &A, int b, int &r)
vector<int> C;
r = 0;
for (int i = A.size() - 1; i >= 0; i -- )
r = r * 10 + A[i];
C.push_back(r / b);
r %= b;
reverse(C.begin(), C.end());
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
int dx[4]=0,-1,0,1,dy[4]=-1,0,1,0;
ll ksm(ll a,ll b)
ll ans = 1;
for(;b;b>>=1LL)
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
return ans;
ll lowbit(ll x)return -x & x;
const int N = 2e6+10;
int n,a[N];
map<int,int> vis;
int main()
cin>>n;
for(int i = 1;i <= n; ++i)
cin>>a[i];
int l = 1,r = 1;
int ans = 1;
while(l <= n && r <= n)
while(r <= n && vis[a[r]] == 0)
vis[a[r++]]++;
ans = max(ans,r-l);
while(l <= r && vis[a[r]] != 0)
vis[a[l++]]--;
cout<<ans<<endl;
return 0;
以上是关于AcWing 799. 最长连续不重复子序列的主要内容,如果未能解决你的问题,请参考以下文章