这里是 LGP5502 个人题解。
显然可以枚举右端点 , 表示 。
对于每次右移, 更新 数组。
时间直接 了pwp。
怎么办呢?发现性质:。
-
, 发现左端点显然不能去 (更新后也不行)。
-
,此时 。
用队列表示当前的 ,重复的只算入最左边的点,由于 ,队列长度不到 。
加上 时间,我们不需要使用分治算法,即可在 的时间复杂度通过本题。
代码如下:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=100010;
int n,a[maxn],ans;
queue<pair<int,int> >q;
queue<pair<int,int> >qq;//用两个队列遍历队内元素
void solve(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i],ans=max(ans,a[i]);
for(int i=1;i<=n;i++){
int pre=0;
while(!q.empty()){
int tmp=q.front().first;
int d=q.front().second;
q.pop();
tmp=__gcd(tmp,a[i]);
ans=max(ans,(i-d+1)*tmp);
if(pre!=tmp)qq.push({tmp,d});
pre=tmp;
}
while(!qq.empty())q.push(qq.front()),qq.pop();
q.push({a[i],i});
}
cout<<ans<<'\n';
}
signed main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int T=1;
// cin>>T;
while(T--)solve();
return 0;
}