POJ 1737 Rails(栈)

本文介绍了一道关于火车车厢重组的问题,通过栈的模拟操作判断是否能够实现特定的车厢顺序。利用两个栈进行逆向操作验证可行性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目来源:http://poj.org/problem?id=1737

Rails

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 37573

Accepted: 14550

Description

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


The local tradition is that every train arriving from the direction A continuesin the direction B with coaches reorganized in some way. Assume that the trainarriving from the direction A has N <= 1000 coaches numbered in increasingorder 1, 2, ..., N. The chief for train reorganizations must know whether it ispossible to marshal coaches continuing in the direction B so that their orderwill be a1, a2, ..., aN. Help him and write a program that decides whether itis possible to get the required order of coaches. You can assume that singlecoaches can be disconnected from the train before they enter the station andthat 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 asnecessary in the station. But once a coach has entered the station it cannotreturn to the track in the direction A and also once it has left the station inthe direction B it cannot return back to the station. 

Input

The input consists of blocks of lines. Eachblock except the last describes one train and possibly more requirements forits reorganization. In the first line of the block there is the integer Ndescribed above. In each of the next lines of the block there is a permutationof 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 tothe lines with permutations in the input. A line of the output contains Yes ifit is possible to marshal the coaches in the order required on thecorresponding line of the input. Otherwise it contains No. In addition, thereis 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 theinput.

Sample Input

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

SampleOutput

Yes
No

Yes

Source

CentralEurope 1997

 -----------------------------------------------------

思路

【题意】

给出一个从1~N的升序序列的若干排列,问这些排列能否由该升序序列经过一个栈的出栈入栈操作得到。

【题解】

栈的模拟题。注意到上述过程是可逆的,用2个栈来求排列的逆,如果排列的逆能恢复到原序列,则Yes,否则No.

首先定义一些记号:

1. 数组arr:存放新序列

2. sta:中间结果

3. cont:恢复的序列

恢复的规则就是把从原序列到新序列的操作过程倒过来,并把pushpop互换:

arr[n-1]入栈sta;

从右到左扫描arr的元素,sta栈顶元素出栈直到sta栈空或arr中当前元素大于sta栈顶元素,sta中出栈的元素按出栈顺序依次入栈cont,然后arr中当前元素入栈sta;

最后sta如果不空的话从栈顶依次出栈并入栈cont直到sta空。

如此一来cont中存放的应该正好是倒序的原序列。

下面用2个例子说明这种可逆性。

Yes的例子

原序列

新序列

 

1 2 3 4 5 6

1 2 3 6 5 4

 

原序列到新序列

新序列到原序列(倒过来看)

恢复原序列

Push 1

Pop 1

Push 1

Pop 1

Push 1

 

Push 2

Pop 2

Push 2

Pop 2

Push 2

 

Push 3

Pop 3

Push 3

Pop 3

Push 3

 

Push 4

Pop 4

Push 4

Push 5

Pop 5

Push 5

Push 6

Pop 6

Push 6

Pop 6

Push 6

 

Pop 5

Push 5

 

Pop 4

Push 4

 

 

2. No的例子

原序列

新序列

 

1 2 3 4 5

5 4 1 2 3

 

原序列到新序列

新序列到原序列

恢复原序列

Null

Pop 4

Push 4

Null

Pop 5

Push 5

Null

Push 5

 

Null

Push 4

 

Null

Pop 1

Push 1

Null

Push 1

 

Null

Pop 2

Push 2

Null

Push 2

 

Null

Pop 3

Push 3

Null

Push 3

 

 

这道题要特别注意的是输出”Yes/No”,而不是通常的”YES/NO”.

-----------------------------------------------------

代码 

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

int n;
vector<int> arr;
vector<int> sta;
vector<int> cont;

void test()
{
	if (!sta.empty())
	{
		sta.clear();
	}
	if (!cont.empty())
	{
		cont.clear();
	}
	int i = n-1, elem, poped;
	sta.push_back(arr.at(i--));
	while (i>=0)
	{
		elem = arr.at(i--);
		while (!sta.empty() && elem < sta.back())
		{
			poped = sta.back();
			sta.pop_back();
			cont.push_back(poped);
		}
		sta.push_back(elem);
	}
	while (!sta.empty())
	{
		poped = sta.back();
		sta.pop_back();
		cont.push_back(poped);
	}
	i = 1;
	for (i=0; i<n; i++)
	{
		if (cont.at(i) != n-i)
		{
			cout << "No" << endl;
			return;
		}
	}
	cout << "Yes" << endl;
	return;
}


int main()
{
#ifndef ONLINE_JUDGE
	ifstream fin ("1363.txt");
	int i,tmp;
	while (fin >> n)
	{
		if (n==0)
		{
			break;
		}
		i = 0;
		while (fin >> tmp)
		{
			if (tmp==0)
			{
				break;
			}
			arr.push_back(tmp);
			i++;
			if (i==n)
			{
				test();
				i = 0;
				arr.clear();
			}
		}
		cout << endl;
	}
	fin.close();
	return 0;
#endif
#ifdef ONLINE_JUDGE
	int i,tmp;
	while (cin >> n)
	{
		if (n==0)
		{
			break;
		}
		i = 0;
		while (cin >> tmp)
		{
			if (tmp==0)
			{
				break;
			}
			arr.push_back(tmp);
			i++;
			if (i==n)
			{
				test();
				i = 0;
				arr.clear();
			}
		}
		cout << endl;
	}
#endif
}


内容概要:本文围绕直流微电网中带有恒功率负载(CPL)的DC/DC升压转换器的稳定控制问题展开研究,提出了一种复合预设性能控制策略。首先,通过精确反馈线性化技术将非线性不确定的DC转换器系统转化为Brunovsky标准型,然后利用非线性扰动观测器评估负载功率的动态变化和输出电压的调节精度。基于反步设计方法,设计了具有预设性能的复合非线性控制器,确保输出电压跟踪误差始终在预定义误差范围内。文章还对比了多种DC/DC转换器控制技术如脉冲调整技术、反馈线性化、滑模控制(SMC)、主动阻尼法和基于无源性的控制,并分析了它们的优缺点。最后,通过数值仿真验证了所提控制器的有效性和优越性。 适合人群:从事电力电子、自动控制领域研究的学者和工程师,以及对先进控制算法感兴趣的研究生及以上学历人员。 使用场景及目标:①适用于需要精确控制输出电压并处理恒功率负载的应用场景;②旨在实现快速稳定的电压跟踪,同时保证系统的鲁棒性和抗干扰能力;③为DC微电网中的功率转换系统提供兼顾瞬态性能和稳态精度的解决方案。 其他说明:文中不仅提供了详细的理论推导和算法实现,还通过Python代码演示了控制策略的具体实现过程,便于读者理解和实践。此外,文章还讨论了不同控制方法的特点和适用范围,为实际工程项目提供了有价值的参考。
内容概要:该论文介绍了一种名为偏振敏感强度衍射断层扫描(PS-IDT)的新型无参考三维偏振敏感计算成像技术。PS-IDT通过多角度圆偏振光照射样品,利用矢量多层光束传播模型(MSBP)和梯度下降算法迭代重建样品的三维各向异性分布。该技术无需干涉参考光或机械扫描,能够处理多重散射样品,并通过强度测量实现3D成像。文中展示了对马铃薯淀粉颗粒和缓步类动物等样品的成功成像实验,并提供了Python代码实现,包括系统初始化、前向传播、多层传播、重建算法以及数字体模验证等模块。 适用人群:具备一定光学成像和编程基础的研究人员,尤其是从事生物医学成像、材料科学成像领域的科研工作者。 使用场景及目标:①研究复杂散射样品(如生物组织、复合材料)的三维各向异性结构;②开发新型偏振敏感成像系统,提高成像分辨率和对比度;③验证和优化计算成像算法,应用于实际样品的高精度成像。 其他说明:PS-IDT技术相比传统偏振成像方法具有明显优势,如无需干涉装置、无需机械扫描、可处理多重散射等。然而,该技术也面临计算复杂度高、需要多角度数据采集等挑战。文中还提出了改进方向,如采用更高数值孔径(NA)物镜、引入深度学习超分辨率技术等,以进一步提升成像质量和效率。此外,文中提供的Python代码框架为研究人员提供了实用的工具,便于理解和应用该技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值