Kenken Race
考虑两个人不互相影响的情况,显然只需要判断是否存在两个相邻的障碍即可;
当原来在前面的人要走到后面时,需要存在三个连续的空位来跳过去。
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef wxh010910
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, a, b, c, d;
string s;
cin >> n >> a >> b >> c >> d >> s;
--a;
--b;
--c;
--d;
auto check = [&](int a, int b) {
for (int i = a; i < b; ++i) {
if (s[i] == '#' && s[i + 1] == '#') {
return false;
}
}
return true;
};
if (!check(a, c) || !check(b, d)) {
cout << "No" << "\n";
} else if (c <= d) {
cout << "Yes" << "\n";
} else {
for (int i = b; i <= d; ++i) {
if (i - 1 >= 0 && i + 1 < n && s[i] == '.' && s[i - 1] == '.' && s[i + 1] == '.') {
cout << "Yes" << "\n";
return 0;
}
}
cout << "No" << "\n";
}
return 0;
}
ABC
可以将一次操作看成将 A A A 往后移,不难发现能移就移是最优的,那么倒着扫一遍维护后面 B C BC BC 的个数即可。
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef wxh010910
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
reverse(s.begin(), s.end());
long long ans = 0;
int stage = 0;
int back = 0;
for (auto c : s) {
if (c == 'A') {
if (stage) {
stage = back = 0;
} else {
ans += back;
}
} else if (c == 'B') {
if (stage) {
stage = 0;
back++;
} else {
stage = back = 0;
}
} else {
if (stage) {
back = 0;
} else {
stage = 1;
}
}
}
cout << ans << "\n";
return 0;
}
Tests
显然劣势的考试会选择 l l l ,优势的会选择 u u u 。由于贡献的导数是不降的,所以一定是选若干个喂满,至多有一个不喂;二分答案后判断即可。
#include <bits/stdc++.h>
using namespace std;
struct point {
int b, l, u;
long long s;
bool operator < (const point &other) const {
return s > other.s;
}
};
int main() {
#ifdef wxh010910
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<point> a(n);
long long need = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i].b >> a[i].l >> a[i].u;
need += (long long) a[i].b * a[i].l;
a[i].s = (long long) a[i].l * a[i].b + (long