Problem with USACO Bronze problem

Question
My solution
My Algorithm Flow:

  1. Store the segments in the road and the speed limit in an array, and also the distance segment covered by Bessie and Bessie speed limit.
  2. Then I created a vector to store the speed of Bessie every mile and the speed limit every mile.
  3. Then I used a for loop to check at what instances Bessie has crossed the speed limit

The output for the 1st test case should be 5 but mine outputs 4 :frowning:

I tried substituting the values but everything seems correct.

edit: pls help

1 Like

I think you may have read the question wrong. In the question, it asks what is the most that Bessie is over the speed limit. You have calculated how many times Bessie is over the speed limit. Also, you have 2 nested for loops in your solution. You have to change the control variable in the nested one, otherwise, the i's in the outer for loop will keep changing to the wrong values.

okay…I will try reading the question again :slight_smile:

1 Like

Can you explain me in more detail by what you mean in the text in quote?

A nested for loop is a for loop inside of another for loop. If you didn’t know, you can change the value of the control variable of a for loop inside of the for loop. Since the outer for loop has a control variable i and since you are changing that variable inside of the inner for loop, the value of i will change. And in your case, i is increasing in the inner for loop. Change that variable name to j(as tradition often dictates) or any variable name you like.

Yes I did the necessary change and this is what I get:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
  ifstream fin("speeding.in");
  ofstream fout("speeding.out");
  int N , M;
  int road_length[N];
  int speed_limit[N];
  int bessie_length[M];
  int bessie_speed[M];
  fin >> N >> M;
  for(int i = 0 ; i < N ; i++)
  {
    fin >> road_length[i] >> speed_limit[i];
  }
  for(int i = 0 ; i < M ; i++)
  {
    fin >> bessie_length[i] >> bessie_speed[i];
  }
  vector<int> speed_limit_rate;
  vector<int> bessie_speed_rate;
  for(int i = 0 ; i < N ; i++)
  {
    for(int j = 0 ; j < road_length[i] ; j++)
    {
      speed_limit_rate.push_back(speed_limit[i]);
    }
  }
  for(int i = 0 ; i < M ; i++)
  {
    for(int j = 0 ; j < bessie_length[i] ; j++)
    {
      bessie_speed_rate.push_back(bessie_speed[i]);
    }
  }
  vector<int> vec;
  int difference = 0;
  for(int i = 0 ; i < 100 ; i++)
  {
    difference = bessie_speed_rate[i] - speed_limit_rate[i];
    vec.push_back(difference);
  }
  sort(vec.begin() , vec.end());
  int n = vec.size();
  fout << vec[n];
}

After running this, I get a segmentation fault error.
How do I fix It? :frowning:

A segmentation fault is when you try to access some memory that is not allocated for your code. This usually happens when you try to access an index that is outside of an array or vector.
Edit,
This is not how you did it.

Edit,
Another way to get a segmentation fault is when a statically sized array does not know how much memory to allocate. This is how you did it. Look at the code above.

so segmentation error comes when I try to access arr[11] where the maximum index of the array is only 10?

Basically, but there are other ways. The index that is outside of your array is how you did it.
Edit,
I am wrong. You did it another way. I have to change my answer. I am so sorry.

You actually did it another way. Another way you can get a segmentation fault is when a statically sized array does not know how much memory to allocate.

1 Like

Thanks! I solved it :slight_smile:

1 Like

Nice job!!