using namespace std;
const int maxn = 1000 + 10;
int n, target[maxn];
int main() {
scanf_s("%d", &n);
for (int i = 0; i < n; i++) {
scanf_s("%d", &target[i]);
}
// 从A驶入的车,从B驶出的车
int A = 0, B = 0;
stack<int> s;
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);
A++;
}
else {
ok = 0;
break;
}
}
printf_s("%s", ok ? "yes" : "no");
}
例题6-2 铁轨 uva514
最新推荐文章于 2021-03-02 20:37:35 发布