Pankaj8433's blog

By Pankaj8433, history, 7 months ago, In English

I AM GETTING RUNTIME ERROR FOR INPUT 1 3 3 2 1 BUT NOT FOR 2 3 1 2 3 3 3 2 1 WHY??? PROBLEM NAME-Grouping Increases CONTEST NAME-Hello 2024

include <bits/stdc++.h>

using namespace std; int t;vectora;vectorv;

define ll long long

int main(){ cin>>t; while(t--){ ll sum=0; int n,b; cin>>n;

for(int i=0;i<n;i++){
        int x;
        cin>>x;
        if(v.empty()&&a.empty()) v.push_back(x);
        else if(a.empty()&&v.back()<x) a.push_back(x);
        else if(a.back()>=x&&v.back()>=x){
            if(a.back()>v.back()) v.push_back(x);
            else a.push_back(x);
        }
        else if(v.back()>=x)v.push_back(x);
        else if(a.back()<x && v.back()<x){
            sum++;
            if(a.back()>v.back()) v.push_back(x);
            else a.push_back(x);
        }
        else a.push_back(x);
    }
    cout<<sum<<endl;
    // for(auto x:v) cout<<x<<" ";cout<<endl;
    a.clear();
    v.clear();
}

}

  • Vote: I like it
  • -16
  • Vote: I do not like it

»
7 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it
#include <bits/stdc++.h>
using namespace std;
int t;vector<int>a;vector<int>v;
#define ll long long
vector<vector<ll>>dop;

int main(){
    cin>>t;
    while(t--){
        ll sum=0;
        int n,b;
        cin>>n;
        
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            if(v.empty()&&a.empty()) v.push_back(x);
            else if(a.empty()&&v.back()<x) a.push_back(x);

//What if v.back()>=x and a is empty? You didnt seem to have checked that case.
            else if(a.back()>=x&&v.back()>=x){
                if(a.back()>v.back()) v.push_back(x);
                else a.push_back(x);
            }
            else if(v.back()>=x)v.push_back(x);
            else if(a.back()<x && v.back()<x){
                sum++;
                if(a.back()>v.back()) v.push_back(x);
                else a.push_back(x);
            }
            else a.push_back(x);
        }
        cout<<sum<<endl;
        // for(auto x:v) cout<<x<<" ";cout<<endl;
        a.clear();
        v.clear();
    }
}

  • »
    »
    7 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    But than why in second test case (2 3 1 2 3 3 3 2 1) it is giving correct answer.