本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TOC
Codeforces Round #735 (Div. 2)-B. Cobb
传送门 Time Limit: 1 second Memory Limit: 256 megabytes
Problem Description
You are given integers and an integer . Find the maximum value of over all pairs of integers with . Here, is the bitwise OR operator.
Input
The first line contains a single integer () — the number of test cases.
The first line of each test case contains two integers () and ().
The second line of each test case contains integers ().
It is guaranteed that the sum of over all test cases doesn't exceed .
Output
For each test case, print a single integer — the maximum possible value of .
Sample Input
4
3 3
1 1 3
2 2
1 2
4 3
0 1 2 3
6 6
3 2 0 0 5 6
Sample Onput
-1
-4
3
12
Note
Let .
In the first test case,
So the maximum is .
In the fourth test case, the maximum is .
题目大意
给你一个数组和一个正整数,让你从中选取不同的两个数和,计算的最大值。
解题思路
暴力解题需要复杂度会超时,所以需要进行优化。
有没有发现这一题的很特殊,不会超过也不会超过。
那么是什么?就是要减去的那个或结果的系数。
如果不需要减去这么一个,那么和肯定选最大的两个和。
这个“讨厌”的让我们减去了多少呢?最多减去倍的或结果。而,,所以由造成的影响最多不超过。
如果特别大,那么选择后面的元素的话和就会非常大,大约是,与造成的影响相差一个与。
所以和不会小于。也就是说前面数据再多,都是没有意义的。我们只需要考虑后个数据就够了。
分析一下复杂度
每组测试样例复杂度为, 每组数据有个的话最多有组数据, 粗略估算复杂度就是
代码实现就不困难了。
AC代码
记得用 ,需要相乘的也要用
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
ll a[100010];
int main()
{
int N;
cin>>N;
while(N--)
{
int n,k;
cd(n),cd(k);
for(int i=1;i<=n;i++)
scanf("%lld", &a[i]);
ll ans=-1000000000000000000;
for(ll i=n;i>=max(1, n-200);i--)
for(ll j=i-1;j>=max(1, n-200);j--)
ans=max(ans,i*j-k*(a[i]|a[j]));
printf("%lld\n",ans);
}
return 0;
}
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:letmefly.blog.csdn.net/article/det…