2 条题解

  • 0
    @ 2025-11-8 20:56:21
    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    void solve() {
        int n; cin >> n;
        int c[n+1] = {0};
        for(int i = 1; i <= n; i++) cin >> c[i];
        stack<int> a,b;
        a.push(c[1]);
        int cnt = 0, h = 0;
        for(int i = 2; i <= n; i++){
            if(c[i] < a.top()) {
                a.push(c[i]);
            }else{
                if(b.empty() || c[i] > b.top()){
                    b.push(c[i]);
                }else {
                    h = max(h, (int)a.size());
                    while (!a.empty()) {
                        a.pop();
                    }
                    cnt++;
                    while (!b.empty()&&b.top() > c[i]) {
                        a.push(b.top());
                        b.pop();
                    }
                    a.push(c[i]);
                }
            }
        }
        int lena = a.size();
        int lenb = b.size();
        h = max({ h,lena,lenb });
        if (a.size()) cnt++;
        if (b.size()) cnt++;
        cout << cnt << " " << h << endl;
    }
    signed main() {
        ios::sync_with_stdio(0);
        cin.tie(0), cout.tie(0);
        int T = 1;
    //    cin >> T;
        while (T--) solve();
        
        return 0;
    }
    
    • 0
      @ 2025-11-8 20:32:18

      按照题意模拟
      #include<bits/stdc++.h>
      using namespace std;
      int c[200100];
      int main(){
      int n;
      scanf("%d",&n);
      for(int i=1;i<=n;i++){
      scanf("%d",&c[i]);
      }
      stack<int>a,b,d;
      a.push(c[1]);
      int ans=0,max1=-1;
      for(int i=2;i<=n;i++){
      if(c[i]<a.top()){
      a.push(c[i]);
      continue;
      }
      int z=b.size();
      if(z==0){
      b.push(c[i]);
      continue;
      }
      if(c[i]>b.top()){
      b.push(c[i]);
      continue;
      }
      int z1=a.size();
      max1=max(max1,z1);
      ans++;
      while(!a.empty()){
      a.pop();
      }
      while(!b.empty()){
      int z=b.top();
      if(z>c[i]){
      a.push(z);
      b.pop();
      }else{
      d.push(z);
      b.pop();
      }
      }
      a.push(c[i]);
      while(!d.empty()){
      int z=d.top();
      b.push(z);
      d.pop();
      }
      }
      int a2=a.size(),b2=b.size();
      if(a2>0){
      ans++;
      }
      if(b2>0){
      ans++;
      }
      max1=max(a2,max1);
      max1=max(b2,max1);
      printf("%d %d",ans,max1);
      return 0;
      }

      • 1

      信息

      ID
      1186
      时间
      1000ms
      内存
      256MiB
      难度
      7
      标签
      递交数
      67
      已通过
      15
      上传者