【POJ1363】栈

B - II
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu

Description

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track. 

The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, ..., N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, ..., aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station. 

Input

The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, ..., N. The last line of the block contains just 0. 

The last block consists of just one line containing 0.

Output

The output contains the lines corresponding to the lines with permutations in the input. A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null'' block of the input.

Sample Input

5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

Sample Output

Yes
No

Yes
 
    
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<cstdio>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
	int a[1000];
	int n;
bool t;
while(~scanf("%d",&n)&&n)
{
	while(scanf("%d",&a[1]))
	{
		int i=1,j=1;
		t=true;
		stack<int >sta;
		if(a[1]==0)
		{
			printf("\n");
			break;
		}
		else
		{
			for(int k=2;k<=n;k++)
			scanf("%d",&a[k]);
		}
		while(j<=n)
		{
			if(i==a[j])
			{
				i++;
				j++;
			}
			else if(!sta.empty()&&sta.top()==a[j])
			{
				sta.pop();
				j++;
			}
			else if(i<=n)
			sta.push(i++);
			else
			{
				t=false;
				break;
			}
		}
		if(t)
		printf("Yes\n");
		else
		printf("No\n");
	}
}
	return 0;
}


先输入火车车厢数N,然后输入N个数代表车厢编号a[i],判断火车是否能按1~N或者N~1的顺序开出,a[0] = 0;代表车厢数为N的循环结束,N = 0;代表程序结束。
思路:    
先输入车厢编号,用数组a[i]保存,在从1~N依次输入栈st中,依次判断st.top()是否等于[i].
1 #include<cstdio>
 2 #include<stack>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,a[1500],i,j;
 7     while(scanf("%d",&n) && n)
 8     {
 9         stack<int>st;
10         while(scanf("%d",&a[0]) && a[0])
11         {
12             for( i = 1 ; i < n ; i++)
13             {
14                 scanf("%d",&a[i]);
15             }
16             for( i = 1 , j = 0 ; i <= n ; i++)
17             {
18                 st.push(i);
19                 while(st.top() == a[j])
20                 {
21                     j++;
22                     st.pop();
23                     if(st.empty())    //栈为空时结束循环
24                         break;
25                 }
26             }
27             if( j == n) printf("Yes\n");
28             else
29                 printf("No\n");
30         }
31         printf("\n");
32     }
33 }

<think>好的,我现在需要帮助用户了解的基本操作以及与POJ相关的实现示例。首先,我要回顾的定义和基本操作,确保自己理解正确。是一种后进先出(LIFO)的数据结构,主要操作有push、pop、top、isEmpty和size。用户提到了POJ,可能是指北京大学的在线评测系统,所以需要提供相关的题目示例。 根据用户提供的引用内容,引用[2]和[3]详细说明了的基本操作和STL中的实现方式。特别是引用[3]中的代码示例,展示了如何使用C++的STL,包括头文件引入、定义、push、pop、top等操作。这可能对用户有帮助。 接下来,我需要考虑用户可能需要的POJ题目。常见的相关题目有括号匹配、表达式求值等。比如POJ 2559(直方图中最大的矩形),通常用单调解决,这也是引用[3]中提到的单调的应用。不过用户可能希望看到更基础的示例,比如括号匹配,这在POJ中可能有对应的题目,比如POJ 3991。 需要确保示例代码的正确性,并解释清楚每个步骤。同时,根据用户要求,所有行内数学表达式要用$...$,但这里可能涉及较少数学内容,主要关注代码和操作步骤。另外,需要生成相关问题,帮助用户进一步学习。 可能的问题点:用户是否熟悉C++的STL?是否需要其他语言的示例?但用户的问题中提到了POJ,通常使用C++较多,所以用C++示例是合适的。另外,需要确保引用的正确引用标识,比如在解释的应用时引用对应的文献。 最后,检查是否符合系统级指令,如格式、引用标识、问题生成等。确保回答结构清晰,分点说明,并包含代码示例和相关问题。</think>### 的基本操作与POJ实现示例 (Stack)是一种**后进先出(LIFO)**的线性数据结构,核心操作包括: 1. **`push`**:将元素压入顶,时间复杂度$O(1)$; 2. **`pop`**:移除顶元素,时间复杂度$O(1)$; 3. **`top`**:获取顶元素,时间复杂度$O(1)$; 4. **`isEmpty`**:判断是否为空; 5. **`size`**:返回中元素数量[^2][^3]。 --- ### 的C++ STL实现示例 ```cpp #include <iostream> #include <stack> using namespace std; int main() { stack<int> s; s.push(10); // :10 s.push(20); // :10 → 20 cout << "顶元素: " << s.top() << endl; // 输出20 s.pop(); // 移除20,顶变为10 cout << "大小: " << s.size() << endl; // 输出1 return 0; } ``` --- ### POJ相关题目实现示例 #### **POJ 2559: 直方图中最大矩形** **问题描述**:给定直方图的高度数组,计算其能形成的最大矩形面积。 **实现思路**:使用**单调**快速找到每个高度向左右扩展的边界[^3]。 ```cpp #include <cstdio> #include <stack> using namespace std; const int MAXN = 1e5+5; int height[MAXN]; long long maxArea(int n) { stack<int> s; long long max_area = 0; for (int i = 0; i <= n; i++) { while (!s.empty() && (i == n || height[s.top()] > height[i])) { int h = height[s.top()]; s.pop(); int width = s.empty() ? i : i - s.top() - 1; max_area = max(max_area, (long long)h * width); } s.push(i); } return max_area; } ``` --- #### **POJ 3991: 括号匹配** **问题描述**:给定字符串,判断其括号是否合法匹配。 **实现思路**:用记录未匹配的左括号,遇到右括号时弹出顶匹配。 ```cpp #include <cstdio> #include <stack> using namespace std; bool isValid(string s) { stack<char> stk; for (char c : s) { if (c == '(') stk.push(c); else if (c == ')') { if (stk.empty()) return false; stk.pop(); } } return stk.empty(); } ``` --- ### 的应用场景 1. **函数调用**:保存函数调用时的返回地址和局部变量; 2. **表达式求值**:中缀转后缀表达式,如计算$3 + 5 \times (2 - 6)$; 3. **撤销操作**:编辑器中的“撤销”功能依赖记录操作历史; 4. **浏览器历史记录**:通过实现前进/后退功能[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值