← Real problems

mccc4j4

easysortingimplementationStatement on DMOJ

CP Lang

21 lines0.3 spec
// mccc4j4: teams play round robins in groups of m; a win is 3 points, a tie 1. Sort each group by
// points, then id, and print each group's k-th team.
let (n, m) = read<(1..=2_000, 1..=2_000)>()
domain Team = 1..=n
enum Outcome:
    @tag("W") Win
    @tag("T") Tie
    @tag("L") Loss
var groups = read<[n / m, m]Team>()
var pts: [Team]Int = 0
for g in read<[(m - 1) * n / 2](u: Team, v: Team, res: Outcome)>():
    match g.res:
        Win => pts[g.u] += 3
        Loss => pts[g.v] += 3
        Tie =>
            pts[g.u] += 1
            pts[g.v] += 1
let k = read<1..=m>()
for g in groups.keys.0:
    groups[g].sort(by: t => (desc(pts[t]), t))
print(groups.keys.0.map(g => groups[g, k - 1]))

Accepted C++

115 lines · 0.92 s on the judge
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
	cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
	cerr << t << " | ";
	dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
	cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
		 << ": ";                                          \
	dbgh(__VA_ARGS__)
#else
#define cerr   \
	if (false) \
	cerr
#define dbg(...) 1412
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())

struct DSU {
	vector<int> p;
	
	DSU(int n): p(n, -1) {}
	
	int find(int u) {
		return p[u] < 0 ? u : (p[u] = find(p[u]));
	}
	
	bool merge(int u, int v) {
		u = find(u);
		v = find(v);
		if (u == v) {
			return false;
		}
		if (p[u] < p[v]) {
			swap(u, v);
		}
		p[v] += p[u];
		p[u] = v;
		return true;
	}
};

void solve() {
	int n, m;
	cin >> n >> m;
	vector<int> groups[n / m];
	for (int i = 0; i < n / m; i++) {
		groups[i].resize(m);
		for (auto& a : groups[i]) {
			cin >> a;
			a--;
		}
	}
	int points[n] {};
	for (int i = 0; i < (m - 1) * n / 2; i++) {
		int u, v;
		char res;
		cin >> u >> v >> res;
		u--;
		v--;
		if (res == 'W') {
			points[u] += 3;
		} else if (res == 'T') {
			points[u]++;
			points[v]++;
		} else {
			points[v] += 3;
		}
	}
	int k;
	cin >> k;
	k--;
	bool first = true;
	for (auto& group : groups) {
		sort(begin(group), end(group), [&](int a, int b) -> bool {
			if (points[a] == points[b]) {
				return a < b;
			}
			return points[a] > points[b];
		});
//		dbg(group[0], group[1]);
		if (!first) {
			cout << " ";
		}
		first = false;
		cout << group[k] + 1;
	}
	cout << endl;
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cin.exceptions(ios::failbit);
	int t = 1;
	// cin >> t;
	for (int _ = 1; _ <= t; _++) {
		dbg(_);
		// cout << "Case #" << _ << ": ";
		solve();
	}
}