Div 2 - 1082 - C2

query

this is my code without using dp or stack

im doing these by brute force - it is failing on some edge case can anyone tell me what im doing wrong

void solve()    
{
    int n ;
    cin >> n ;
 
    vector<int> v(n) ;
    for(int &i : v) cin >> i ;
 
    int k = 0 ;
    int cnt = n ;
    for(int i = 1 ; i < n ; i++){
 
        int x = n - i ; 
        if(v[i-1] >= v[i] - 1 && v[i] > v[k]){
            // it means these can be obtained 
            // now find the closest position of v[i] - 1 and count no .of elements present between them 
            // how how can u find v[i] - 1 in these inc order
 
            int ind = upper_bound(v.begin() + k , v.begin() + i , v[i] - 1) - v.begin() - 1 ;
            cnt += (i - ind) * x ;
 
        }
        else{
            cnt += (i + 1) * x ;
            k = i ;
        }
    }
    cout << cnt << '\n' ;
}
 

Posted Feb 25, 2026, 5:31 AM UTC

21
Loading comments…