SMU 2024 spring 天梯赛自主训练3
SMU 2024 spring 天梯赛自主训练3
7-1 2018我们要赢 - SMU 2024 spring 天梯赛自主训练3 (pintia.cn)
2018
wo3 men2 yao4 ying2 !
7-2 打折 - SMU 2024 spring 天梯赛自主训练3 (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,r;
cin >> n >> r;
printf("%.2lf\n", n * (r) * 0.1);
return 0;
}
7-3 电子汪 - SMU 2024 spring 天梯赛自主训练3 (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 a,b;
cin >> a >> b;
for(int i = 0;i < a+ b;i ++)
cout << "Wang!";
return 0;
}
7-4 谁是赢家 - SMU 2024 spring 天梯赛自主训练3 (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 pa, pb,a=0,b=0;
cin >> pa >> pb;
for(int i = 0;i < 3;i ++){
int x;
cin >> x;
a += (x == 0);
b += (x == 1);
}
if(a + pa > b + pb && b != 3) {
printf("The winner is a: %d + %d\n",pa,a);
}else{
printf("The winner is b: %d + %d\n",pb,b);
}
return 0;
}
7-5 福到了 - SMU 2024 spring 天梯赛自主训练3 (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);
char c;
int n;
cin >> c >> n;
vector<string> s(n), ans(n);
getline(cin, s[0]);
for (int i = 0; i < n; i ++) {
getline(cin , s[i]);
ans[i] = s[i];
reverse(ans[i].begin(), ans[i].end());
}
reverse(ans.begin(), ans.end());
if (ans == s) {
cout << "bu yong dao le\n";
}
for (auto str : ans) {
for (auto i : str)
if (i != ' ') cout << c;
else cout << ' ';
cout << '\n';
}
return 0;
}
7-6 倒数第N个字符串 - SMU 2024 spring 天梯赛自主训练3 (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 L, n;
cin >> L >> n;
string s = string(L, 'z');
for (int i = 1; i < n; i ++) {
int cnt = L - 1;
s[cnt]--;
while (s[cnt] < 'a' && cnt >= 0) {
s[cnt] = 'z';
s[--cnt] --;
}
}
cout << s << '\n';
return 0;
}
7-7 天梯赛座位分配 - SMU 2024 spring 天梯赛自主训练3 (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, sum = 0, m;
cin >> n, m = n;
vector<int> a(n + 1), now(n + 1);
for (int i = 1; i <= n; i ++) {
cin >> a[i];
a[i] *= 10;
sum += a[i];
}
vector ans(n + 1, vector<int>());
int people = 0, val = 1;
while (sum--) {
for (int i = 1; i <= n; i ++) {
if (ans[i].size() < a[i]) {
ans[i].push_back(val);
if (people + 1 == n) val += 2;
else val ++;
people = 0;
for (int j = 1; j <= n; j ++)
if (ans[j].size() >= a[j])
people ++;
}
}
}
for (int i = 1; i <= n; i ++) {
cout << '#' << i << '\n';
int cnt = 1;
for (int j = 0; j < ans[i].size(); j ++) {
cout << ans[i][j] << " \n"[cnt % 10 == 0];
cnt ++;
}
}
return 0;
}
7-8 7206 猜数字 - SMU 2024 spring 天梯赛自主训练3 (pintia.cn)
直接从\(1000 \sim 9999\)枚举符合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;
auto check = [](int x, array<int, 3> Case) {
vector<int> NumX(5), NumC(5);
for (int i = 4; i >= 1; i --) {
NumX[i] = x % 10, x /= 10;
NumC[i] = Case[0] % 10, Case[0] /= 10;
}
int SameNum = 0, SameLoc = 0;
for (int i = 1; i <= 4; i ++)
if (NumC[i] == NumX[i]) SameLoc ++;
for (int i = 1; i <= 4; i ++)
for (int j = 1; j <= 4; j ++)
if (NumC[j] == NumX[i]) {
SameNum ++, NumC[j] = -1;
break;
}
return (SameLoc == Case[2] && SameNum == Case[1]);
};
while (cin >> n) {
if (!n) break;
vector<array<int, 3>> Case(n);
for (auto &[a, b, c] : Case)
cin >> a >> b >> c;
int Ke = 0, ans = 0;
for (int i = 1000; i < 10000; i ++) {
int m = 0;
for (auto v : Case)
if (check(i, v)) m ++;
if (m == n) Ke ++, ans = i;
}
if (Ke == 1) cout << ans << '\n';
else cout << "Not sure\n";
}
return 0;
}
7-9 分而治之 - SMU 2024 spring 天梯赛自主训练3 (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 {
int n;
vector<int> fa;
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;
vector<PII> edge(m + 1);
for (int i = 1; i <= m; i ++) {
int u, v;
cin >> u >> v;
edge[i] = {u, v};
}
int k;
cin >> k;
while (k --) {
int ke ;
cin >> ke;
set<int> cuit;
for (int i = 0; i < ke; i ++) {
int x;
cin >> x;
cuit.insert(x);
}
UFS ufs(n);
for (auto [u, v] : edge) {
if (cuit.count(u) || cuit.count(v)) continue;
ufs.unin(u, v);
}
int num = 0;
for (int i = 1; i <= n; i ++) {
if (ufs.find(i) == i) num ++;
}
if (num == n) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
7-10 小字辈 - SMU 2024 spring 天梯赛自主训练3 (pintia.cn)
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;
cin >> n;
vector<int> fa(n + 1), Bei(n + 1);
int root = 0, mx = 0;
vector g(n + 1, vector<int>());
for (int i = 1; i <= n; i ++) {
cin >> fa[i];
if (fa[i] == -1) root = i;
else {
g[fa[i]].push_back(i);
}
}
auto dfs = [&](auto dfs, int x) -> void{
Bei[x] ++;
for (auto v : g[x]) {
Bei[v] += Bei[x];
dfs(dfs, v);
}
};
dfs(dfs, root);
for (int i = 1; i <= n; i ++)
mx = max(mx, Bei[i]);
cout << mx << '\n';
vector<int> ans;
for (int i = 1; i <= n; i ++)
if (Bei[i] == mx)
ans.push_back(i);
for (auto i : ans)
cout << i << " \n"[i == ans.back()];
return 0;
}
7-11 名人堂与代金券 - SMU 2024 spring 天梯赛自主训练3 (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,g,k;
cin >> n >> g >> k;
vector<pair<string,int>> s(n);
int sum = 0;
for(auto &[x,y] : s){
cin >> x >> y;
if(y >= g) sum += 50;
else if(y >= 60) sum += 20;
}
sort(s.begin(),s.end(),[](pair<string,int> a,pair<string,int> b){
if(a.second != b.second) return a.second > b.second;
return a.first < b.first;
});
cout << sum << '\n';
int now = 1;
for(int i = 0;i < n;i ++){
int cnt = 1;
cout << now << ' ' << s[i].first << ' ' << s[i].second << '\n';
while(i + 1 < n && s[i].second == s[i + 1].second){
i ++;
cnt ++;
cout << now << ' ' << s[i].first << ' ' << s[i].second << '\n';
}
now += cnt;
if(now > k) break;
}
return 0;
}
7-12 秀恩爱分得快 - SMU 2024 spring 天梯赛自主训练3 (pintia.cn)
这题我真的很想吐槽下;
wa了30多发,感觉很ex啊,有0和-0这玩意,而且还有可能两者就没出现在照片里过,还有些函数像是abs,stoi啊,能用一次就用一次,不要多次调用,本来\(\mathcal{O}(mk^2)\)的复杂度也很难过吧,没过那就要要注意一下上面的点,还是冲过去了,400多ms,还有就是可以用vector去记录照片里的人,还有还有,就是最后找出来的ma和mb不能直接比较等于,要和他们各自对对方的亲密度比较,就因为这个玩意一直在测试点5wa,真的服了;
#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;
vector val(n, vector<double>(n, 0));
vector<int> fu(n, 1);
for (int i = 0; i < m; i ++) {
int k;
cin >> k;
vector<int> people(k);
for (int j = 0; j < k; j ++) {
string s;
cin >> s;
int x = stoi(s);
if (s[0] == '-') x = -x, fu[x] = -1;
people[j] = x;
}
double ok = 1.0 / k;
for (int l = 0; l < k; l ++) {
for (int r = l + 1; r < k; r ++) {
int j = people[l], p = people[r];
if (fu[j] != fu[p]) {
val[j][p] += ok;
val[p][j] += ok;
}
}
}
}
int a, b;
string x, y;
cin >> x >> y;
a = stoi(x), b = stoi(y);
if (x[0] == '-') a = -a, fu[a] = -1;
if (y[0] == '-') b = -b, fu[b] = -1;
double ma = 0, mb = 0;
for (int i = 0; i < n; i ++) {
if (fu[i] != fu[a])
ma = max(ma, val[a][i]);
if (fu[i] != fu[b])
mb = max(mb, val[b][i]);
}
auto print = [&](int x, int y) {
if (fu[x] == -1) cout << '-';
cout << x << ' ';
if (fu[y] == -1) cout << '-';
cout << y << '\n';
};
if (ma == val[a][b] && mb == val[b][a]) {
print(a, b);
} else {
for (int i = 0; i < n; i ++) {
if (val[a][i] == ma && fu[a] != fu[i]) {
print(a, i);
}
}
for (int i = 0; i < n; i ++) {
if (val[b][i] == mb && fu[b] != fu[i]) {
print(b, i);
}
}
}
return 0;
}