天梯赛L2题解(041-044)
L2-041 插松枝
模拟,感觉有点难分析,需要仔细读题分析
#include <bits/stdc++.h>
using namespace std;
#define int long long
using PII = pair<int, int>;
using AIT = array<int, 3>;
// 首先弄清楚,他优先是在小盒子里拿的,然后才会去推送器拿
// 一根插完只有两种可能,第一种就是没得拿了,第二种就是拿不到了
void ylh_() {
int n, m, k;
cin >> n >> m >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
queue<int> q; // 推送器
stack<int> st; // 盒子
vector<int> res; // 手上的松枝
vector<vector<int>> ans; // 答案
for (int i = 1; i <= n; ++i) {
q.push(a[i]);
}
auto get = [&]() -> bool {
if (res.size() == 0) {
if (!st.empty()) {
res.push_back(st.top());
st.pop();
return 1;
}
if (!q.empty()) {
res.push_back(q.front());
q.pop();
return 1;
}
} else {
if (!st.empty() && st.top() <= res.back()) {
res.push_back(st.top());
st.pop();
return 1;
}
while (!q.empty() && q.front() > res.back() && st.size() < m) {
st.push(q.front());
q.pop();
}
if (!q.empty() && q.front() <= res.back()) {
res.push_back(q.front());
q.pop();
return 1;
}
}
return 0;
};
while (q.size() + st.size() > 0) {
int f = get();
// for (auto v : res) {
// cout << v << ' ';
// }
// cout << endl
// << f << endl;
if (!f || res.size() == k) {
ans.push_back(res);
res.clear();
}
}
if (res.size() != 0) {
ans.push_back(res);
}
for (auto vec : ans) {
for (int i = 0; i < vec.size(); ++i) {
cout << vec[i] << " \n"[i == vec.size() - 1];
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}
L2-042 老板的作息表
#include <bits/stdc++.h>
using namespace std;
#define int long long
using PII = pair<int, int>;
void ylh_() {
auto trans_to = [&](string s) {
int times = ((s[0] - '0') * 10 + (s[1] - '0')) * 3600 + ((s[3] - '0') * 10 + (s[4] - '0')) * 60 + ((s[6] - '0') * 10 + (s[7] - '0'));
return times;
};
auto trans_back = [&](int x) {
string s_hour, s_min, s_s;
int hour = x / 3600, min = x / 60 % 60, s = x % 60;
s_hour = ((hour < 10) ? "0" + to_string(hour) : to_string(hour));
s_min = ((min < 10) ? "0" + to_string(min) : to_string(min));
s_s = ((s < 10) ? "0" + to_string(s) : to_string(s));
string res = s_hour + ":" + s_min + ":" + s_s;
return res;
};
int n;
cin >> n;
vector<PII> a(n + 1);
for (int i = 1; i <= n; ++i) {
string s1, s2;
cin >> s1 >> s2 >> s2;
a[i] = { trans_to(s1), trans_to(s2) };
}
sort(a.begin() + 1, a.end());
auto print = [&](int t1, int t2) -> void {
cout << trans_back(t1) << " - " << trans_back(t2) << '\n';
};
if (a[1].first != 0) {
print(0ll, a[1].first);
}
for (int i = 2; i <= n; ++i) {
if (a[i].first != a[i - 1].second) {
print(a[i - 1].second, a[i].first);
}
}
const int ED = 23 * 3600 + 59 * 60 + 59;
if (a[n].second != ED) {
print(a[n].second, ED);
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}
L2-043 龙龙送外卖
怎么说呢,所有点到起点的经过边数都要走两遍,但是没说要回外卖站,所以减掉一个最深点的深度就行
#include <bits/stdc++.h>
using namespace std;
#define int long long
using PII = pair<int, int>;
void ylh_() {
int n, m;
cin >> n >> m;
vector<vector<int>> g(n + 1), fg(n + 1);
int rt = 0;
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
if (x == -1) {
rt = i;
} else {
g[i].push_back(x);
fg[x].push_back(i);
}
}
vector<int> vis(n + 1), dep(n + 1);
vis[rt] = 0;
int Maxdep = 0;
int Length = 0;
auto dfs1 = [&](auto&& dfs, int u) -> void {
for (auto v : fg[u]) {
dep[v] = dep[u] + 1;
dfs(dfs, v);
}
};
dfs1(dfs1, rt);
vis[rt] = 1;
auto dfs = [&](auto&& dfs, int u, int step) -> void {
// cout << '*' << u << endl;
if (vis[u]) {
Length += step;
return;
}
vis[u] = 1;
for (auto v : g[u]) {
dfs(dfs, v, step + 1);
}
};
for (int i = 1; i <= m; ++i) {
int x;
cin >> x;
Maxdep = max(dep[x], Maxdep);
dfs(dfs, x, 0);
cout << Length * 2 - Maxdep << '\n';
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}
L2-044 大众情人
建立距离矩阵用floyd求最短路即可
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 2e9;
void ylh_() {
int n;
cin >> n;
vector<int> sex(n + 1);
vector<vector<int>> dist(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; ++ i) {
dist[i][i] = 0;
char c;
cin >> c;
sex[i] = (c == 'M');
int k;
cin >> k;
for (int j = 1; j <= k; ++ j) {
int p, x;
cin >> p >> c >> x;
dist[i][p] = x;
}
}
for (int k = 1; k <= n; ++ k) {
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= n; ++ j) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
int Min1 = INF, Min2 = INF;
vector<int> ans1, ans2;
for (int i = 1; i <= n; ++ i) {
int Max = 0;
for (int j = 1; j <= n; ++ j) {
if (i == j) continue;
if (sex[i] == sex[j]) continue;
Max = max(dist[j][i], Max);
}
if (sex[i] == 0) {
if (Min1 > Max) {
Min1 = Max;
ans1.clear();
ans1.push_back(i);
} else if (Min1 == Max) {
ans1.push_back(i);
}
} else {
if (Min2 > Max) {
Min2 = Max;
ans2.clear();
ans2.push_back(i);
} else if (Min2 == Max) {
ans2.push_back(i);
}
}
}
for (int i = 0; i < ans1.size(); ++ i) {
if (i != 0) {
cout << ' ';
}
cout << ans1[i];
}
cout << '\n';
for (int i = 0; i < ans2.size(); ++ i) {
if (i != 0) {
cout << ' ';
}
cout << ans2[i];
}
cout << '\n';
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}

浙公网安备 33010602011771号