TLE issue
Difference between en2 and en3, changed 889 character(s)
can anybody help me, why I am getting TLE in TEST CASE 9 ,even though time complexity of my code is nlog(n).↵
question C of  Educational Codeforces Round 170.↵
CODE↵
int main(){↵
    int t;↵
    cin>>t;↵
    while(t--){↵
       ll n,k;cin>>n>>k;↵
       unordered_map<ll,ll>mp;↵
       set<ll>p;↵
       for(int i=0;i<n;i++){↵
        ll x;cin>>x; p.insert(x);↵
        mp[x]++;↵
       }↵
       vector<ll>s;↵
       for(auto i:p){↵
        s.pb(i);↵
       }↵
      //  cout<<s.size()<<endl;↵
       ul ans=mp[s[0]],count=mp[s[0]],crr=1,l=0;↵
       for(int i=1;i<s.size();i++){↵
        if(s[i]-s[i-1]==1 && crr<k){↵
         count+=mp[s[i]];↵
         crr++;↵
        }↵
        else if(s[i]-s[i-1]!=1){↵
            ans=max(ans,count);↵
            count=mp[s[i]];crr=1;↵
            l=i;↵
        }↵
        else{↵
            ans=max(ans,count);↵
            count-=mp[s[l]];↵
            count+=mp[s[i]];↵
            l++;↵
        }↵
       }↵
       ans=max(ans,count);↵
       cout<<ans<<endl;↵

       ↵
       ↵
    }↵
    return 0;↵
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English _manish_yadav_16 2024-10-15 18:11:59 889
en2 English _manish_yadav_16 2024-10-15 18:10:57 892
en1 English _manish_yadav_16 2024-10-15 18:09:35 170 Initial revision (published)