2024 蓝桥杯模拟赛 1
P8761 [蓝桥杯 2021 国 BC] 大写
#include<bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
i32 main() {
string s;
cin >> s;
for( auto & i : s ){
if( i >= 'a' and i <= 'z' ) cout << char( i - 'a' + 'A');
else cout << i;
}
return 0;
}
P8195 [传智杯 #4 决赛] 小智的疑惑
#include<bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
i32 main() {
string s;
cin >> s;
int res = 0;
for( int i = 0 ; i + 8 <= s.size() ; i ++ ){
if( s.substr( i , 8 ) == "chuanzhi" ) res ++;
}
cout << res << "\n";
return 0;
}
P8651 [蓝桥杯 2017 省 B] 日期问题
注意去重
#include<bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
int t[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
i32 main() {
int a, b, c;
cin >> a, cin.ignore();
cin >> b, cin.ignore();
cin >> c, cin.ignore();
int y, m, d;
set<array<int, 3>> res;
if (a <= 59) y = a + 2000;
else y = a + 1900;
m = b, d = c;
if ( m >= 1 and m <= 12) {
if (m != 2) {
if (d >= 1 and d <= t[m]) res.insert({y, m, d});
} else {
if ((y % 4 == 0 and y % 100 != 0) or (y % 400) == 0) {
if (d >= 1 and d <= 29) res.insert({y, m, d});
} else if (d >= 1 and d <= 28)res.insert({y, m, d});
}
}
if (c <= 59) y = c + 2000;
else y = c + 1900;
m = a, d = b;
if (m >= 1 and m <= 12) {
if (m != 2) {
if (d >= 1 and d <= t[m]) res.insert({y, m, d});
} else {
if ((y % 4 == 0 and y % 100 != 0) or (y % 400) == 0) {
if (d >= 1 and d <= 29) res.insert({y, m, d});
} else if (d >= 1 and d <= 28)res.insert({y, m, d});
}
}
if (c <= 59) y = c + 2000;
else y = c + 1900;
m = b, d = a;
if (m >= 1 and m <= 12) {
if (m != 2) {
if (d >= 1 and d <= t[m]) res.insert({y, m, d});
} else {
if ((y % 4 == 0 and y % 100 != 0) or (y % 400) == 0) {
if (d >= 1 and d <= 29) res.insert({y, m, d});
} else if (d >= 1 and d <= 28)res.insert({y, m, d});
}
}
for (auto [x, y, z]: res)
printf("%d-%02d-%02d\n", x, y, z);
return 0;
}
P9240 [蓝桥杯 2023 省 B] 冶炼金属
两次二分
#include<bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
i32 main() {
int n;
cin >> n;
vector<pii> a(n);
for (auto &[x, y]: a) cin >> x >> y;
int l = 1, r = inf, res = -1;
for (int mid, flag; l <= r;) {
mid = (l + r) / 2, flag = 1;
for (const auto &[x, y]: a) {
if (x / mid <= y) continue;
flag = 0;
break;
}
if (flag) res = mid, r = mid - 1;
else l = mid + 1;
}
cout << res << " ";
l = 1, r = inf, res = -1;
for (int mid, flag; l <= r;) {
mid = (l + r) / 2, flag = 1;
for (const auto &[x, y]: a) {
if (x / mid >= y) continue;
flag = 0;
break;
}
if (flag) res = mid, l = mid + 1;
else r = mid - 1;
}
cout << res << "\n";
return 0;
}
P8627 [蓝桥杯 2015 省 A] 饮料换购
模拟
#include<bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
const int mod = 998244353;
int power(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod, y /= 2;
}
return ans;
}
int inv(int x) {
return power(x, mod - 2);
}
i32 main() {
int n, res = 0;
cin >> n;
for (int a = n, b = 0; a > 0 or b >= 3; a = b / 3, b %= 3)
res += a, b += a, a = 0;
cout << res << "\n";
return 0;
}
P8647 [蓝桥杯 2017 省 AB] 分巧克力
二分
#include<bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
const int mod = 998244353;
int power(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod, y /= 2;
}
return ans;
}
int inv(int x) {
return power(x, mod - 2);
}
i32 main() {
int n, m;
cin >> n >> m;
vector<pii> a(n);
for (auto &[x, y]: a) cin >> x >> y;
int l = 1, r = 1e5, res = -1;
for (int mid, cnt; l <= r;) {
mid = (l + r) / 2, cnt = 0;
for (const auto &[x, y]: a)
cnt += (x / mid) * (y / mid);
if (cnt >= m) res = mid, l = mid + 1;
else r = mid - 1;
}
cout << res << "\n";
return 0;
}
P8637 [蓝桥杯 2016 省 B] 交换瓶子
直接从小到大,每次判断瓶子在不在位置上,如果不在就进行一次交换
#include<bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
const int mod = 998244353;
int power(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod, y /= 2;
}
return ans;
}
int inv(int x) {
return power(x, mod - 2);
}
i32 main() {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++)
cin >> a[i], a[i]--, b[a[i]] = i;
int res = 0;
for (int i = 0, x, y, p, q; i < n; i++) {
if (b[i] == i) continue;
res++;
x = i, y = a[i], p = b[x], q = b[y];
swap(a[p], a[q]), swap(b[x], b[y]);
}
cout << res << "\n";
return 0;
}
P8625 [蓝桥杯 2015 省 B] 生命之树
其实就是求树上的最大连通块,所以直接进行树形dp,枚举每个叶子选不选即可
#include<bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
const int mod = 998244353;
int power(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod, y /= 2;
}
return ans;
}
int inv(int x) {
return power(x, mod - 2);
}
i32 main() {
int n, res = 0;
cin >> n;
vector<int> f(n);
for (auto &i: f) cin >> i;
vector<vi> e(n);
for (int i = 1, u, v; i < n; i++)
cin >> u >> v, --u, --v, e[u].push_back(v), e[v].push_back(u);
auto dfs = [&res, &f, e](auto &&self, int x, int fa) -> void {
for (const auto &y: e[x]) {
if (y == fa)continue;
self(self, y, x);
f[x] += max(0ll, f[y]);
}
res = max(res, f[x]);
};
dfs(dfs, 0, -1);
cout << res << "\n";
return 0;
}
P8638 [蓝桥杯 2016 省 A] 密码脱落
把原序列反序之后求一下lcs就行
#include<bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
const int mod = 998244353;
int power(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod, y /= 2;
}
return ans;
}
int inv(int x) {
return power(x, mod - 2);
}
i32 main() {
string a, b;
cin >> a, b = a, reverse(b.begin(), b.end());
int n = a.size();
a = " " + a, b = " " + b;
auto f = vector(n + 1, vi(n + 1));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (a[i] == b[j]) f[i][j] = f[i - 1][j - 1] + 1;
else f[i][j] = max(f[i - 1][j], f[i][j - 1]);
cout << n - f[n][n] << "\n";
return 0;
}
P8774 [蓝桥杯 2022 省 A] 爬树的甲壳虫
几何概型,求出每一步所需要爬的次数即可
#include<bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
const int mod = 998244353;
int power(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod, y /= 2;
}
return ans;
}
int inv(int x) {
return power(x, mod - 2);
}
i32 main() {
int n, res = 0;
cin >> n;
for (int x, y; n; n--)
cin >> x >> y, res = (res + 1) * y % mod * inv(y - x) % mod;
cout << res << "\n";
return 0;
}