AtCoder Beginner Contest 124
A - Buttons
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int res = 0;
if(a > b) res += a, a --;
else res += b, b --;
if(a > b) res += a, a --;
else res += b, b --;
cout << res << "\n";
return 0;
}
B - Great Ocean View
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
int main() {
int n;
cin >> n;
vi h(n);
for(auto &i : h) cin >> i;
int res = 1, pre = h[0];
for(int i = 1; i < n; i ++){
res += (h[i] >= pre);
pre = max(pre, h[i]);
}
cout << res << "\n";
return 0;
}
C - Coloring Colorfully
动态规划
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
int main() {
string s;
cin >> s;
int n = s.size();
s = " " + s;
vector<array<int,2>> f(n + 1);
for(int i = 1, x; i <= n ; i ++) {
x = s[i] - '0';
f[i][x] = f[i-1][x ^ 1];
f[i][x ^ 1] = f[i-1][x] + 1;
}
cout << min(f[n][0], f[n][1]);
return 0;
}
D - Handstand
双指针
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n, k;
cin >> n >> k;
string s;
cin >> s;
s += '.';
vector<int> a, b;
for (int i = 1, cnt = 1; i < s.size(); i++) {
if (s[i] == s[i - 1]) cnt++;
else {
a.push_back(cnt);
b.push_back(s[i - 1] - '0');
cnt = 1;
}
}
int m = a.size();
int res = 0;
for (int l = 0, r = -1, cnt = 0, len = 0; l < m; l++) {
while (r + 1 < m and cnt + (b[r + 1] == 0) <= k)
r++, cnt += (b[r] == 0), len += a[r];
res = max(res, len);
cnt -= (b[l] == 0), len -= a[l];
}
cout << res << "\n";
return 0;
}