ccf-csp 202109-2 非零段划分

ccf-csp 202109-2 非零段划分

#include 
using namespace std;
set<int> ss;
int n;
int A[500005];
struct p{
    vector<int> v;
}p[500005];
int main(){
    cin>>n;
    int num;
    for(int i=1;i<=n;i++){
        cin>>num;
        ss.insert(num);
        p[num].v.push_back(i);
    }
    A[n+1]=0;
    int max=0;
    int before=0;
    for(auto it=ss.rbegin();it!=ss.rend();it++){
        for(int i=0;i<p[*it].v.size();i++){ 
            if(*it==0) break;
            A[p[*it].v[i]]=*it;
            if(A[p[*it].v[i]-1]==0&&A[p[*it].v[i]+1]==0){
                before+=1;
            }
            else if(A[p[*it].v[i]-1]>0&&A[p[*it].v[i]+1]>0){
                before-=1;
            }
        }
        if(before>max)
        max=before;
    }
    cout<<max<<endl;
    system("pause");
    return 0;
}

你可能感兴趣的:(c++)