1 solutions

  • 0
    @ 2022-1-2 19:06:53

    视频讲解:https://www.bilibili.com/video/BV1E44y1E7Jc?share_source=copy_web

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<stack>
    using namespace std;
    stack <int> s;
    int main(){
    	ios::sync_with_stdio(false);
    	int t,n,x,ans=0;
    	cin>>t;
    	for(int i=1;i<=t;i++){
    		// stack <int> s;
    		ans=0;
    		cin>>n;
    		for(int j=0;j<n;j++){
    			cin>>x;
    			while(!s.empty()&&s.top()>x){
    				ans++;
    				s.pop();
    			}
    			if(x==0)continue;
    			if(s.empty()||x>s.top()){
    				s.push(x);
    			}
    		}
    		while(!s.empty()){
    			ans++;
    			s.pop();
    		}
    		cout<<"Case "<<i<<": "<<ans<<endl;
    	}
    
    
    	return 0;
    }
    
    
    • 1

    Information

    ID
    1527
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    70
    Accepted
    14
    Uploaded By