SMU 2024 spring 天梯赛自主训练1
SMU 2024 spring 天梯赛自主训练1
7-1 宇宙无敌大招呼 - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
#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; cin >> s; cout << "Hello " << s << '\n'; return 0; }
7-2 日期格式化 - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
#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 m,d,y; scanf("%d-%d-%d",&m,&d,&y); printf("%d-%02d-%02d\n",y,m,d); return 0; }
7-3 寻找250 - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
#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 x,i = 0; while(1){ i ++; cin >> x; if(x == 250){ cout << i << '\n'; exit(0); } } return 0; }
7-4 装睡 - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
#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; for(int i = 0;i < n;i ++){ string s; int x,y; cin >> s >> x >> y; if(x < 15 || x > 20 || y < 50 || y > 70){ cout << s << '\n'; } } return 0; }
7-5 矩阵A乘以B - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
#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 ra,ca,rb,cb; cin >> ra >> ca; vector A(ra,vector<int>(ca)); for(int i = 0;i < ra;i ++) for(int j = 0;j < ca;j ++) cin >> A[i][j]; cin >> rb >> cb; vector B(rb,vector<int>(cb)); for(int i =0;i < rb;i ++) for(int j =0;j < cb;j ++) cin >> B[i][j]; if(ca != rb){ cout << "Error: " << ca << " != " << rb << '\n'; }else{ cout << ra << ' ' << cb << '\n'; for(int i = 0;i < ra;i ++){ for(int j = 0;j < cb;j ++){ int res = 0; for(int k = 0;k < ca;k ++) res += A[i][k] * B[k][j]; cout << res << " \n"[j == cb - 1]; } } } return 0; }
7-6 稳赢 - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
#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 k, i = 0; cin >> k; string s; cin >> s; while(s != "End"){ i ++; if(i % (k + 1) == 0){ cout << s << '\n'; }else{ if(s == "ChuiZi") cout << "Bu\n"; else if(s == "JianDao") cout << "ChuiZi\n"; else cout << "JianDao\n"; } cin >> s; } return 0; }
7-7 整除光棍 - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
C++暴力过不了,Python可以;
维护一个全为1的字段以及其位数,每次记得取模;
#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 x; cin >> x; int num = 1,cnt = 1; while(num < x){ num *= 10, num ++; cnt ++; } while(1){ cout << num / x; num %= x; if(!num){ break; } num *= 10; num ++; cnt ++; } cout << ' ' << cnt << '\n'; return 0; }
7-8 阅览室 - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
测试点1,一本书多次借走,以最后一次时间为准,一本书多次返还,以第一次时间为准;
测试点3,平均阅读时间四舍五入;
#include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> PII; struct Book{ bool f = false; int st,ed; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for(int i = 0;i < n;i ++){ vector<Book> Bu(1010); int shu,h,m,sum = 0,num = 0,now = 0; char key,op; cin >> shu >> key >> h >> op >> m; while(shu){ now = h * 60 + m; if(key == 'E' && Bu[shu].f){ num ++,sum += now - Bu[shu].st; Bu[shu].f = false; }else if(key == 'S'){ Bu[shu].f = true; Bu[shu].st = max(now,Bu[shu].st); } cin >> shu >> key >> h >> op >> m; } cout << num << ' ' << (num ? round(sum*1.0/num) : 0) << '\n'; } return 0; }
7-9 点赞狂魔 - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
排序;
#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; map<string,set<int>> Name; vector<string> ans(n); map<string,double> P; for(int i = 0;i < n;i ++){ int k; string s; cin >> s >> k; ans[i] = s; for(int j = 0;j < k;j ++){ int f; cin >> f; Name[s].insert(f); } int p = Name[s].size(); P[s] = 1.0 * k / p ; } sort(ans.begin(), ans.end(),[&](string a,string b){ if(Name[a].size() == Name[b].size()) return P[a] < P[b]; return Name[a].size() > Name[b].size(); }); for(int i = 0;i< 3;i++){ if(i >= ans.size()) cout << "-"; else cout << ans[i] ; cout << " \n"[i == 2]; } return 0; }
7-10 重排链表 - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
模拟链表,链表不是按\(Data\)链接的,要按照给出的起始地址得出链表;
链表可能不是一整条链,有多余节点,最后结果得按链表的最终节点数重排;
#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 st,n; cin >> st >> n; vector<PII> L(n); vector<PII> Num(100100); for(int i = 0;i < n;i ++){ int x,a,addr; cin >> addr >> a >> x; Num[addr] = {a,x}; } int cnt = 0; while(st != -1){ L[cnt ++] = {Num[st].first,st}; st = Num[st].second; } for(int i = 0,j = cnt - 1;i <= j;i ++, j --){ if(i == j){ printf("%05d %d -1\n",L[i].second,L[i].first); return 0; } printf("%05d %d %05d\n", L[j].second,L[j].first,L[i].second); printf("%05d %d ",L[i].second,L[i].first); if(i == j - 1) printf("-1\n"); else printf("%05d\n",L[j - 1].second); } return 0; }
7-11 图着色问题 - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
所着色一定为\(k\)种;
dfs判断每个点周围是否合法;
#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, k; cin >> n >> m >> k; vector g(n + 1, vector<int>()); for (int i = 0; i < m; i ++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } vector<int> color(n + 1); vector<bool> vis(n + 1); bool ok = true; auto solve = [&](auto self, int u, int fa, int x) -> void{ vis[u] = true; for (auto v : g[u]) { if (color[v] == x) ok = false; if (v == fa) continue; if (vis[v]) continue; self(self, v, u, color[v]); } }; int Case; cin >> Case; while (Case--) { ok = true; set<int> ck; for (int i = 1; i <= n; i ++) { cin >> color[i]; ck.insert(color[i]); } for (int i = 1; i <= n; i ++) solve(solve, i, 0, -1); if (ck.size() != k) ok = false; cout << (ok ? "Yes" : "No") << '\n'; vector<bool>(n + 1).swap(vis); } return 0; }
7-12 部落 - SMU 2024 spring 天梯赛自主训练1 (pintia.cn)
并查集;
#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; set<int> people; vector<int> fa(10010); iota(fa.begin(),fa.end(),0); auto find = [&](auto self,int x)->int{ return fa[x] == x ? x : fa[x] = self(self,fa[x]); }; for(int i = 0;i < n;i ++){ int k,one,u,v; cin >> k >> one; people.insert(one); u = find(find,one); for(int j = 1;j < k;j ++){ int x; cin >> x; people.insert(x); v = find(find,x); if(u != v){ fa[v] = u; } } } int All = people.size(), Num = 0; for(int i = 1;i <= All;i ++) if(find(find,i) == i) Num ++; cout << All << ' ' << Num << '\n'; int m; cin >> m; while(m --){ int u,v; cin >> u >> v; u = find(find,u); v = find(find,v); if(u != v) cout << "N\n"; else cout << "Y\n"; } return 0; }
本文作者:Ke_scholar
本文链接:https://www.cnblogs.com/Kescholar/p/18078498
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步