3 条题解

  • 0
    @ 2025-4-6 19:51:44
    #include <iostream>
    #include <deque>
    using namespace std;
    
    const int N = 1000010;
    int a[N];
    
    int main() {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
    
        deque<int> q;
        for (int i = 1; i <= n; i++) {
            while (!q.empty() && a[q.back()] <= a[i]) q.pop_back();
            q.push_back(i);
        }
    
        while (!q.empty()) {
            cout << a[q.front()] << " ";
            q.pop_front();
        }
    
        return 0;
    }
    
    • 0
      @ 2025-3-19 21:49:39
      #include <bits/stdc++.h>
      #define endl '\n'
      using namespace std;
      typedef pair<int,int> aII;  
      using ll = long long;
      using ULL = unsigned long long;
      const int N = 1e6+5;
      
      int n, a[N], q[N];
      int hh = 0, tt = -1;
      inline void solve() { 
          cin >> n;
          for (int i = 1; i <= n; i++) cin >> a[i];
          for(int i = 1;i <= n;i++) {
              while (hh <= tt && a[q[tt]] <= a[i]) --tt;
              q[++tt] = i;
          }
          for (int i = hh; i <= tt; i++) cout << a[q[i]] << ' '; 
      
      }
      int main() { 
          ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
          int _ = 1; 
          //int _; cin >> _;
          while (_--) solve();
          return 0;
      }
      
      • 0
        @ 2024-4-10 18:31:01

        单调队列板子

        #include <bits/stdc++.h>
        using namespace std;
        const int N = 1000010;
        int a[N];
        int n;
        deque<int> q;
        int main(){
            scanf("%d",&n);
            for (int i = 1; i <= n; ++i) {
                scanf("%d",&a[i]);
            }
            for (int i = 1; i <= n; ++i) {
                while(!q.empty() && a[q.back()] <= a[i]) q.pop_back();
                q.push_back(i);
            }
            for (int i = 0; i <= q.size() - 1; ++i) {
                printf("%d ",a[q[i]]);
            }
            return 0;
        }
        
        • 1

        信息

        ID
        103
        时间
        1000ms
        内存
        256MiB
        难度
        6
        标签
        递交数
        190
        已通过
        54
        上传者