周报
周报
写在前言
放假到现在几乎没怎么训练与刷题,也不知道该怎么去描述,从回到家基本就待在医院陪老爸再到陪他最后一面,心情也很是复杂,其实我甚至想过退队,不知道该怎么说,也可能受到网上一些网友的言论影响,总之想多赚点钱,竞赛这条路从最开始的兴趣到现在开始迷茫了,老爸在的时候还想着兴趣在学校里也能打打算法竞赛觉得挺好的,现在只想把对老爸亏欠的都补偿在老妈身上,之前是陪老爸,现在是陪老妈,感觉已经没什么心思训练了,这次也是跟着打了一下训练赛,可能看上去还行,但是这些题看着其实也就是比较基础的模拟和一些基础算法,而这些对于自己来说题意和模拟也需要长时间去写,关于期望那题也没什么思路,觉得脑子挺乱的,烦死了,
补题及题解
SMU 2024 winter round 3
7-1 猫是液体
输出\(a \times b \times c\)即可
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); i64 a,b,c; cin >> a >> b >> c; cout << a * b * c << '\n'; return 0; }
7-2 计算指数
输出\(2\)的\(n\)次方即可
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); i64 n, ans = 1; cin >> n; for (int i = 1; i <= n; i ++, ans *= 2); cout << "2^" << n << " = " << ans << '\n'; return 0; }
7-3 电子汪
输出\(a + b\)个\(Wang!\)即可
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int a, b; cin >> a >> b; for (int i = 0; i < a + b; i ++) cout << "Wang!"; return 0; }
7-4 最佳情侣身高差
男性除以\(1.09\),女性乘以\(1.09\)即可
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; while (n --) { char c; double a; cin >> c >> a; if (c == 'M') { printf("%.2lf\n", a / 1.09); } else { printf("%.2lf\n", a * 1.09); } } return 0; }
7-5 不变初心数
对\(n\)的每一倍数挨个计算每一位的和,判断最后是否一样即可
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; void solve() { int n; cin >> n; int num = -1; for (int i = 2; i <= 9; i ++) { i64 x = i * n, sum = 0; while (x) { sum += x % 10; x /= 10; } if (num == -1) num = sum; else if (num != sum) { cout << "NO\n"; return ; } } cout << num << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T --) solve(); return 0; }
7-6 吃火锅
暴力判断每句是否含有目标字符串即可,一句中含有多个目标按一个计算
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string s, tar = "chi1 huo3 guo1"; getline(cin, s); int ans = 0, first = 0, sum = 0; while (s != ".") { ans ++; for (int i = 0; i < s.size(); i ++) { if (s.substr(i, tar.size()) == tar) { if (!first) first = ans; sum ++; break; } } getline(cin, s); } if (!first) { cout << ans << '\n' << "-_-#\n"; } else { cout << ans << '\n' << first << ' ' << sum << '\n'; } return 0; }
7-7 敲笨钟
把每句的拼音都单独存储,判断逗号和句号前面的是否都压了“ong”,是就替换最后三个拼音
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string s; getline(cin, s); while (n --) { getline(cin, s); vector<string> so; int kong = 0, i = 0, fi = 0, num = 0; for (; i < s.size(); i ++) { if (s[i] == ' ') { if (s[i - 1] == ',') fi = num; so.push_back(s.substr(kong, i - kong)); kong = i + 1; num ++ ; } } so.push_back(s.substr(kong, i - kong)); string op = so[fi]; if (op.substr(max((int)op.size() - 4, 0), 3) == "ong" && so.back().substr(max((int)so.back().size(), 0) - 4, 3) == "ong") { for (int i = 0; i < so.size() - 3; i ++) cout << so[i] << ' '; cout << "qiao ben zhong.\n"; } else cout << "Skipped\n"; } return 0; }
7-8 查找书籍
按价格排下序即可
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; typedef pair<double, string> PSD; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<PSD> so(n); string s; getline(cin, s); for (int i = 0; i < n; i ++) { getline(cin, so[i].second); cin >> so[i].first; getline(cin, s); } sort(so.begin(), so.end()); printf("%.2lf, %s\n", so.back().first, so.back().second.c_str()); printf("%.2lf, %s\n", so[0].first, so[0].second.c_str()); return 0; }
7-9 梯云纵
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<double> a(110000), mid(110000); a[0] = 0, a[1] = 1, a[2] = 1.5, a[3] = 2.25; mid[0] = a[1] - a[0], mid[1] = a[2] - a[1], mid[2] = a[3] - a[2]; auto getmid = [&](auto self, int x) -> double { return (mid[x] != 0 ? mid[x] : mid[x] = (self(self, x - 1) + self(self, x - 2)) / 2); }; auto geta = [&](auto self, int x) -> double { return (a[x] != 0 ? a[x] : a[x] = (self(self, x - 1) + getmid(getmid, x - 1))); }; while (n --) { int x; cin >> x; printf("%.6lf\n", geta(geta, x) + (x > 10000) * 0.0000002); } return 0; }
7-10 家庭房产
用并查集将家庭联系起来,用家庭中编号最小的作为根节点,最后单独开个数组将家庭的各个数值计算出来,再排序即可.
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; template<typename T> struct UFS { int sz; vector<T> rank, p; void link(T x, T y) { if (x == y) return; if (y > x) p[y] = x; else p[x] = y; } void init(int n) { sz = n; rank.resize(n + 1); p.resize(n + 1); for (int i = 0; i <= sz; i++) { p[i] = i; rank[i] = 0; } } T find(T x) { return x == p[x] ? x : (p[x] = find(p[x])); } void unin(T x, T y) { link(find(x), find(y)); } void compress() { for (int i = 0; i < sz; i++) find(i); } }; struct Home { int id, fa, ma, kid, all = 0; double fang = 0, S = 0; bool home = 0; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; UFS<int> ufs; vector<Home> info(10086), ans(10086); vector<bool> vis(10086); ufs.init(10086); for (int i = 0; i < n; i ++) { int kid, k; cin >> info[i].id >> info[i].fa >> info[i].ma >> k ; vis[info[i].id] = 1; if (info[i].fa != -1) { ufs.unin(info[i].id, info[i].fa); vis[info[i].fa] = 1; } if (info[i].ma != -1) { ufs.unin(info[i].id, info[i].ma); vis[info[i].ma] = 1; } for (int j = 0; j < k; j ++) { cin >> kid; ufs.unin(kid, info[i].id); vis[kid] = 1; } cin >> info[i].fang >> info[i].S; } for (int i = 0; i < n; i ++) { int x = ufs.find(info[i].id); ans[x].id = x; ans[x].S += info[i].S; ans[x].fang += info[i].fang; ans[x].home = 1; } int num = 0; for (int i = 0; i < 10086; i ++) { if (vis[i]) ans[ufs.find(i)].all ++; if (ans[i].home) num ++; } for (int i = 0; i < 10086; i ++) { if (ans[i].home) { ans[i].S = ans[i].S / ans[i].all; ans[i].fang = ans[i].fang / ans[i].all; } } sort(ans.begin(), ans.end(), [&](Home a, Home b) { if (a.S != b.S) return a.S > b.S; return a.id < b.id; }); printf("%d\n", num); for (int i = 0; i < num; i ++) { printf("%04d %d %.03lf %.03lf\n", ans[i].id, ans[i].all, ans[i].fang, ans[i].S); } return 0; }
7-12 秀恩爱分得快
感觉本质上其实还是模拟,以及对题意的理解,
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; const int N = 1010; vector<int> a(N), sex(N); vector g(N, vector<double>(N)); while (m --) { int k, num = 0; cin >> k; for (int i = 0; i < k; i ++) { string s; cin >> s; int x; if (s[0] == '-') s[0] = '0', x = stoi(s), sex[x] = 1; else x = stoi(s); a[num ++] = x; } for (int i = 0; i < k - 1; i ++) { for (int j = i + 1; j < k; j ++) { if (sex[a[i]] == sex[a[j]]) continue; g[a[i]][a[j]] += 1.0 / k; g[a[j]][a[i]] = g[a[i]][a[j]]; } } } string s; int x, y; cin >> s; if (s[0] == '-') s[0] = '0', x = stoi(s), sex[x] = 1; else x = stoi(s); cin >> s; if (s[0] == '-') s[0] = '0', y = stoi(s), sex[y] = 1; else y = stoi(s); double mx = 0, my = 0; for (int i = 0; i < n; i ++) { if (sex[i] != sex[x]) mx = max(mx, g[x][i]); if (sex[i] != sex[y]) my = max(my, g[y][i]); } if (mx == my && g[x][y] == mx) cout << (sex[x] ? "-" : "") << x << " " << (sex[y] ? "-" : "") << y << '\n'; else { for (int i = 0; i < n; i ++) if (g[x][i] == mx && sex[x] != sex[i]) cout << (sex[x] ? "-" : "") << x << ' ' << (sex[i] ? "-" : "") << i << '\n'; for (int i = 0; i < n; i ++) if (g[y][i] == my && sex[y] != sex[i]) cout << (sex[y] ? "-" : "") << y << ' ' << (sex[i] ? "-" : "") << i << '\n'; } return 0; }
7-13 地下迷宫探索
\(DFS\)从起点往下搜即可,判断到达的地方与\(n\)的关系,小于\(n\)则代表没有全部到达
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, s; cin >> n >> m >> s; vector g(n + 1, vector<int>()); for (int i = 0; i < m; i ++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; i ++) { sort(g[i].begin(), g[i].end()); } vector<bool> vis(n + 1); int num = 0; auto dfs = [&](auto self, int x) -> void { if (num) cout << ' '; cout << x ; for (auto v : g[x]) { if (!vis[v]) { vis[v] = 1; num ++; self(self, v); cout << ' ' << x; } } }; vis[s] = 1; dfs(dfs, s); if (++ num < n) cout << " 0\n"; return 0; }
7-14 畅通工程之最低成本建设问题
\(Kurskal\)算法跑一遍最小生成树即可,模板题吧
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; template<typename T> struct UFS { T sz; vector<T> rank, p; void link(T x, T y) { if (x == y) return; if (rank[x] > rank[y]) p[y] = x; else p[x] = y; if (rank[x] == rank[y]) rank[y]++; } void init(int n) { sz = n; rank.resize(n + 1); p.resize(n + 1); for (int i = 0; i <= sz; i++) { p[i] = i; rank[i] = 0; } } T find(T x) { return x == p[x] ? x : (p[x] = find(p[x])); } void unin(T x, T y) { link(find(x), find(y)); } void compress() { for (int i = 0; i < sz; i++) find(i); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<pair<int, pair<int, int>>> road(m); for (int i = 0; i < m; i ++) { cin >> road[i].second.first >> road[i].second.second; cin >> road[i].first; } sort(road.begin(), road.end()); UFS<int> ufs; ufs.init(n); i64 ans = 0; for (int i = 0; i < m; i ++) { auto [x, y] = road[i].second; int dx = ufs.find(x), dy = ufs.find(y); if (dx != dy) { ufs.unin(dx, dy); ans += road[i].first; } } for (int i = 1; i <= n; i ++) { if (ufs.find(i) != ufs.find(1)) { cout << "Impossible\n"; return 0; } } cout << ans << '\n'; return 0; }
本文作者:Ke_scholar
本文链接:https://www.cnblogs.com/Kescholar/p/18020199
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2023-02-19 《冬训周报六》