COUPSYS - Editorial

PROBLEM LINK:

Practice
Contest

Author: Praveen Dhinwa
Testers: Misha Chorniy
Editorialist: Praveen Dhinwa

DIFFICULTY:

cakewalk

PREREQUISITES:

none

PROBLEM:

An exam can be of three levels, level 1, 2 and 3. There are n exams being conducted, each exam will be conducted in a particular city, for a particular level and provide a fixed discount.

For each level, you have to provide maximum discount you can obtain and find the city in which this discount is applicable. If there are more than one cities for a discount, choose the city with less index.

SOLUTION

The solution is to iterate over all the exams and maintain for each level, what is the maximum discount you can get, and update it for each exam.

int n;
scanf("%d", &n);
vector<int> which_city(3, (int) 1e9), maxDiscounts(3);
for (int i = 0; i < n; i++) {
	int city, level, discount;
	scanf("%d %d %d", &city, &level, &discount);
	level--;
	if (discount > maxDiscounts[level]) {
		maxDiscounts[level] = discount;
		which_city[level] = city;
	} else if (discount == maxDiscounts[level]) {
		maxDiscounts[level] = discount;
		which_city[level] = min(which_city[level], city);
	}
}
for (int level = 0; level < 3; level++) {
	printf("%d %d\n", maxDiscounts[level], which_city[level]);
}

Setters and Tester Solution Codes

Setter’s solution
Tester’s solution