Codeforces Round #735 (Div. 2) A~D
A
You are given 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an. Find the maximum value of 𝑚𝑎𝑥(𝑎𝑙,𝑎𝑙+1,…,𝑎𝑟)⋅𝑚𝑖𝑛(𝑎𝑙,𝑎𝑙+1,…,𝑎𝑟)max(al,al+1,…,ar)⋅min(al,al+1,…,ar) over all pairs (𝑙,𝑟)(l,r) of integers for which 1≤𝑙<𝑟≤𝑛1≤l<r≤n.
Input
The first line contains a single integer 𝑡t (1≤𝑡≤100001≤t≤10000) — the number of test cases.
The first line of each test case contains a single integer 𝑛n (2≤𝑛≤1052≤n≤105).
The second line of each test case contains 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖≤1061≤ai≤106).
It is guaranteed that the sum of 𝑛n over all test cases doesn't exceed 3⋅1053⋅105.
Output
For each test case, print a single integer — the maximum possible value of the product from the statement.
Example
input
Copy
4
3
2 4 3
4
3 2 3 1
2
69 69
6
719313 273225 402638 473783 804745 323328
output
Copy
12
6
4761
381274500335
不妨枚举选择的最大的数,那么当前最优解一定在其两边(如果次大的数在其两边那就直接选,如果不在的话也只能选其两边的数),不断更新答案即可。比如3 4 1 2 5,如果枚举到4,想要选5的话1就会包含在区间里就不得不选1,那么区间最小值就是1了;再比如1 2 3 6 4 5,枚举到6如果想选5那4就会包含在区间里。
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
int n, a[200005];
int main() {
int t;
cin >> t;
while(t--) {
cin >> n;
long long maxx = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for(int i = 2; i <= n - 1; i++) {
maxx = max(1ll * maxx, 1ll * a[i] * a[i - 1]);
maxx = max(1ll * maxx, 1ll * a[i] * a[i + 1]);
}
if(n == 2) cout << 1ll * a[1] * a[2] << endl;
else cout << maxx << endl;
}
return 0;
}
B.
You are given 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an and an integer 𝑘k. Find the maximum value of 𝑖⋅𝑗−𝑘⋅(𝑎𝑖|𝑎𝑗)i⋅j−k⋅(ai|aj) over all pairs (𝑖,𝑗)(i,j) of integers with 1≤𝑖<𝑗≤𝑛1≤i<j≤n. Here, || is the bitwise OR operator.
Input
The first line contains a single integer 𝑡t (1≤𝑡≤100001≤t≤10000) — the number of test cases.
The first line of each test case contains two integers 𝑛n (2≤𝑛≤1052≤n≤105) and 𝑘k (1≤𝑘≤min(𝑛,100)1≤k≤min(n,100)).
The second line of each test case contains 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (0≤𝑎𝑖≤𝑛0≤ai≤n).
It is guaranteed that the sum of 𝑛n over all test cases doesn't exceed 3⋅1053⋅105.
Output
For each test case, print a single integer — the maximum possible value of 𝑖⋅𝑗−𝑘⋅(𝑎𝑖|𝑎𝑗)i⋅j−k⋅(ai|aj).
Example
input
Copy
4
3 3
1 1 3
2 2
1 2
4 3
0 1 2 3
6 6
3 2 0 0 5 6
output
Copy
-1
-4
3
12
比赛时读完题发现没什么好的思路,但是注意到k的范围和a的范围都比较特殊,在n > 100的时候k直接取100,因此考虑最大数据范围时i * j大约在1e10的数量级,而k * (ai | aj)最大也就在1e7,负贡献相比正贡献而言很小,想到从大往小暴力小范围其实就能得到最优解。详细证明见题解。
#include <bits/stdc++.h>
#define pb push_back
#define int long long
using namespace std;
int n, k, a[300005];
//注意a[i]和k的范围
int f[100005], mx[100005];
signed main() {
int t;
cin >> t;
while(t--) {
cin >> n >> k;
for(int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
mx[a[i]] = i;
}
int ans = -0x3f3f3f3f;
if(n <= 100) {
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;
} else {
for(int i = n - 1; i >= max(n - 250, 1ll); i--) {
for(int j = i + 1; j <= n; j++) {
ans = max(ans, i * j - k * (a[i] | a[j]));
}
}
cout << ans << endl;
}
}
return 0;
}
C.
You are given two integers 𝑛n and 𝑚m. Find the MEXMEX of the sequence 𝑛⊕0,𝑛⊕1,…,𝑛⊕𝑚n⊕0,n⊕1,…,n⊕m. Here, ⊕⊕ is the bitwise XOR operator.
MEXMEX of the sequence of non-negative integers is the smallest non-negative integer that doesn't appear in this sequence. For example, MEX(0,1,2,4)=3MEX(0,1,2,4)=3, and MEX(1,2021)=0MEX(1,2021)=0.
Input
The first line contains a single integer 𝑡t (1≤𝑡≤300001≤t≤30000) — the number of test cases.
The first and only line of each test case contains two integers 𝑛n and 𝑚m (0≤𝑛,𝑚≤1090≤n,m≤109).
Output
For each test case, print a single integer — the answer to the problem.
Example
input
Copy
5
3 5
4 6
3 2
69 696
123456 654321
output
Copy
4
3
0
640
530866
这个题的意思实际上是找到一个大于m的数k使得n ^ k = x 最小(因为小于等于m的k与n异或已经出现过了)当然由异或的性质,n ^ k = x 等价于 n ^ x = k 也可以说找到一个最小的x使得n ^ x = k >= m + 1。考虑从高位到低位逐位贪心地进行构造即可,详情见注释。
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
int n, m;
int main() {
int t;
cin >> t;
while(t--) {
int n, m;
cin >> n >> m;
//找到一个大于m的数k使得n ^ k = x 最小(因为小于等于m的k与n异或已经出现过了)
//当然n ^ k = x 等价于 n ^ x = k 也可以说找到一个最小的x使得n ^ x = k >= m + 1
m++;
int x = 0;
//ni == 1 mi == 0 可以让xi = 0
//ni == 1 mi == 1 可以让xi = 0
//ni == 0 mi == 1 此时没有break的话 必须让xi = 1
//ni == 0 mi == 0 可以让xi = 0
for(int i = 30; i >= 0; i--) {
int p = ((n >> i) & 1), q = ((m >> i) & 1);
if((n ^ x) >= m) break;//后面x全取0即可
if(p == q) {
continue;
}
if(q) {//此时没有break
x |= (1 << i);
}
}
cout << x << endl;
}
return 0;
}
D.
You are given an integer 𝑛n. Find any string 𝑠s of length 𝑛n consisting only of English lowercase letters such that each non-empty substring of 𝑠s occurs in 𝑠s an odd number of times. If there are multiple such strings, output any. It can be shown that such string always exists under the given constraints.
A string 𝑎a is a substring of a string 𝑏b if 𝑎a can be obtained from 𝑏b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Input
The first line contains a single integer 𝑡t (1≤𝑡≤5001≤t≤500) — the number of test cases.
The first line of each test case contains a single integer 𝑛n (1≤𝑛≤1051≤n≤105).
It is guaranteed that the sum of 𝑛n over all test cases doesn't exceed 3⋅1053⋅105.
Output
For each test case, print a single line containing the string 𝑠s. If there are multiple such strings, output any. It can be shown that such string always exists under the given constraints.
Example
input
Copy
4
3
5
9
19
output
Copy
abc
diane
bbcaabbba
youarethecutestuwuu
一开始想到了正解但没有完全想到。。后来就没时间了。
首先不难发现对于连续n个相同字符x,x出现n次,xx出现n - 1次,xxx出现n - 2次...是奇偶交替的。那么怎么构造呢?考虑奇数+偶数等于奇数!只需要再来一段连续n - 1个字符,中间用其他字符分隔开,就能保证每个子串出现的次数为奇数了。
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
if(n == 1) puts("a");
else if(n == 2) puts("jb");
else {
if(n & 1) {
int k = n / 2;
for(int i = 1; i <= k; i++) cout << 'a';
cout << "bc";
for(int i = 1; i <= k - 1; i++) cout << 'a';
cout << endl;
} else {
int k = n / 2;
for(int i = 1; i <= k; i++) cout << 'a';
cout << 'b';
for(int i = 1; i <= k - 1; i++) cout << 'a';
cout << endl;
}
}
}
}