A - Add Sub Mul
输出加减乘的最大值
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int a, b;
int main(){
cin >> a >> b;
cout << max({a + b, a - b, a * b}) << endl;
return 0;
}
B - Cut and Count
将一个字符串分成两个字符串,问两个字符串中共同含有的字符最多有多少
直接用set求即可
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int n;
string s;
int main() {
cin >> n;
cin >> s;
int res = 0;
for (int i = 0; i < n; i++) {
set<char> s1, s2,sR;
for (int j = 0; j < i; j++) {
s1.insert(s[j]);
}
for (int j = i; j < n; j++) {
s2.insert(s[j]);
}
set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
inserter(sR, sR.begin()));
res = max(res, (int)sR.size());
}
cout << res << endl;
return 0;
}
C - Attention
n个人站成一排,有的人向左看有的人向右看
现在要选择一个人,使得其他人都向他看,问最少需要多少个人转换方向
直接处理某个位置左边需要多少人转换方向 右边有多少人需要转换方向即可
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int n, l[N], r[N];
string s;
int main() {
cin >> n >> s;
for (int i = 1; i < n; i++) {
l[i] = l[i - 1] + (s[i - 1] == 'W');
}
for (int i = n - 2; i >= 0; i--) {
r[i] = r[i + 1] + (s[i + 1] == 'E');
}
int res = 0x3f3f3f3f;
for (int i = 0; i < n; i++) {
res = min(l[i] + r[i], res);
}
cout << res << endl;
return 0;
}
D - Xor Sum 2
给出n个数的数组,有多少个子数组满足区间和等于区间异或和
因为如果区间和=区间异或和,那么可以肯定每一位都只被贡献一次,也就是说如果(AxorBxorC=A+B+C)
那么a&b=0,(a+b)&c=0,也就是说如果长度为n的区间区间和等于异或和,那么1到2,3,4,5....n的这n个区间都是满足区间和等于异或和的,然后就可以利用双指针做了
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int n, a[N];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0, j = 0, sum = 0;
LL res = 0;
while(i<n){
while (j < n && ((sum ^ a[j]) == (sum + a[j]))) sum ^= a[j],j++;
res += j - i;
sum ^= a[i];
i++;
}
cout << res << endl;
return 0;
}