본문 바로가기

Language Proficiency/C++

Attending Workshops

A student signed up for  workshops and wants to attend the maximum number of workshops where no two workshops overlap. You must do the following:

Implement  structures:

  1. struct Workshop having the following members:

    • The workshop's start time.
    • The workshop's duration.
    • The workshop's end time.
  2. struct Available_Workshops having the following members:

    • An integer,  (the number of workshops the student signed up for).
    • An array of type Workshop array having size .

Implement  functions:

  1. Available_Workshops* initialize (int start_time[], int duration[], int n) 
    Creates an Available_Workshops object and initializes its elements using the elements in the  and parameters (both are of size ). Here,  and  are the respective start time and duration for the workshop. This function must return a pointer to an Available_Workshops object.

  2. int CalculateMaxWorkshops(Available_Workshops* ptr) 
    Returns the maximum number of workshops the student can attend—without overlap. The next workshop cannot be attended until the previous workshop ends.

Note: An array of unknown size () should be declared as follows:

DataType* arrayName = new DataType[n];

Input Format

Input from stdin is handled by the locked code in the editor; you simply need to write your functions to meet the specifications of the problem statement above.

Constraints

Output Format

Output to stdout is handled for you.

Your initialize function must return a pointer to an Available_Workshops object. 
Your CalculateMaxWorkshops function must return maximum number of non-overlapping workshops the student can attend.

Sample Input

6
1 3 0 5 5 8
1 1 6 2 4 1

Sample Output

CalculateMaxWorkshops should return .

Explanation

The first line denotes , the number of workshops. 
The next line contains  space-separated integers where the  integer is the  workshop's start time. 
The next line contains  space-separated integers where the  integer is the  workshop's duration.

The student can attend the workshops  and  without overlap, so CalculateMaxWorkshops returns  to main (which then prints  to stdout).




정렬(std::sort)을 이용할 때, 단순 자료형(int)가 아닌 구조체의 변수값을 가지고 정렬하는 법은 이미 다른 글에서 작성했었다.

여기서도 그 부분이 그대로 사용되었다. (EndTime 순서로 정렬)




'Language Proficiency > C++' 카테고리의 다른 글

Vector-Sort  (0) 2018.08.01
Attribute Parser  (0) 2018.08.01
Overload Operators  (0) 2018.07.31
Operator Overloading  (0) 2018.07.31
Deque-STL  (0) 2018.07.31