1 条题解

  • 0
    @ 2025-4-1 9:17:41

    code

    #include <bits/stdc++.h>
    using namespace std;
    
    bool mlest;
    
    double tlest, tleed;
    
    int R(){
    	int x = 0, f = 1; char ch = getchar();
    	while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); }
    	while(isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
    	return x * f;
    }
    
    void W(int x){
    	if(x < 0) { x = -x; putchar('-'); }
    	if(x > 9) W(x/10); putchar(x%10+'0');
    }
    
    const int N = 1e5 + 10;
    
    int n, a[N];
    
    int st[N], top;
    
    int r[N], tmp[N], l[N];
    
    bool k;
    
    void read(){
    	n = R();
    	for(int i = 1;i <= n; i++) {
    		tmp[i] = a[i] = R();
    		if(a[i] == 0) k = 1;
    	}
    	sort(tmp+1,tmp+1+n);
    	int m = unique(tmp + 1, tmp + 1 + n) - tmp - 1;
    	for(int i = 1;i <= n; i++) a[i] = lower_bound(tmp + 1, tmp + 1 + m, a[i]) - tmp;
    }
    
    void init(){
    	for(int i = 1;i <= n; i++){
    		if(!l[a[i]]) l[a[i]] = i;
    		r[a[i]] = i;
    	}
    }
    
    void compute(){
    	int ans = 0;
    	for(int i = 1;i <= n; i++){
    		if(a[i] == 1 && k){
    			if(top){
    				W(-1);
    				return ;
    			}
    			continue;
    		}
    		if(i == l[a[i]]){
    			st[top++] = a[i];
    			ans = max(ans,top);
    		}
    		if(i == r[a[i]]){
    			if(st[top-1] != a[i]) {
    				W(-1);
    				return ;
    			}
    			top--;
    		}
    		if (i!=l[a[i]] && i!=r[a[i]]) {
    			if(st[top-1] != a[i]){
    				W(-1);
    				return ;
    			}
    		}
    	}
    	W(ans);
    }
    
    void clear(){
    	
    }
    
    void run() { read(); init(); compute(); clear(); }
    
    bool mleed;
    
    void wa() { cout << '\n' << tleed-tlest << "ms\n" << (&mleed-&mlest-1)/1024.0/1024.0 << "MB\n"; }
    
    void fre(string s){
    	freopen((s+".in").c_str(),"r",stdin);
    	freopen((s+".out").c_str(),"w",stdout);
    }
    
    int main(){
    //	fre("");
    	tlest = clock();
    	run();
    	tleed = clock();
    //	wa();
    	return 0;
    }
    
    
    • 1

    信息

    ID
    116
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    (无)
    递交数
    38
    已通过
    11
    上传者