天梯赛L2题解(045-048)
L2-045 堆宝塔
不是很复杂的模拟,依照题意写就行了
#include <bits/stdc++.h>
using namespace std;
#define int long long
void ylh_() {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
stack<int> st1, st2;
int ans1 = 0, ans2 = 0;
for (int i = 1; i <= n; ++i) {
if (st1.empty()) {
st1.push(a[i]);
} else {
if (a[i] < st1.top()) {
st1.push(a[i]);
} else {
if (st2.empty()) {
st2.push(a[i]);
} else {
if (a[i] > st2.top()) {
st2.push(a[i]);
} else {
++ans1;
ans2 = max((int)st1.size(), ans2);
while (!st1.empty()) {
st1.pop();
}
while (!st2.empty() && st2.top() > a[i]) {
st1.push(st2.top());
st2.pop();
}
st1.push(a[i]);
}
}
}
}
}
if (st2.size()) {
++ans1;
ans2 = max((int)st2.size(), ans2);
}
if (st1.size()) {
++ans1;
ans2 = max((int)st1.size(), ans2);
}
cout << ans1 << ' ' << ans2;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}
L2-046 天梯赛的赛场安排
题目依旧讲不清白
每个学校的监考人数是学校的学生分配去的考场数
因为只要遇见大于c的就直接开一个考场塞满,对答案没影响,这一部分可以优化掉,就不会超时
#include <bits/stdc++.h>
using namespace std;
#define int long long
using PII = pair<int, int>;
void ylh_() {
int n, c;
cin >> n >> c;
int ans = 0;
vector<string> name(n + 1);
vector<PII> num(n + 1);
vector<int> cnt(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> name[i] >> num[i].first;
cnt[i] += num[i].first / c;
ans += cnt[i];
num[i].first %= c;
num[i].second = i;
}
vector<int> Room;
sort(num.begin() + 1, num.end(), greater<PII>());
for (int i = 1; i <= n; ++i) {
auto [val, idx] = num[i];
if (val == 0)
continue;
int find = 0;
for (auto& person : Room) {
if (person + val <= c) {
person += val;
find = 1;
cnt[idx]++;
break;
}
}
if (!find) {
Room.push_back(val);
cnt[idx]++;
}
}
for (int i = 1; i <= n; ++i) {
cout << name[i] << ' ' << cnt[i] << '\n';
}
cout << ans + Room.size();
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}
L2-047 锦标赛
#include <bits/stdc++.h>
using namespace std;
#define int long long
using PII = pair<int, int>;
void ylh_() {
int t;
cin >> t;
vector<PII> tr(1 << 20); // PII第一维表示败者,二维表示胜者,按照每场比赛为节点建立完美二叉树
for (int i = 1; i <= t; ++i) {
for (int j = (1 << (t - i)); j <= (1 << (t - i + 1)) - 1; ++j) {
cin >> tr[j].first;
}
}
cin >> tr[1].second;
auto dfs = [&](auto&& dfs, int u) -> bool {
if (u > ((1 << t) - 1))
return true;
if (tr[u].first > tr[u].second) {
return false;
}
tr[u << 1].second = tr[u].first;
tr[(u << 1) + 1].second = tr[u].second;
if (dfs(dfs, u << 1) && dfs(dfs, (u << 1) + 1))
return true;
swap(tr[u << 1].second, tr[(u << 1) + 1].second);
if (dfs(dfs, u << 1) && dfs(dfs, (u << 1) + 1))
return true;
return false;
};
if (dfs(dfs, 1)) {
for (int i = (1 << t - 1); i <= (1 << t) - 1; ++i) {
if (i != (1 << t) - 1) {
cout << tr[i].first << ' ' << tr[i].second << ' ';
} else {
cout << tr[i].first << ' ' << tr[i].second;
}
}
} else {
cout << "No Solution";
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}
L2-048 寻宝图
纯血经典搜索题
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };
void ylh_() {
int n, m;
cin >> n >> m;
vector<string> a(n + 1);
vector<vector<int>> vis(n + 1, vector<int>(m + 1));
int find = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
a[i] = " " + a[i];
}
auto dfs = [&](auto&& dfs, int x, int y) -> void {
if (vis[x][y])
return;
if (a[x][y] != '1') {
find = 1;
}
vis[x][y] = 1;
for (int i = 0; i < 4; ++i) {
int nx = dx[i] + x;
int ny = dy[i] + y;
if (nx < 1 || nx > n || ny < 1 || ny > m)
continue;
if (a[nx][ny] == '0') {
continue;
}
dfs(dfs, nx, ny);
}
};
int ans1 = 0, ans2 = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (a[i][j] != '0' && !vis[i][j]) {
++ans1;
find = 0;
dfs(dfs, i, j);
ans2 += find;
}
}
}
cout << ans1 << ' ' << ans2;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}

浙公网安备 33010602011771号