cdoj 847 方老师与栈 火车进出战问题

时间:2022-05-20 08:50:53

//其实我是不想写这题的,但是这题让我想起了我年轻的时候

解法:直接模拟栈就好。

//另外我年轻时候做的那题数据范围比较小,原理也不一样。

//对于序列中的任何一个数其后面所有比它小的数应该是倒序的,因此对于任意三个数a,b,c(按顺序),若b<a c<a 则有b>c

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<string> using namespace std; int n;
int a[];
int b[];
int stk[];
int top=;
int nowb=; int main(){
scanf("%d",&n);
for (int i=;i<n;i++) scanf("%d",&a[i]);
for (int i=;i<n;i++) scanf("%d",&b[i]);
for (int i=;i<n;i++){
stk[++top]=a[i];
while (top> && stk[top]==b[nowb]){
top--;
nowb++;
}
}
if (nowb==n)
printf("Yes\n");
else
printf("No\n");
return ;
}
/*
3
3 2 1
1 2 3 4
1 2 3 4
3 1 2 4
*/