这道题的第17个点完美卡掉了 gp_hash_table,第39个点完美卡掉了map,用 cc_hash_talbe 才过,按理来说 1e6 的数据范围完全卡不掉 map 的单 log 和 gp_hash_table 的均摊线性,但是为什么这道题目卡掉了上述两种数据结构?map,gp_hash_talbe,cc_hash_talbe 到底谁快,什么时候快?
代码唯一变化就是 mp 的数据类型
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
const int N=1e6+10;
int a[N],b[N],rk[N];
vector<pair<int,int> > g[N];
int dis[N];
int n;
int w(int x,int y)
{
x=n-x+1,y=n-y+1;
return max(x,y);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) cin>>b[i],rk[b[i]]=i;
cc_hash_table<int,int> mp;
for(int i=n;i;i--)
{
//删掉上面的
int k=i-rk[a[i]];
if(mp.find(k+1)!=mp.end()) g[a[i]].push_back(make_pair(rk[mp[k+1]]-rk[a[i]]+1,mp[k+1]));
else g[a[i]].push_back(make_pair(w(i+1,rk[a[i]])+1,n+1));
if(mp.find(k-1)!=mp.end()) g[a[i]].push_back(make_pair(rk[mp[k-1]]-rk[a[i]],mp[k-1]));
else g[a[i]].push_back(make_pair(w(i,rk[a[i]]+1)+1,n+1));
mp[k]=a[i];
}
if(mp[0]) g[0].push_back(make_pair(rk[mp[0]]-1,mp[0]));
else g[0].push_back(make_pair(n,n+1));
memset(dis,0x3f,sizeof(dis));
std::priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
q.push(make_pair(0,0));
while(q.size())
{
pair<int,int> x=q.top();
q.pop();
if(dis[x.second]<=x.first) continue;
dis[x.second]=x.first;
for(pair<int,int> to:g[x.second]) q.push(make_pair(x.first+to.first,to.second));
}
cout<<dis[n+1];
return 0;
}