An architect wants to design a very high building. The building will consist of some floors, and each floor has a certain size. The size of a floor must be greater than the size of the floor immediately above it. In addition, the designer (who is a fan of a famous Spanish football team) wants to paint the building in blue and red, each floor a color, and in such a way that the colors of two consecutive floors are different.
To design the building the architect has n available floors, with their associated sizes and colours. All the available floors are of different sizes. The architect wants to design the highest possible building with these restrictions, using the available floors.
Hints
function.c
#include <stdlib.h>
#include "function.h"
/* implement compare function
* if ( *(MyType*)a < *(MyType*)b ) return -1;
* if ( *(MyType*)a == *(MyType*)b ) return 0;
* if ( *(MyType*)a > *(MyType*)b ) return 1;
*/
int compare(const void *a, const void *b) {
}
function.h
#ifndef FUNCTION_H
#define FUNCTION_H
typedef struct {
char color;
unsigned int size;
} Floor;
int compare(const void *a, const void *b);
#endif
main.c
#include <stdio.h>
#include <stdlib.h>
#include "function.h"
#define MAX_FLOOR_NUM 20000
#define RED 0
#define BLUE 1
int design(int floorNum, Floor floorArr[]) {
int height, color;
int idx;
qsort(floorArr, floorNum, sizeof(Floor), compare);
height = 0;
color = (floorArr[0].color == 'B') ? BLUE : RED;
for (idx = 0; idx < floorNum; idx++) {
if (color == BLUE && floorArr[idx].color == 'B') {
height++;
color = RED;
}
else if (color == RED && floorArr[idx].color == 'R') {
height++;
color = BLUE;
}
}
return height;
}
int main() {
int floorNum;
int i;
Floor floorArr[MAX_FLOOR_NUM];
scanf("%d", &floorNum);
for (i = 0; i < floorNum; i++) {
scanf(" %c %d", &floorArr[i].color, &floorArr[i].size);
}
printf("%d", design(floorNum, floorArr));
return 0;
}
The first line contains the number of available floors. Then, the size and color of each floor appear in one line. Each floor is represented with a character and an integer between 0 and 999999. There is no floor with size 0. Character 'B' represents the blue floor and character 'R' represents the red floor. The integer represents the size of the floor. There are not two floors with the same size. The maximum number of floors for a problem is 20000
The output will consist of a line with the number of floors of the highest building with the mentioned conditions.