0

i am getting 0xc0000005 error(access violation error), where am i wrong in this code? i couldnt debug this error. please help me.

question is this

Formally, given a wall of infinite height, initially unpainted. There occur N operations, and in ith operation, the wall is painted upto height Hi with color Ci. Suppose in jth operation (j>i) wall is painted upto height Hj with color Cj such that Hj >= Hi, the Cith color on the wall is hidden. At the end of N operations, you have to find the number of distinct colors(>=1) visible on the wall.

#include<iostream>
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;

int main()
{
   int t;
   cin>>t;
   for(int tt= 0;tt<t;tt++)
   {
       int h,c;
       int temp = 0;
       cin>>h>>c;
       int A[h], B[c];
       vector<int> fc;
       for(int i = 0;i<h;i++)
       {
          cin>>A[i];
       }
       for(int j =0;j<h;j++)
       {
           cin>>B[j];
       }
       if(is_sorted(A,A+h))
       {
           return 1;
       }
       if(count(A,A+h,B[0]) == h)
       {
           return 1;
       }


       for(int i = 0;i<h;i++)
       {

          if(A[i]>=temp)
          {
              temp = A[i];

          }
          else
          {
              if(temp == fc[fc.size()-1])
              {
                  fc[fc.size()-1] = B[i];
              }
              else
              {
                  fc.push_back(B[i]);
              }
           }
         }


     }
  }

1 Answers1

0

There are several issues.

When reading values into B, your loop check is j<h. How many elements are in B?

You later look at fc[fc.size()-1]. This is Undefined Behavior if fc is empty, and is the likely source of your problem.

Other issues:

  1. Don't use #include <bits/stdc++.h>
  2. Avoid using namespace std;
  3. Variable declarations like int A[h], where h is a variable, are not standard C++. Some compilers support them as an extension.
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56