UVA 514 铁轨 (Rails)
程序员文章站
2022-04-09 12:48:02
...
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int main() {
int N, nums[1005];
while(scanf("%d", &N) && N) {
while(scanf("%d", &nums[1]) && nums[1]) {
for(int i = 2; i <= N; i++) scanf("%d", &nums[i]);
int flag = 1;
stack<int> S;
// 第j节车厢的出栈顺序nums[i] A用j表示,B用nums[i]表示,C用栈表示
for(int i = 1, j = 1; i <= N && flag; ) {
if(j == nums[i]) i++, j++; // B要的A可以给
else if(!S.empty() && S.top() == nums[i]) S.pop(), i++; // B要的C可以给
else if(j <= N) S.push(j++); // B要的AC都给不了,但后续有可能C可以给,前提是A还有!
else flag = 0; //
}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
cout<<endl;
}
return 0;
}