Showing posts with label Session #3. Show all posts
Showing posts with label Session #3. Show all posts

Problem Solution of GBus count, Coding Practice with Kick Start Session #3 - Kick Start 2022 in C Language.

 


Problem

There exist some cities that are built along a straight road. The cities are numbered 1,2,3, from left to right.

There are N GBuses that operate along this road. For each GBus, the range of cities that it serves is provided: the i-th gBus serves the cities with numbers between Ai and Bi, inclusive.

We are interested in a particular subset of P cities. For each of those cities, we need to find out how many GBuses serve that particular city.

Input

The first line of the input gives the number of test cases, T. Then, T test cases follow; each case is separated from the next by one blank line. (Notice that this is unusual for Kickstart data sets.)

In each test case:

  • The first line contains one integer N: the number of GBuses.
  • The second line contains 2N integers representing the ranges of cities that the buses serve, in the form A1 B1 A2 B2 A3 B3 ... AN BN. That is, the first GBus serves the cities numbered from A1 to B1 (inclusive), the second GBus serves the cities numbered from A2 to B2 (inclusive), and so on.
  • The third line contains one integer P: the number of cities we are interested in, as described above. (Note that this is not necessarily the same as the total number of cities in the problem, which is not given.)
  • Finally, there are P more lines; the i-th of these contains the number Ci of a city we are interested in.

Output

For each test case, output one line containing Case #xy, where x is the number of the test case (starting from 1), and y is a list of P integers, in which the i-th integer is the number of GBuses that serve city Ci.

Limits

Memory limit: 1 GB.
1T10.

Test Set 1

Time limit: 60 seconds.
1N50.
1Ai500, for all i.
1Bi500, for all i.
1Ci500, for all i.
1P50.

Test Set 2

Time limit: 120 seconds.
1N500.
1Ai5000, for all i.
1Bi5000, for all i.
1Ci5000, for all i.
1P500.

Here is the solved code for this problem in c language:

#include<stdio.h>

int main()

{

    int t,ccase=0;

    scanf("%d",&t);

    while(t--)

    {

        int a,i,j,k;

        scanf("%d",&a);

        int c = 2*a;

        int b[c];

        for(i=0;i<c;i++)

        scanf("%d",&b[i]);

            

        int p;

        scanf("%d",&p);

        int q[p];

        int count[p];

        

        for(i=0;i<p;i++)

        count[i]=0;

        

        for(i=0;i<p;i++)

        scanf("%d",&q[i]);

        

        for(j=0;j<p;j++)

        {

        for(k=0;k<c;k++)

        {

            if((q[j] >= b[k]) && (q[j] <= b[k+1])) {

            count[j]++;

            }

            k+=1;

        }

      }

      printf("Case #%d: ",++ccase);

      for(i=0;i<p;i++)

      printf("%d ",count[i]);

      printf("\n");

     }

    return 0;

}


Operator Overloading in C++

#include<iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i = 0){       re...