Element Extermination

题意:
给你n大小的数组排列,现在如果有俩个连续的数满足ai < ai+1,那么你可以删去其中的一个数,现在问:在进行上述操作后,是否有可能使得数组元素为1

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<stack>
 7 #include <bitset>
 8 #include<set>
 9 #include<map>
10 #include<unordered_map>
11 #include<vector>
12 #include<cmath>
13 #include<string>
14 using namespace std;
15 typedef long long ll;
16 #define inf 99999999
17 int t, n;
18 
19 int main() {
20     cin >> t;
21     int num;
22     while (t--) {
23         int flag = 0;
24         cin >> n;
25         stack<int>s;
26         while (n--) {
27             cin >> num;
28             if (s.empty()) {
29                 s.push(num);// 如果栈为空,那么直接进栈
30             }
31             else if (s.top() < num) {//可删除  5  9
32                 if (s.size() == 1) {
33                     continue;//如果现在的栈中元素个数为1,那么保留更小的数
34                 }
35                 else {
36                     s.push(num);//否则的话我们先入栈 10 8 6 5 9
37                 }
38                 while (1) {
39                     int a = s.top(); s.pop();
40                     int b = s.top(); s.pop();
41                     if (a > b) {
42                         if (s.size() >= 1) s.push(a);
43                         else s.push(b);
44                     }
45                     else {
46                         //原路返回
47                         s.push(b); s.push(a);
48                         break;
49                     }
50                     if (s.size() == 1) break;
51                 }
52             }
53             else { //如果不满足删除条件,那么入栈
54                 s.push(num);
55             }
56         }
57         s.size() == 1 ? cout << "YES" << endl : cout << "NO" << endl;
58     }
59     return 0;
60 }

原作者写的更详细https://blog.csdn.net/moasad/article/details/107135018

原文地址:https://www.cnblogs.com/0211ji/p/13385757.html