典型的动态区间维护值
// 数学公式要变形
// 莫急莫急先读题
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) (x&(-x))
#define endl "\n"
#define ios ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
#define LF(x) fixed<<setprecision(x)// c++ 保留小数
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N=1000010,M=1010,F=2*N,INF=0x3f3f3f3f,pp=13331;
const double pai=acos(-1.0);// pai
int t,n,m;
int a[N],cnt[N];
int res,ans;
void solve()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1,j=1;i<=n;i++)
{
if(cnt[a[i]]==0) cnt[a[i]]++,ans++;
else cnt[a[i]]++;
while(ans>2)
{
cnt[a[j]]--;
if(!cnt[a[j]]) ans--;
j++;
}
res=max(res,i-j+1);
}
cout<<res<<endl;
return ;
}
// 万事读题为先
int main ()
{
ios// 不能有printf puts scanf
solve();
return 0;
}
周赛动态变化的好题
// 数学公式要变形
// 莫急莫急先读题
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) (x&(-x))
#define endl "\n"
#define ios ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
#define LF(x) fixed<<setprecision(x)// c++ 保留小数
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N=1000010,M=1010,F=2*N,INF=0x3f3f3f3f,pp=13331;
const double pai=acos(-1.0);// pai
LL t,n,m;
map<LL,LL> mp1,mp2;
LL a[N];
void solve()
{
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
mp2[a[i]]++;
}
LL ans=0;
for(int i=1;i<=n;i++){
LL x=a[i];
mp2[x]--;
if(x%m==0&&mp2.count(x*m)&&mp1.count(x/m)){
ans+=mp2[x*m]*mp1[x/m];
}
mp1[x]++;
}
cout<<ans<<endl;
return ;
}
// 万事读题为先
int main ()
{
ios// 不能有printf puts scanf
solve();
return 0;
}