AcWing 4472. 插松枝
原题链接
中等
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
typedef long long ll;
using namespace std;
const int N = 5e4 + 10;
const int Mod = 998244353;
vector<int> line, box;
int n, m, k;
int main() {
cin >> n >> m >> k;
vector<int> now;
line.resize(n);
for (int i = n - 1; i >= 0; i--) cin >> line[i];
int ind = n - 1;
while (true) {
//盒子不为空,并且符合大小条件,松枝没有插满, 松枝为空不考虑大小关系
if (!box.empty() && now.size() < k && (now.empty() || box[box.size() - 1] <= now[now.size() - 1])) {
now.push_back(box[box.size() - 1]);//盒子最上面的插入松枝,并且弹出
box.pop_back();
}
//推送带不为空,并且符合大小条件,松枝没有插满,松枝为空不考虑大小关系
else if (!line.empty() && (now.size() < k) && (now.empty() || line[ind] <= now[now.size() - 1])) {
now.push_back(line[ind--]);//推送器上的插入松枝,弹出
line.pop_back();
}
//推送带不为空,推送带的也不满足大小要求,如果盒子没满,放入盒子内,满了就制作下一个松枝了
else if (!line.empty() && line[ind] > now[now.size() - 1] && box.size() < m) {
box.push_back(line[ind--]);
line.pop_back();
}
if ((now.size() == k) || (box.size() == m && (line.empty() || line[ind] > now[now.size() - 1]))) {
for (int i = 0; i < now.size(); i++) cout << now[i] << " ";
cout << endl;
now.erase(now.begin(), now.end());
}
else if ((!box.empty() && box[box.size() - 1] > now[now.size() - 1] && line.empty())) {
for (int i = 0; i < now.size(); i++) cout << now[i] << " ";
cout << endl;
now.erase(now.begin(), now.end());
}
else if (box.empty() && line.empty()) {
for (int i = 0; i < now.size(); i++) cout << now[i] << " ";
cout << endl;
now.erase(now.begin(), now.end());
}
if (line.empty() && box.empty()) break;
}
}