Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with a test case #4

Open
rajatb115 opened this issue Apr 2, 2020 · 0 comments
Open

issue with a test case #4

rajatb115 opened this issue Apr 2, 2020 · 0 comments

Comments

@rajatb115
Copy link

Hi, this is a brilliant solution but I was getting some difficulty while running a specific test case using your code.

Test case

5 3
10 20 30 40 50

I went through the code and came up with the new solution Please review my solution

#include
#include
#include

using namespace std;

int main(void) {
int n,d;
cin >> n >> d;

vector<int> days(n);
for (auto& e : days)
    cin >> e;

vector<size_t> freq(201);
size_t i = 0;
for (; i < d; ++i)
    ++freq[days[i]];
// till here we are doing

for (size_t j = 1; j < freq.size(); ++j)
    freq[j] += freq[j-1];

size_t cnt = 0;
double tmp;

size_t j;
while(i < n) {
    for (j = 0; j < freq.size() && freq[j] <= d/2; ++j);
    
    //cout<<"j="<<j<<" freq[j]="<<freq[j]<<"\n";
    //  freq[j] >= d/2
   if (freq[j-1] == d/2) {
        tmp = j;
        //cout<<"#temp="<<tmp<<" \n";
        if (d % 2 == 0) {
            for(int k=j-1;k>0;k--){
                if(freq[k]<d/2){
                    //cout<<"#freq[k]="<<freq[k]<<"\n";
                    tmp+=k+1;
                    break;
                }
            }
            tmp /= 2;
        }
    }

    else {
        tmp = j;
        if (d % 2 == 0) {
            tmp += freq[j-1] + 1 == freq[j] ? j-1 : j;
           tmp /= 2;
        }
    }

    tmp *= 2; 
    //cout<<"temp="<<tmp<<" ";

    cnt += days[i] >= tmp;

    for (j = days[i-d]; j < freq.size(); ++j)
        --freq[j];
    for (j = days[i]; j < freq.size(); ++j)
        ++freq[j];
    
    ++i;
}

cout << cnt << '\n';

return 0;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant