SMU 2024 spring 天梯赛2
SMU 2024 spring 天梯赛2
7-1 计算指数 - SMU 2024 spring 天梯赛2 (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,ans = 1;
cin >> n;
for(int i = 1;i <= n;i ++, ans *= 2);
cout << "2^"<< n<<" = " << ans;
return 0;
}
7-2 计算摄氏温度 - SMU 2024 spring 天梯赛2 (pintia.cn)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << "fahr = 100, celsius = "<<5 * (100-32)/9 ;
return 0;
}
7-3 念数字 - SMU 2024 spring 天梯赛2 (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> a = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string s;
cin >> s;
if(s[0] == '-') cout << "fu ",s.erase(s.begin());
for(int i = 0;i < s.size();i++)
cout << a[s[i] - '0'] << " \n"[i == s.size() - 1];
return 0;
}
7-4 求阶乘累加和 - SMU 2024 spring 天梯赛2 (pintia.cn)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
i64 n, ans = 0,a = 1;
cin >> n;
for(int i = 1;i <= n;i ++){
a *= i;
ans += a;
}
cout << ans << '\n';
return 0;
}
7-5 6翻了 - SMU 2024 spring 天梯赛2 (pintia.cn)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
getline(cin, s);
for (int i = 0; i < s.size();) {
if (s[i] == '6') {
int j = i;
while (j < s.size() && s[j] == '6') j ++;
if (j - i > 9) cout << "27", i = j;
else if (j - i > 3) cout << "9", i = j;
else cout << s[i] , i ++;
} else
cout << s[i], i ++;
}
return 0;
}
7-6 福到了 - SMU 2024 spring 天梯赛2 (pintia.cn)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
char c;
int n;
cin >> c >> n;
vector<string> a(n), b(n);
string s;
getline(cin,s);
for (int i = 0;i < n;i ++) {
getline(cin,s);
b[i] = a[i] = s;
reverse(b[i].begin(), b[i].end());
}
reverse(b.begin(), b.end());
if (a == b) cout << "bu yong dao le\n";
for (auto s : b) {
for (auto j : s)
if (j == '@') cout << c;
else cout << j;
cout << '\n';
}
return 0;
}
7-7 估值一亿的AI核心代码 - SMU 2024 spring 天梯赛2(补题) (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;
getline(cin, s);
auto biaoDian = [](char c) -> bool{
if (c >= 'A' && c <= 'Z') return false;
if (c >= 'a' && c <= 'z') return false;
if (c >= '0' && c <= '9') return false;
return true;
};
for (int i = 0; i < n; i ++) {
getline(cin, s);
cout << s << '\n';
vector<string> a;
int j = 0, k = 0;
for (int l = 0; l < s.size(); l ++)
if (s[l] == '?') s[l] = '!';
while (k < s.size()) {
if (s[k] == ' ') {
while (k + 1 < s.size() && s[k + 1] == ' ') k ++;
if (a.empty()) {
k ++;
j = k;
continue;
};
a.push_back(" ");
} else if (!biaoDian(s[k])) {
while (k + 1 < s.size() && s[k + 1] != ' ' && !biaoDian(s[k + 1])) k ++;
a.push_back(s.substr(j, k - j + 1));
} else {
string as = "";
as += s[k];
a.push_back(as);
}
k ++;
j = k;
}
if (a.size() && a.back() == " ") a.pop_back();
for (j = a.size() - 1; j > 0; j --) {
if (biaoDian(a[j][0])) {
if (a[j - 1] == " ") a.erase(a.begin() + j - 1);
}
}
for (j = 0; j < a.size(); j ++) {
for (k = 0; k < a[j].size(); k ++) {
if (a[j][k] >= 'A' && a[j][k] < 'Z' && a[j][k] != 'I')
a[j][k] += 32;
}
}
for (j = 0; j < a.size(); j ++) {
if (a[j] == "I" || a[j] == "me") {
a[j] = "you#";
continue;
}
}
for (j = 0; j < a.size(); j ++) {
if (j + 2 >= a.size()) continue;
string p = a[j] + a[j + 1] + a[j + 2];
if (p == "can you" || p == "could you") {
a[j + 2] = a[j];
a[j] = "I";
j += 2;
}
}
string ans = "";
for (auto p : a)
ans += p;
cout << "AI: ";
for (j = 0; j < ans.size(); j ++)
if (ans[j] == '#') continue;
else cout << ans[j];
cout << '\n';
}
return 0;
}
7-8 前世档案 - SMU 2024 spring 天梯赛2(补题) (pintia.cn)
是对照0,否对照1;
按照上述去判断对应问题即结论1为000,结论2为001 \(\dots\) 结论8为111;
很明显可以用递归去判断,结论数即对应的二进制转十进制后加1即可;
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
while (m --) {
string s;
cin >> s;
auto dfs = [&](auto dfs, int now) -> i64{
if (now >= n) return 1;
if (s[now] == 'y')
return dfs(dfs, now + 1);
else
return (1ll << (n - now - 1)) + dfs(dfs, now + 1);
};
cout << dfs(dfs, 0) << '\n';
}
return 0;
}
7-9 抢红包 - SMU 2024 spring 天梯赛2(补题) (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;
struct people{
int id,num = 0;
double val = 0;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<people> Ke(n + 1);
for(int i = 1;i <= n;i ++){
int k,num = 0;
cin >> k;
Ke[i].id = i;
for(int j = 0;j < k;j ++){
int ni,pi;
cin >> ni >> pi;
num += pi;
Ke[ni].num ++;
Ke[ni].val += pi;
}
Ke[i].val -= num;
}
Ke.erase(Ke.begin());
sort(Ke.begin(),Ke.end(),[&](people a,people b){
if(a.val == b.val && a.num == b.num) return a.id < b.id;
if(a.val == b.val) return a.num > b.num;
return a.val > b.val;
});
for(auto [id,num,val] : Ke){
printf("%d %.2lf\n",id, val / 100);
}
return 0;
}
7-10 红色警报 - SMU 2024 spring 天梯赛2(补题) (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;
struct UFS {
vector<int> fa;
int n;
UFS(int n) : n(n) {
fa.resize(n + 1);
for (int i = 0; i <= n; i ++)
fa[i] = i;
}
int find(int x) {
return fa[x] == x ? x : find(fa[x]);
}
void unin(int x, int y) {
x = find(x), y = find(y);
if (x != y) fa[x] = y;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
typedef pair<i64, i64> PII;
int num = 0;
vector<PII> edge(m);
UFS ufs(n);
for (auto &[u, v] : edge) {
cin >> u >> v;
ufs.unin(u, v);
}
for (int i = 0; i < n; i ++)
if (ufs.find(i) == i) num ++;
int k;
cin >> k;
vector<bool> vis(n);
while (k --) {
int x, now = 0;
cin >> x;
UFS ufs1(n);
vis[x] = 1;
for (int i = 0; i < m; i ++) {
auto [u, v] = edge[i];
if (vis[u] || vis[v]) continue;
ufs1.unin(u, v);
}
for (int i = 0; i < n; i ++)
if (ufs1.find(i) == i && !vis[i]) now ++;
if (now <= num) {
cout << "City " << x << " is lost.\n";
} else {
cout << "Red Alert: City " << x << " is lost!\n";
}
num = now;
bool f = false;
for (int i = 0; i < n; i ++)
if (!vis[i]) f = true;
if (!f) cout << "Game Over.\n";
}
return 0;
}
7-11 排座位 - SMU 2024 spring 天梯赛2(补题) (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;
struct UFS {
vector<int> fa;
int n;
UFS(int n) : n(n) {
fa.resize(n + 1);
for (int i = 0; i <= n; i ++)
fa[i] = i;
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void unin(int x, int y) {
x = find(x), y = find(y);
if (x != y) fa[x] = y;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
UFS frend(n);
vector Diren(n + 1,vector<int>(n + 1));
for (int i = 0; i < m; i ++) {
int u, v, c;
cin >> u >> v >> c;
if (c == 1) {
frend.unin(u, v);
} else Diren[u][v] = 1,Diren[v][u] = 1;
}
while(k --){
int u,v;
cin >> u >> v;
int a = frend.find(u),b = frend.find(v);
if(a == b && !Diren[u][v]){
cout << "No problem\n";
}else if(a != b && !Diren[u][v]){
cout << "OK\n";
}else if(a == b && Diren[u][v]){
cout << "OK but...\n";
}else{
cout << "No way\n";
}
}
return 0;
}
7-12 这是二叉搜索树吗? - SMU 2024 spring 天梯赛2(补题) (pintia.cn)
建一颗二叉搜索树,跑一遍dfs记录前后序遍历;
如果是镜像就在原来的dfs基础上交换一下左右子树的顺序即可;
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
struct Node {
int data;
Node* left;
Node* right;
Node* newNode(int v) {
Node* node = new Node;
node->data = v;
node->right = node->left = NULL;
return node;
}
void insert(Node* &root, int x) {
if (root == NULL) {
root = newNode(x);
return ;
}
if (root->data > x)
insert(root->left, x);
else
insert(root->right, x);
}
Node* Create(vector<int> tree) {
Node* root = NULL;
for (auto i : tree) {
insert(root, i);
}
return root;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> tree(n);
for (auto &i : tree) cin >> i;
Node Tr;
vector<int> pre, suf, Jpre;
auto dfs = [&](auto dfs, Node * root) {
if (root == NULL) return ;
pre.push_back(root->data);
dfs(dfs, root->left);
dfs(dfs, root->right);
suf.push_back(root->data);
};
dfs(dfs, Tr.Create(tree));
if (pre == tree) {
cout << "YES\n";
for (int i = 0; i < suf.size(); i ++)
cout << suf[i] << " \n"[i == suf.size() - 1];
return 0;
}
vector<int>().swap(suf);
auto dfs1 = [&](auto dfs, Node * root) {
if (root == NULL) return ;
Jpre.push_back(root->data);
dfs(dfs, root->right);
dfs(dfs, root->left);
suf.push_back(root->data);
};
dfs1(dfs1, Tr.Create(tree));
if (Jpre == tree) {
cout << "YES\n";
for (int i = 0; i < suf.size(); i ++)
cout << suf[i] << " \n"[i == suf.size() - 1];
return 0;
}
cout << "NO\n";
return 0;
}