Fraudulent Activity Notifications

  • + 1 comment
    you need to use insert and delete instead of sort to decrease complexity
    
    #include <bits/stdc++.h>
    #define int long long
    
    using namespace std;
    float median(vector<int> &a, int d)
    {
        float median ;
        if(d%2==0)
        {
           int m1=d/2-1;
           median = (a[m1]+a[m1+1]);
        }
        else
        {
            median= a[d/2]*2;
        }
        return median;
    }
    
    signed main()
    {
        int n, d;
        int cnt=0;
        cin>>n>>d;
        vector<int> a(d);
        vector<int> mark(n+1);
        for(int i=0;i<d;i++){
        cin>>a[i];mark[i]=a[i];}
        sort(a.begin(),a.end());
        for(int i=d;i<n;i++)
        {
            int temp;cin>>temp;
            mark[i]=temp;
            float me = median(a,d);
            if(temp>=me)cnt++;
            auto it = lower_bound(a.begin(), a.end(), mark[i-d]);
            a.erase(it);
            auto m = lower_bound(a.begin(), a.end(), temp);
            a.insert(m,temp);
    
        }
        cout<<cnt;
    }