ZJCPC 2022 浙江省赛
B. JB Loves Comma
题目大意
给定字符串S,在每个“cjb”子串后面添加一个逗号
解题思路
py直接替换“cjb”为“cjb,”
代码实现
print(input().replace("cjb", "cjb,"))
C. JB Wants to Earn Big Money
题目大意
n人会买不低于x的股票,m人会卖不高于x的股票,问会有多少人参与交易
解题思路
直接比大小统计答案
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n, m, x, ans = 0;
std::cin >> n >> m >> x;
for (int i = 0; i < n; i++) {
int p;
std::cin >> p;
ans += p >= x;
}
for (int i = 0; i < m; i++) {
int p;
std::cin >> p;
ans += p <= x;
}
std::cout << ans << "\n";
}
A. JB Loves Math
题目大意
给你两个数ab,你可以选定一个正奇数x和正偶数y,让a+x或a-y,问让a变成b的最小操作次数
解题思路
按ab大小奇偶关系分类讨论
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int tt;
std::cin >> tt;
while (tt--) {
int a, b;
std::cin >> a >> b;
if (a > b) {
if ((a - b) % 2) {
std::cout << 2 << "\n";
} else {
std::cout << 1 << "\n";
}
} else if (a == b) {
std::cout << 0 << "\n";
} else {
if ((a - b) % 2) {
std::cout << 1 << "\n";
} else {
if ((b - a) / 2 % 2) {
std::cout << 2 << "\n";
} else {
std::cout << 3 << "\n";
}
}
}
}
}
L. Candy Machine
题目大意
给定一个数组,你可以选择一个子序列,然后得到严格大于平均值的数字,问最多能得到多少个数字
解题思路
由于是选择子序列,答案一定是排序后数组的某个前缀,因为加入一些较小的数字一定不会让答案更劣,因此直接遍历记录最大的答案即可
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
i64 n, sum = 0, ans = 0;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::sort(a.begin(), a.end());
for (int i = 0; i < n; i++) {
sum += a[i];
i64 pos = i - (std::lower_bound(a.begin(), a.begin() + i, sum / (i + 1) + 1) - a.begin()) + 1;
ans = std::max(ans, pos);
}
std::cout << ans << "\n";
}
G. Easy Glide
题目大意
给定初始位置S和目标位置T,并给定n个加速点,无加速的固定速度为V1,遇到一个加速点可以在接下来3秒加速为V2,求从S到T的最短时间
解题思路
分别计算各点花费的时间作为边权,最后用dij跑一遍最短路即可
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int stx, sty, edx, edy, v1, v2;
double time1(i64 x1, i64 y1, i64 x2, i64 y2) {
double time = std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if (time <= v2 * 3) {
return time / v2;
} else {
return (time - v2 * 3) / v1 + 3;
}
}
double time2(i64 x1, i64 y1, i64 x2, i64 y2) {
double time = std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return time / v1;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n;
std::cin >> n;
std::vector<std::array<int, 2>> xy(n + 2);
for (int i = 1; i <= n; i++) {
std::cin >> xy[i][0] >> xy[i][1];
}
std::cin >> stx >> sty >> edx >> edy >> v1 >> v2;
xy[0][0] = stx, xy[0][1] = sty;
xy[n + 1][0] = edx, xy[n + 1][1] = edy;
std::vector<std::vector<double>> g(n + 2, std::vector<double>(n + 2, 4e18));
for (int i = 0; i <= n + 1; i++) {
for (int j = 0; j <= n + 1; j++) {
if (i != j) {
if (i == 0) {
g[i][j] = time2(stx, sty, xy[j][0], xy[j][1]);
} else if (j == n + 1) {
g[i][j] = time1(xy[i][0], xy[i][1], edx, edy);
} else {
g[i][j] = time1(xy[i][0], xy[i][1], xy[j][0], xy[j][1]);
}
}
}
}
std::priority_queue<std::pair<double, int>, std::vector<std::pair<double, int>>, std::greater<>> pq;
std::vector<double> time(n + 2, 4e18);
time[0] = 0.0;
pq.push({time[0], 0});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (time[u] == d) {
for (int v = 0; v <= n + 1; v++) {
if (time[u] + g[u][v] < time[v]) {
time[v] = time[u] + g[u][v];
pq.push({time[v], v});
}
}
}
}
std::cout << std::fixed << std::setprecision(6) << time[n + 1] << "\n";
}
M. BpbBppbpBB
题目大意
给你两种类型的三个图案,再给你一幅图,请你统计两种类型的图案各有多少
解题思路
C类型为双孔,S类型为单孔,因此只需要统计每个单孔周围是否为双孔的某一部分来判断这个孔是属于C还是S即可
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n, m;
std::cin >> n >> m;
std::vector<std::string> g(n);
for (int i = 0; i < n; i++) {
std::cin >> g[i];
}
std::map<std::array<int, 2>, int> mp;
for (int i = 0; i < n - 5; i++) {
for (int j = 0; j < m - 5; j++) {
if (g[i].substr(j, 6) == "######" && g[i + 1].substr(j, 6) == "##..##" && g[i + 2].substr(j, 6) == "#....#" && g[i + 3].substr(j, 6) == "#....#" && g[i + 4].substr(j, 6) == "##..##" &&
g[i + 5].substr(j, 6) == "######") {
mp[{i, j}] = 1;
}
}
}
int cntS = 0, cntC = mp.size();
for (auto [xy, p] : mp) {
if (!p) {
continue;
}
if (mp.count({xy[0], xy[1] + 7}) && mp[{xy[0], xy[1] + 7}]) {
cntS++;
mp[xy] = 0;
mp[{xy[0], xy[1] + 7}] = 0;
cntC -= 2;
}
if (mp.count({xy[0] + 7, xy[1]}) && mp[{xy[0] + 7, xy[1]}]) {
cntS++;
mp[xy] = 0;
mp[{xy[0] + 7, xy[1]}] = 0;
cntC -= 2;
}
}
std::cout << cntS << " " << cntC << "\n";
}
I. Barbecue
题目大意
给定一个字符串,p和f多次在lr区间的子串进行游戏,每次可以去掉子串的开头或者结尾,轮到谁操作时是一个回文串则他会输掉游戏,p先手,问谁会获胜
解题思路
首先用字符串哈希快速判断子串是否一开始就是回文串,否则只需要判断子串长度的奇偶性
代码实现
#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
const int MOD1 = 1e9 + 7, MOD2 = 1e9 + 9;
class StringHash {
public:
int P1, P2;
std::vector<u64> h1, h2, p1, p2;
StringHash() {
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
// P1 = std::uniform_int_distribution<int>(128, 10000)(rng);
// P2 = std::uniform_int_distribution<int>(128, 10000)(rng);
P1 = 131;
P2 = 1331;
}
template <typename Sequence>
void build(const Sequence& seq) {
int n = seq.size();
h1.resize(n + 1, 0);
h2.resize(n + 1, 0);
p1.resize(n + 1, 1);
p2.resize(n + 1, 1);
for (int i = 1; i <= n; i++) {
h1[i] = (h1[i - 1] * P1 + seq[i - 1]) % MOD1;
h2[i] = (h2[i - 1] * P2 + seq[i - 1]) % MOD2;
p1[i] = (p1[i - 1] * P1) % MOD1;
p2[i] = (p2[i - 1] * P2) % MOD2;
}
}
std::pair<u64, u64> get(int l, int r) {
u64 hash1 = (h1[r] - h1[l - 1] * p1[r - l + 1] % MOD1 + MOD1) % MOD1;
u64 hash2 = (h2[r] - h2[l - 1] * p2[r - l + 1] % MOD2 + MOD2) % MOD2;
return {hash1, hash2};
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n, q;
std::cin >> n >> q;
std::string s1, s2;
std::cin >> s1;
s2 = s1;
std::reverse(s2.begin(), s2.end());
StringHash sh1, sh2;
sh1.build(s1);
sh2.build(s2);
int siz = s1.size();
while (q--) {
int l, r;
std::cin >> l >> r;
if (sh1.get(l, r) == sh2.get(siz - r + 1, siz - l + 1)) {
std::cout << "Budada\n";
} else {
if ((r - l) % 2) {
std::cout << "Budada\n";
} else {
std::cout << "Putata\n";
}
}
}
}

浙公网安备 33010602011771号