模拟
AC代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<string> a(3);
for (int i = 0; i < 3; i++) {
cin >> a[i];
}
cout << "A" << a[1][0] << "C\n";
return 0;
}
计算出[a,b]内能被x整除的上下界
AC代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
LL a, b, x;
cin >> a >> b >> x;
LL c = (a + x - 1) / x, d = b / x;
cout << max(0LL, d - c + 1) << '\n';
return 0;
}
贪心,如果当前糖果和前一个糖果的总和大于x,则优先减少当前糖果的数量,多余的再减少前一个糖果的数量,保证对后面的计算更有利
AC代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
LL x;
cin >> n >> x;
vector<LL> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
LL ans = 0;
for (int i = 2; i <= n; i++) {
if (a[i] + a[i - 1] > x) {
ans += (a[i] + a[i - 1] - x);
if (a[i] + a[i - 1] - x >= a[i]) {
a[i] = 0;
} else {
a[i] -= a[i] + a[i - 1] - x;
}
}
}
cout << ans << '\n';
return 0;
}
博弈,因为只能拿走中间的字符,并且保证相邻的两个字符没有相同的,考虑极端情况ababa,此时两头字符相等,先手拿任何一个都不可以所以后手赢,再有ababab,两头字符不相等,先手拿任何一个都不可以,后手仍然赢。考虑一般情况,如果两头字符相等,最多就是拿完所有中间n-2个字符,所以如果字符长度为奇数,后手赢,否则先手赢,如果两头字符不相等,最多也是拿完所有中间n-2个字符,最后的情况是没有字符可以拿,而不是相邻两个字符相等,所以字符长度为奇数,先手赢,否则后手赢
AC代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
int len = s.size();
if (s[0] == s[len - 1]) {
if (len % 2 == 0) {
cout << "First\n";
} else {
cout << "Second\n";
}
} else {
if (len % 2 == 0) {
cout << "Second\n";
} else {
cout << "First\n";
}
}
return 0;
}