kornfire's blog

By kornfire, 10 years ago, In English

Problem statement is here[Link]

I have below solution to the problem.

#include <bits/stdc++.h>

using namespace std;

const int MAXLEN = 100001;

int main()
{
int n;
cin >> n;
int x;
cin >> x;

deque<pair<int, int> > input;
int st, end;
for(int i = 0; i < n; i++)
{
    cin >> st;
    cin >>end;
    input.push_back(make_pair(st, end));
}

int trav = 1;
int sum = 0;
while(!input.empty())
{
    int nxt = input.front().first;
    while(trav <= nxt)
    {
        trav += x;
    }
    trav -= x ;
    int last = input.front().second ;

    sum += last - trav + 1;
    trav = last ;
    input.pop_front();
}

cout << sum <<endl;

return 0;

}

For very first test case, my solution prints 7 while accepted answer is 6. With several attempts i am not able to think what i am doing wrong. Any help in this will be greatly appreciated.

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

This line:

    trav = last ;

is wrong.