AtCoder Beginner Contest 048
A - AtCoder *** Contest
先输出首字母,然后遍历字符串,遇到空格就输出后面的第一个字符。
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false), cin.tie(nullptr); string s; getline(cin, s); cout << s[0]; for (int i = 0; i < s.size(); i++) { if (s[i] == ' ') cout << s[i + 1]; } return 0; }
B - Between a and b ...
如果暴力来做的话肯定会超时。问
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false), cin.tie(nullptr); i64 a, b, x; cin >> a >> b >> x; i64 k1 = (a + x - 1) / x, k2 = b / x; cout << k2 - k1 + 1; return 0; }
C - Boxes and Candies
简单贪心。对于相邻的两个箱子,肯定优先吃右边箱子的糖果,因为其可以兼顾左边和右边。
首先需要注意的是
一开始的做法:WA 了最后四个点。
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int N, x; cin >> N >> x; vector<int> a(N); for (int i = 0; i < N; i++) cin >> a[i]; i64 ans = 0; for (int i = 1; i < N; i++) { i64 sum = a[i] + a[i - 1]; if (sum > x) { i64 t = sum - x; a[i] -= t; ans += t; } } cout << ans; return 0; }
这里犯的错误在于,
代码:
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int N, x; cin >> N >> x; vector<int> a(N); for (int i = 0; i < N; i++) cin >> a[i]; i64 ans = 0; for (int i = 1; i < N; i++) { i64 sum = a[i] + a[i - 1]; if (sum > x) { i64 t = sum - x; if (a[i] >= t) { a[i] -= t; ans += t; } else { a[i] = 0; a[i - 1] -= (t - a[i]); ans += t; } } } cout << ans; return 0; }
D - An Ordinary Game
思维题。
我们发现:当删除到无法删除时,字符串一定是这样的:
哪方操作后率先出现这样的死局,哪方先获胜。当字符串长度为奇数时,首尾字符下标均为奇数,若首尾字符不同则最后一定无法在己方形成死局,己方获胜。反之,当字符串长度为偶数且首尾字符不同时,我做了一步操作之后对方进入必胜态。
当首尾字符相同时,如果字符串长度为奇数我方必败(形成死局),反之则胜。
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false), cin.tie(nullptr); string s; cin >> s; int n = s.size(); if ((n % 2 == 1 && s[0] != s[n - 1]) || (n % 2 == 0 && s[0] == s[n - 1])) cout << "First"; else cout << "Second"; return 0; }
本文作者:pangyou3s
本文链接:https://www.cnblogs.com/pangyou3s/p/18374849
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
标签:
,
,
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步