NC14326. Rails
描述
输入描述
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.
输出描述
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.
示例1
输入:
5 1 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 0
输出:
Yes No Yes
Python3 解法, 执行用时: 34ms, 内存消耗: 4588K, 提交时间: 2023-04-08 21:12:22
a=[i+1 for i in range(1000)] def shabi(n): s=[] t=0 for i in range(n): s.append(a[i]) while s and b[t]==s[len(s)-1]: s.pop() t+=1 return not s while True: n=int(input()) if n==0: break while True: b=list(map(int,input().split())) if b[0]==0: break if shabi(n):print("Yes") else:print("No") print()
C++(g++ 7.5.0) 解法, 执行用时: 3ms, 内存消耗: 304K, 提交时间: 2022-10-10 20:26:15
#include <bits/stdc++.h> using namespace std; const int N=1e5+10; int a[N],n; int main(){ while(cin>>n,n!=0){ while(cin>>a[0],a[0]!=0){ for(int i=1;i<n;i++) cin>>a[i]; stack<int> st; for(int i=0,k=1; k<=n;k++){ st.push(k); while(st.size()&&st.top()==a[i]) st.pop(),i++; } if(st.size()) cout<<"No"; else cout<<"Yes"; cout<<endl; } cout<<endl; } return 0; }
C++ 解法, 执行用时: 3ms, 内存消耗: 312K, 提交时间: 2022-05-18 15:46:40
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; int a[N],n; int main(void) { while(cin>>n,n!=0) { while(cin>>a[0],a[0]!=0) { for(int i=1;i<n;i++) cin>>a[i]; stack<int>st; for(int i=0,k=1;k<=n;k++) { st.push(k); while(st.size()&&st.top()==a[i]) st.pop(),i++; } if(st.size()) puts("No"); else puts("Yes"); } cout<<endl; } return 0; }