#include <bits/stdc++.h>
using namespace std;
int ARR[1000000+100]={0};
int COLORARR[1000000+100]={0};
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;++i){
cin>>ARR[i];
}
int pos=1,id=1;
while(true){
if(COLORARR[pos]==0){
COLORARR[pos]=id;
pos=ARR[pos];
}
else{
bool fbo=false;
for(int i=1;i<=n;++i)
if(COLORARR[i]==0){
fbo=true;pos=i;break;
}
if(fbo==false)break;
id++;
}
}
unordered_map<int,int>mp;
multimap<int,int>nmp;
for(int i=1;i<=n;++i){
mp[COLORARR[i]]++;
if(i>=2 && COLORARR[i-1]!=COLORARR[i]){
std::pair<int,int>pr(COLORARR[i],COLORARR[i-1]);
bool inbo=true;
for (auto it = nmp.lower_bound(COLORARR[i]); it != nmp.upper_bound(COLORARR[i]); ++it) {
if(it->first==pr.first && it->second==pr.second){
inbo=false;
break;
}
}
if(inbo)
nmp.insert(pr);
}
}
int max_result=0;
for(auto& val:nmp){
max_result=max(max_result,mp[val.first]+mp[val.second]);
}
cout<<max_result<<endl;
return 0;
}
蓝桥杯2024年第十五届省赛真题-传送阵
最新推荐文章于 2025-03-15 11:33:41 发布