### 題目描述

Given a stack which can keep M numbers at most.  Push N numbers in the order of 1, 2, 3, ..., N and pop randomly.  You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack.  For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.

### 輸入描述:

Each input file contains one test case.  For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked).  Then K lines follow, each contains a pop sequence of N numbers.  All the numbers in a line are separated by a space.

### 輸出描述:

For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.

### 輸入例子:

5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2

### 輸出例子:

YES
NO
NO
YES
NO
 1 #include<iostream>
2 #include<cstdio>
3 #include<vector>
4 #include<stack>
5 #include<cstring>
6 #include<algorithm>
7 using namespace std;
8 int n,m,k;
9 int main()
10 {
11     cin>>m>>n>>k;
12     while(k--)
13     {
14         vector<int>v;
15         for(int i=0;i<n;i++)
16         {
17             int x;
18             scanf("%d",&x);
19             v.push_back(x);
20         }
21         stack<int>s;
22         int pos=0;
23         for(int i=1;i<=n;i++)
24         {
25             if(s.size()==m)
26                 break;
27             if(v[pos]==i)
28             {
29                 pos++;
30                 while(s.size()&&v[pos]==s.top())
31                 {
32                     pos++;
33                     s.pop();
34
35                 }
36             }
37             else
38                 s.push(i);
39         }
40         if(s.size())
41             cout<<"NO"<<endl;
42         else
43             cout<<"YES"<<endl;
44     }
45 }
L2-032 彩虹瓶 (25 分)

### 輸入樣例：

7 5 3
7 6 1 3 2 5 4
3 1 5 4 2 6 7
7 6 5 4 3 2 1


### 輸出樣例：

YES
NO
NO
 1 #include<iostream>
2 #include<cstdio>
3 #include<vector>
4 #include<stack>
5 #include<cstring>
6 #include<algorithm>
7 using namespace std;
8 int n,m,k;
9 int main()
10 {
11     cin>>n>>m>>k;
12     while(k--)
13     {
14         vector<int>v;
15         for(int i=0;i<n;i++)
16         {
17             int x;
18             scanf("%d",&x);
19             v.push_back(x);
20         }
21         stack<int>s;
22         int now=1;
23         for(int i=0;i<n;i++)
24         {
25             if(v[i]!=now)
26             {
27                 s.push(v[i]);
28                 if(s.size()>m)
29                     break;
30             }
31             else
32             {
33                 s.push(v[i]);
34                 while(s.size()&&s.top()==now)
35                 {
36                     s.pop();
37                     now++;
38                 }
39             }
40         }
41         if(s.size())
42             cout<<"NO"<<endl;
43         else
44             cout<<"YES"<<endl;
45     }
46 }