AcWing 1535. 弹出序列
原题链接
中等
作者:
王小强
,
2021-03-03 11:24:02
,
所有人可见
,
阅读 364
Greedy Algorithm Solution
#include <iostream>
#include <vector>
#include <stack>
#include <numeric>
using namespace std;
int m, n, k; // m == capacity
// Greedy Algorithm Solutions
bool check(vector<int>& pushed, vector<int>& popped) {
stack<int> s;
int j = 0;
for (const auto& x : pushed) {
if (s.size() < m) s.emplace(x);
while (not s.empty() && s.top() == popped[j]) {
s.pop();
++j;
}
}
return j == popped.size();
}
int main(void) {
scanf("%d %d %d", &m, &n, &k);
vector<int> pushed(n);
iota(begin(pushed), end(pushed), 1);
while (k--) {
vector<int> popped(n);
for (int i = 0; i < n; ++i) cin >> popped[i];
puts(check(pushed, popped) ? "YES" : "NO");
}
}