[ICPC 2025 Shanghai R] Round screws 题解
Blue_Fish_Junly · · 题解
下文中默认
首先在序列左右侧插入一个
定义
由于
这里使用根号分治。定义
定义
插入时,
访问时,
均衡
#include<bits/stdc++.h>
using namespace std;
const long long inf=1e18;
int T,n,c,a,lo,hi;
long long g[513][513],f;
inline void solve(){
for(int i=0; i<512; i++) for(int j=0; j<512; j++) g[i][j]=inf;
cin>>n>>c;
for(int i=0; i<=n+1; i++){
if(i==0 or i==n+1) a=0;
else cin>>a;
lo=a&((1<<9)-1), hi=((a-lo)>>9), f=inf;
if(i==0) f=0;
else for(int j=0; j<512; j++) f=min(f,g[j][lo]+((j^hi)<<9)+1ll*(i-1)*c);
if(i==n+1) cout<<f<<"\n";
else for(int j=0; j<512; j++) g[hi][j]=min(g[hi][j],f+(j^lo)-1ll*i*c);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin>>T; while(T--) solve();
return 0;
}