AtCoder Beginner Contest 124

1|0A - 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; }

2|0B - 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; }

3|0C - 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; }

4|0D - 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; }

__EOF__

本文作者PHarr
本文链接https://www.cnblogs.com/PHarr/p/18218791.html
关于博主:前OIer,SMUer
版权声明CC BY-NC 4.0
声援博主:如果这篇文章对您有帮助,不妨给我点个赞
posted @   PHarr  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示