CF1554B Cobb
B. Cobb
这个题比较巧妙。
首先我们考虑 \(n\) 比较大的时候,由于 \(a_i\leq n\),答案一定是 \(n^2\) 级别的。随着 \(n\) 不断减小的过程,答案被更新的概率不断减小,所以我们可以用一个时间复杂度不严格为 \(O(n^2)\) 的算法通过此题。
其实这个剪枝很好想,我们枚举 \(i\) 时,从大向小枚举,再枚举 \(i*j<ans\) 时(\(ans\) 为当前答案),不再枚举即可。
//Don't act like a loser.
//This code is written by huayucaiji
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read() {
char ch=getchar();
int f=1,x=0;
while(ch<'0'||ch>'9') {
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
return f*x;
}
const int MAXN=1e5+10;
int n,k,ans;
int a[MAXN];
signed main() {
int t=read();
while(t--) {
cin>>n>>k;
ans=-1e18;
for(int i=1;i<=n;i++) {
a[i]=read();
}
for(int i=n;i;i--) {
for(int j=i-1;j;j--) {
if(i*j<=ans) {
break;
}
ans=max(ans,i*j-k*(a[i]|a[j]));
}
}
cout<<ans<<endl;
}
return 0;
}