公式

B - Round-Robin Tournament 解説 by en_translator


In order to solve this problem, accept the input as strings, find the number of wins for each player by counting o, and sorting the player numbers according to the conditions in the problem statement.

The main task here is to sort the player numbers. For example, the following approaches are possible:

  • Sort the pairs of (wins, player number) by a comparing function designed to sort them in descending order of number of wins, and then ascending order of player numbers.

  • Sort the pairs of (wins, player number \(\times -1\)) in lexicographically descending order.

  • Sort the player numbers by the wins with a stable sort algorithm, like std::stable_sort. (For more details, see also ABC308-C editorial.)

See also the sample code.

Sample code (C++, the first approach):

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

int main() {
	int n;
	cin >> n;
	vector<pair<int, int>> win(n);
	for(int i = 0; i < n; i++) {
		string s;
		cin >> s;
		win[i] = {count(s.begin(), s.end(), 'o'), i};
	}
	auto cmp = [](pair<int, int> a, pair<int, int> b) {
		if(a.first != b.first) return a.first > b.first;
		return a.second < b.second;
	};
	sort(win.begin(), win.end(), cmp);
	vector<int> ans(n);
	for(int i = 0; i < n; i++) ans[i] = win[i].second + 1;
	for(int i = 0; i < n; i++) cout << ans[i] << " \n"[i == n - 1];
}

Sample code (C++, the second approach):

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

int main() {
	int n;
	cin >> n;
	vector<pair<int, int>> win(n);
	for(int i = 0; i < n; i++) {
		string s;
		cin >> s;
		win[i] = {count(s.begin(), s.end(), 'o'), -i};
	}
	sort(win.rbegin(), win.rend());
	vector<int> ans(n);
	for(int i = 0; i < n; i++) ans[i] = -win[i].second + 1;
	for(int i = 0; i < n; i++) cout << ans[i] << " \n"[i == n - 1];
}

Sample code (C++, the third approach):

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

int main() {
	int n;
	cin >> n;
	vector<int> win(n), p(n);
	iota(p.begin(), p.end(), 0);
	for(int i = 0; i < n; i++) {
		string s;
		cin >> s;
		win[i] = count(s.begin(), s.end(), 'o');
	}
	auto cmp = [&](int i, int j) {
		return win[i] > win[j];
	};
	stable_sort(p.begin(), p.end(), cmp);
	for(int i = 0; i < n; i++) cout << p[i] + 1 << " \n"[i == n - 1];
}

投稿日時:
最終更新: