SMU 2024 spring 天梯赛4
SMU 2024 spring 天梯赛4
7-1 心理阴影面积 - SMU 2024 spring 天梯赛4 (pintia.cn)
由 \(d = \frac{Ax+By+c}{\sqrt {A^2 + B^2}}\) 再乘以 \(100\sqrt 2 \times \frac{1}{2}\)可得\(|x-y| \times 50\);
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int x,y;
cin >> x >> y;
cout << abs(y-x) * 50 << '\n';
return 0;
}
7-2 人与神 - SMU 2024 spring 天梯赛4 (pintia.cn)
To iterate is human, to recurse divine.
7-3 通讯录的录入与显示 - SMU 2024 spring 天梯赛4 (pintia.cn)
要查询的记录编号可能为负数;
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string s;
vector<array<string,5>> mp(n + 1);
for(int i = 0;i < n;i ++){
for(int j = 0;j < 5;j ++)
cin >> mp[i][j];
}
int k;
cin >> k;
while(k--){
int num;
cin >> num;
if(num < n && num >= 0){
cout << mp[num][0] << ' ' << mp[num][3] << ' ' << mp[num][4] << ' ' << mp[num][2] << ' ' << mp[num][1] << '\n';
}else{
cout << "Not Found\n";
}
}
return 0;
}
7-4 算术入门之加减乘除 - SMU 2024 spring 天梯赛4 (pintia.cn)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a,b;
cin >> a >> b;
printf("%d + %d = %d\n",a,b,a+b);
printf("%d - %d = %d\n",a,b,a-b);
printf("%d * %d = %d\n",a,b,a*b);
if(a % b == 0)
printf("%d / %d = %d\n",a,b,a/b);
else
printf("%d / %d = %.2lf\n",a,b,a * 1.0 / b);
return 0;
}
7-5 出生年 - SMU 2024 spring 天梯赛4 (pintia.cn)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,x,y;
cin >> y >> n;
for(int i = y;;i ++){
set<int> s;
if(i<1000) s.insert(0);
int j = i;
while(j){
s.insert(j % 10);
j /= 10;
}
if(s.size() == n){
printf("%d %04d\n",i - y, i);
break;
}
}
return 0;
}
7-6 九宫格输入法 - SMU 2024 spring 天梯赛4 (pintia.cn)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<string> ok{
{"0 "},{"1,.?!"},{"2ABC"},{"3DEF"},{"4GHI"},{"5JKL"},{"6MNO"},{"7PQRS"},{"8TUV"},{"9WXYZ"}
};
string s;
getline(cin,s);
for(int i = 0;i < s.size();i ++){
if(s[i] != ' '){
int j = i;
while(j + 1 < s.size() && s[j + 1] != ' ') j ++;
int num = j - i;
string p = ok[s[i] - '0'];
cout << p[num % (int)p.size()];
i = j + 1;
}
}
return 0;
}
7-7 螺旋方阵 - SMU 2024 spring 天梯赛4 (pintia.cn)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector ans(n,vector<int>(n));
int cnt = 1;
for(int i = 0;i <= n / 2;i ++){
int j = i, k = i + 1;
while(j < n && !ans[i][j]) ans[i][j ++] = cnt ++;
j --;
while(k < n && !ans[k][j]) ans[k ++][j] = cnt ++;
k --,j --;
while(j >= 0 && !ans[k][j]) ans[k][j--] = cnt ++;
j ++,k --;
while(k >= 0 && !ans[k][j]) ans[k --][j] = cnt ++;
}
for(int i = 0;i < n;i ++){
for(int j = 0;j < n;j ++){
int num = to_string(ans[i][j]).size();
for(int k = 0;k < 3 - num;k ++)
cout << ' ';
cout << ans[i][j];
}
cout << '\n';
}
return 0;
}
7-8 抓老鼠啊~亏了还是赚了? - SMU 2024 spring 天梯赛4 (pintia.cn)
f1为不高兴期,f2为伤心期,g为兴奋期,其余按题意模拟;
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
int ans = 0;
int f1 = 0, f2 = 0, g = 0;
for (int i = 0; i < s.size() - 1; i ++) {
if (!g) {
if (f1 || f2) {
if (f1) f1 --;
if (f2) f2 --;
cout << '-';
continue;
}
}
if(f1) f1 --;
if(f2) f2 --;
if(g) g --;
if (s[i] == 'X') {
f1 = 1;
cout << 'U';
} else if (s[i] == 'T') {
f2 = 2;
ans += 7;
cout << 'D';
} else if (s[i] == 'C') {
f1 = f2 = 0;
g = 2;
ans -= 3;
cout << '!';
}
}
cout << '\n' << ans << '\n';
return 0;
}
7-9 Windows消息队列 - SMU 2024 spring 天梯赛4 (pintia.cn)
用优先队列按优先级为关键字模拟;
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
using a2 = pair<int,string>;
priority_queue<a2,vector<a2>,greater<a2>> Q;
while(n --){
string s;
cin >> s;
if(s == "PUT"){
string a;
int x;
cin >> a >> x;
Q.push({x,a});
}else{
if(Q.empty()){
cout << "EMPTY QUEUE!\n";
continue;
}
cout << Q.top().second << '\n';
Q.pop();
}
}
return 0;
}
7-10 名人堂与代金券 - SMU 2024 spring 天梯赛4 (pintia.cn)
模拟;
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct Node {
int x;
string s;
bool operator < (const Node& y) const {
if (x != y.x) return x > y.x;
return s < y.s;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, g, k;
cin >> n >> g >> k;
string s;
vector<Node> res(n);
int ans = 0;
for (int i = 0; i < n; i ++) {
int x;
cin >> s >> x;
res[i] = {x, s};
if (x >= g) ans += 50;
else if (x >= 60) ans += 20;
}
cout << ans << '\n';
sort(res.begin(), res.end());
int cnt = 1, las = 0, now = 0;
while (cnt <= k) {
las = now;
cout << cnt << ' ' << res[now].s << ' ' << res[now].x << '\n';
now ++;
while (now < n && res[now].x == res[now - 1].x) {
cout << cnt << ' ' << res[now].s << ' ' << res[now].x << '\n';
now ++;
}
cnt += now - las;
}
return 0;
}
7-11 用扑克牌计算24点 - SMU 2024 spring 天梯赛4(补题) (pintia.cn)
括号有三种类型,一种是2 3 12 12,((3-2)*12)+12,一种是2 13 7 7, 2*(13-(7/7)),还有一种是5 5 5 5,(5*5)-(5/5),注意判断的时候即可,其余可以直接用全排列然后去枚举四种符号即可;
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
double a[4] = {};
for (int i = 0; i < 4; i ++)
cin >> a[i];
auto cal = [](double x, double y, char c) -> double {
if (c == '+') return x + y;
if (c == '-') return x - y;
if (c == '*') return x * y;
return x / y;
};
sort(a, a + 4);
char c[] = {'+', '-', '*', '/'};
do {
for (int i = 0; i < 4; i ++) {
for (int j = 0; j < 4; j ++) {
for (int k = 0; k < 4; k ++) {
if (cal(cal(cal(a[0], a[1], c[i]), a[2], c[j]), a[3], c[k]) == 24) {
cout << "((" << a[0] << c[i] << a[1] << ')' << c[j] << a[2] << ')' << c[k] << a[3];
return 0;
} else if (cal(cal(a[0], a[1], c[i]), cal(a[2], a[3], c[j]) , c[k]) == 24) {
cout << "(" << a[0] << c[i] << a[1] << ")" << c[k] << "(" << a[2] << c[j] << a[3] << ")";
return 0;
} else if (cal(a[0], cal(a[1], cal(a[2], a[3], c[k]), c[j]), c[i]) == 24) {
cout << a[0] << c[i] << "(" << a[1] << c[j] << "(" << a[2] << c[k] << a[3] << "))" ;
return 0;
}
}
}
}
} while (next_permutation(a, a + 4));
cout << "-1\n";
return 0;
}
7-12 玩转二叉树 - SMU 2024 spring 天梯赛4(补题) (pintia.cn)
注意节点没有说是\(1\sim n\)啊QAQ
写得时候一直段错误,后来改成存下标就过了,如果不理解这题的可以先去看看已知前序和中序怎么求后序,在递归的时候其实就是按左右儿子去递归,所以我们存的时候可以先存右儿子再存左儿子,这样就可以保证遍历的时候是反转的;
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> pre(n + 1), mid(n + 1);
vector g(n + 1, vector<int>());
for (int i = 1; i <= n; i ++) cin >> mid[i];
for (int i = 1; i <= n; i ++) cin >> pre[i];
auto dfs = [&](auto dfs, int root, int start, int end) -> void{
if (start > end)
return ;
int i = start;
while (i < end && mid[i] != pre[root])
i++;
int l = root + 1, r = root + 1 - start + i;
if (r <= n && r > 0)
g[root].push_back(r);
if (l > 0 && l <= n)
g[root].push_back(l);
dfs(dfs, r, i + 1, end);
dfs(dfs, l, start, i - 1);
};
dfs(dfs, 1, 1, n);
queue<int> Q;
Q.push(1);
vector<bool> vis(n + 1);
vector<int> ans;
while (Q.size()) {
int u = Q.front();
Q.pop();
if (vis[u]) continue;
vis[u] = true;
ans.push_back(pre[u]);
for (auto v : g[u]) {
Q.push(v);
}
}
for (auto i : ans)
cout << i << " \n"[i == ans.back()];
return 0;
}
7-13 六度空间 - SMU 2024 spring 天梯赛4(补题) (pintia.cn)
bfs的进阶一点小技巧(,就是记录每层的最后一个节点last,然后当该次bfs遍历到了这一层的最后一个节点,那么久层数level++,大于6的时候退出即可;
dfs的话大多数题解也讲了,就是会丢掉某些最优解导致不能AC;
#include <bits/stdc++.h>
using namespace std;
using i64 = long long ;
int main() {
int n, m;
cin >> n >> m;
vector g(n + 1, vector<int>());
vector<bool> vis(n + 1);
for (int i = 0; i < m; i ++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
auto bfs = [&](int x) {
vector<bool>(n + 1).swap(vis);
int level = 0, last = x, now = 0, ans = 1;
queue<int> Q;
Q.push(x);
vis[x] = 1;
while (Q.size()) {
auto u = Q.front();
Q.pop();
for (auto v : g[u]) {
if (!vis[v]) {
ans ++;
Q.push(v);
now = v;
vis[v] = 1;
}
}
if (u == last) {
level ++;
last = now;
}
if (level >= 6) {
break;
}
}
return ans;
};
for (int i = 1; i <= n; i ++) {
int ans = bfs(i);
printf("%d: %.2lf%%\n", i, ans * 100.0 / n);
}
return 0;
}