//1051. Pop Sequence (25)
//一定记得进行完之后要将所有的元素都pop出来才能得到正确结果。
//acc
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int M, N, K;
cin >> M >> N >> K;
if (M == 0)
{
cout << "NO";
return 0;
}
stack<int> s;
int i;
int tmp;
int num = 1;
int j;
int t;
bool flag = false;
int popnum;
for (i = 0;i<K;i++)
{
for (j = 0;j<N;j++)
{
cin >> tmp;
if (tmp - num + 1 <= M - s.size())
{
for (t = num; t <= tmp;t++)
{
s.push(t);
}
num = tmp + 1;
if (s.size() > 0)
{
popnum = s.top();
if (popnum == tmp)
{
s.pop();
}
}
}
else
{
if (s.size() > 0 && s.top() == tmp)
{
s.pop();
}
else
{
for (;j<N-1;j++)
{
cin >> tmp;
}
flag = true;
cout << "NO"<<endl;
break;
}
}
}
if (flag == false)
{
cout << "YES"<<endl;
}
num = 1;
flag = false;
while(s.empty() == false)
s.pop();
}
return 0;
}
PAT 1051. Pop Sequence (25)
最新推荐文章于 2022-08-06 23:50:21 发布