在哪里可以找到做网站的公司,wordpress下拉,微问数据平台入口,烟台消防建设信息网站同个人网站 https://www.serendipper-x.cn/#xff0c;欢迎访问 #xff01;
问题描述#xff1a; 某城市有一个火车站#xff0c;铁轨铺设如图所示。有n节车厢从A方向驶入车站#xff0c;按进站顺序编号为 1~n 。你的任务是判断是否能让它们按照某种特定的顺序进入 B 方…同个人网站 https://www.serendipper-x.cn/欢迎访问
问题描述 某城市有一个火车站铁轨铺设如图所示。有n节车厢从A方向驶入车站按进站顺序编号为 1~n 。你的任务是判断是否能让它们按照某种特定的顺序进入 B 方向的铁轨并驶出车站。例如出栈顺序5 4 1 2 3是不可能的但5 4 3 2 1是可能的。
为了重组车厢你可以借助中转站 C。这是一个可以停放任意多节车厢的车站但由于末端封顶驶入 C 的车厢必须按照相反的顺序驶出 C。对于每个车厢一旦从 A 移入 C 就不能再回到 A 了一旦从 C 移入 B就不能回到 C 了。换句话说在任意时刻只能由两种选择 A - C 和 C - B。
分析 在这里中转站 C 符合先进后出的原则可以把它当作是栈。
C:
#includecstdio
#includestack
using namespace std;
const int MAXN 1000 10;int n, target[MAXN];int main(){while(scanf(%d, n) 1){stackint s;int A 1, B 1;for(int i 1; i n; i)scanf(%d, target[i]);int ok 1;while(B n){if(A target[B]){A;B;}else if(!s.empty() s.top() target[B]){s.pop();B;}else if(A n)s.push(A);else{ok 0;break;}}printf(%s\n, ok ? Yes : No);}return 0;
}Python:
n int(input())
target list(map(int, input().split())) # 测试队列
stack []ok, A, B 1, 1, 0
while B n:if A target[B]:A 1B 1elif len(stack) ! 0 and stack[len(stack)-1] target[B]:stack.pop()B 1elif A n:stack.append(A)A 1else:ok 0break
if ok 1:print (Yes)
else:print (No)