贪心
#include <bits/stdc++.h> #define all(a) a.begin(),a.end() #define pb push_back using namespace std; using ll = long long ; int test(int n){} void solve() { int n; cin>>n; vector<ll>a(n+1); for(int i=1;i<=n;i++)cin>>a[i]; long long int ans=0; for(int i=2;i<=n;i++) ans=max(ans,a[i]*a[i-1]); cout<<ans<<"\n"; } int main() { ios::sync_with_stdio(false); int T=1; cin>>T; while(T--) solve(); return 0; }
求i⋅j−k⋅(ai|aj) 的最大值
暴力加剪枝就行
#include <bits/stdc++.h> #define all(a) a.begin(),a.end() #define pb push_back using namespace std; using ll = long long ; int test(){ int n,k; cin>>n>>k; vector<int>a(n+1); for(int i=1;i<=n;i++)cin>>a[i]; int ans=-0x3f3f3f3f; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) ans=max(ans,i*j-k*(a[i]|a[j])); cout<<ans<<endl; } void solve() { int n,k; cin>>n>>k; vector<ll>a(n+1); for(register int i=1,x;i<=n;i++) cin>>a[i]; ll res=-0x3f3f3f3f; for(register ll i=n;i>=1;i--){ for(register ll j=i-1;j>=1;j--){ ll p=1ll*i*j-k*(a[i]|a[j]); if(p>res) res=p; if(i*j<res)break; } } cout<<res<<"\n"; } int main() { //freopen("out.txt","r",stdin); ios::sync_with_stdio(false); int T=1; cin>>T; while(T--) solve(); return 0; }