Ranking 解答例(C++)
Mojacoderさんのサイトで作った問題 “Ranking” の解答例です:
問題 -> https://mojacoder.app/users/matcharate12/problems/ranking
(ネタバレ防止で↓になってます)
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define all(A) (A).begin(),(A).end()
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
int main(void){
int n; cin >> n;
vector<P> A(n);
vector<int> ans(n);
rep(i,n){
cin >> A[i].first;
A[i].second = i;
}
sort(all(A),greater<P>());
map<int,int> M;
int rank = 0;
rep(i,n){
if(M.count(A[i].first)) ans[A[i].second] = M[A[i].first];
else {
ans[A[i].second] = rank;
M[A[i].first] = rank;
rank++;
}
}
rep(i,n) cout << ans[i]+1 << " ";
return 0;
}
~余談~
どこかのゲーム実況動画を見て思いついたものです。ゲームの順位システムはソートでもいいですが、そのまま表示できるのもいいですよね。私は粋だと思います!
(でも順位が被るとその処理が面d)