QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#573819#9313. Make MaxDINGRE 1ms4512kbC++142.2kb2024-09-18 20:03:112024-09-18 20:03:12

Judging History

你现在查看的是最新测评结果

  • [2024-09-18 20:03:12]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:4512kb
  • [2024-09-18 20:03:11]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define inf 1e9
using namespace std;
const int N=4e4+7;
int h[N],d[N],b[N],tmp[N];
int n,m;
void solve(){
    
    cin>>n;
  //  cout<<"_______"<<endl;
    stack<int>st;
    int ans=0,cnt=0;
    for(int i=1;i<=n;i++){
        cin>>h[i]; tmp[i] =h[i];
    }

	sort(tmp + 1, tmp + n + 1);                          // step 2
	int len = unique(tmp + 1, tmp + n + 1) - (tmp + 1);  // step 3
	for (int i = 1; i <= n; ++i)                              // step 4
  		h[i] = lower_bound(tmp + 1, tmp + len + 1, h[i]) - tmp;//cout<<h[i]<<" ";cout<<endl;
    
    st.push(1);
 
    for(int i=2;i<=n;i++){
        
        if(st.empty()||h[i]<=h[st.top()]){
            //d[i]=st.size();
            st.push(i);
        }
        else {
            int tem=st.top();
            while(!st.empty()&&h[i]>h[st.top()]){
               tem=st.top();
                d[i]+=d[tem]+1; 
                //cout<<d[i]<<" "<<i<<endl;
                st.pop();
              //  cout<<i<<endl;
                
            }
           // cout<<st.size()<<"??"<<endl;
           
            if(!st.empty()&&h[st.top()]==h[i]){
                cnt+=d[i];
            }
            st.push(i);
        }
       ans+=d[i];
      //cout<<d[i]<<" ";
        
    } // cout<<endl;
    while(!st.empty())st.pop();
    st.push(n);
    for(int i=n-1;i>=1;i--){
        if(st.empty()||h[i]<=h[st.top()]){
            st.push(i);
        }
        else{
            int tem=st.top();
            while(!st.empty()&&h[i]>h[st.top()]){
                tem=st.top();
                b[i]+=b[tem]+1;
                st.pop();
            }
           
             st.push(i);
            // if(h[st.top()]==h[i]){
            //     cnt+=b[i];
            // }
        }
        ans+=b[i];
        //cout<<b[i]<<" ";
    } //cout<<endl;
   // cout<<ans<<" "<<cnt<<endl;
    cout<<ans-cnt<<endl;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;cin>>t;
    while(t--){
        solve();
        memset(h,0,sizeof(h));
        memset(d,0,sizeof(d));
        memset(b,0,sizeof(b));
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4512kb

input:

4
2
1 2
2
2 2
7
1 1 1 2 2 2 2
3
1 2 3

output:

1
0
3
3

result:

ok 4 number(s): "1 0 3 3"

Test #2:

score: -100
Runtime Error

input:

2
198018
875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...

output:


result: