Official

B - Representative Balls Editorial by en_translator


For each color, prepare a variable that manages the maximum size.

Inspect the balls in order. If that ball’s size exceeds the maximum size of that color so far, update that managed maximum value.

If we set the initial value of the maximum size for each color to \(-1\), we can skip the casework in the implementation for the case where the answer is \(-1\).

Sample code

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

int main() {
	int n, m;
	cin >> n >> m;
	vector<int> a(m, -1);
	for (int i = 0; i < n; i++) {
		int c, s;
		cin >> c >> s;
		c--;
		if (a[c] < s) a[c] = s;
	}
	for (int i = 0; i < m; i++) cout << a[i] << " \n"[i == m - 1];
}

posted:
last update: