B - Ranking with Ties
Source: atcoder
Difficulty: 200

Problem Statement
N
people labeled from
1
to
N
participated in a certain contest. The
score
of person
i
(
1 \leq i \leq N
) was
P_i
.
In this contest, the
rank
of each of the
N
people is determined by the following procedure:
Prepare a variable
r
, and initialize
r = 1
. Initially, the ranks of the
N
people are all undetermined.
Repeat the following operation until the ranks of all
N
people are determined:
Let
x
be the maximum score among the people whose ranks are currently undetermined, and let
k
be the number of people whose score is
x
. Determine the rank of those
k
people with score
x
to be
r
, and then add
k
to
r
.
Print the rank of each of the
N
people.
Constraints
1\leq N \leq 100
1\leq P_i \leq 100
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
P_1
P_2
\dots
P_N
Output
Print
N
lines. The
i
-th line (
1 \leq i \leq N
) should contain the rank of person
i
as an integer.
Sample Input 1
4
3 12 9 9
Sample Output 1
4
1
2
2
The ranks of the
N\ (=4)
people are determined as follows:
Prepare a variable
r
and initialize
r=1
. At first, the ranks of all
4
people are undetermined.
Currently, persons
1, 2, 3, 4
have undetermined ranks. The maximum score among them is
P_2\ (=12)
. Therefore, determine the rank of person
2
to be
r\ (=1)
, and then add
1
to
r
, making
r=2
.
Currently, persons
1, 3, 4
have undetermined ranks. The maximum score among them is
P_3=P_4\ (=9)
. Therefore, determine the ranks of persons
3
and
4
to be
r\ (=2)
, and then add
2
to
r
, making
r=4
.
Currently, person
1
has an undetermined rank. The maximum score among them is
P_1\ (=3)
. Therefore, determine the rank of person
1
to be
r\ (=4)
, and then add
1
to
r
, making
r=5
.
The ranks of all
4
people are now determined, so the process ends.
Sample Input 2
3
3 9 6
Sample Output 2
3
1
2
Sample Input 3
4
100 100 100 100
Sample Output 3
1
1
1
1
Sample Input 4
8
87 87 87 88 41 38 41 38
Sample Output 4
2
2
2
1
5
7
5
7

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

#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define sz(x) ((int)x.size())
#define trav(a, x) for (auto& a : x)
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair

typedef long long ll;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;

ll safeMult(ll a, ll b) {
    if (a == 0 || b == 0) return 0;
    if (a > INF / b) return INF;
    return a * b;
}

ll Pow(ll base, ll exp) {
    ll result = 1;
    while (exp > 0) {
        if (exp % 2 == 1) {
            result = safeMult(result, base);
            if (result == INF) return INF;
        }
        base = safeMult(base, base);
        if (base == INF) return INF;
        exp /= 2;
    }
    return result;
}

bool isPerfectSquare(ll n) {
    ll root = sqrt(n);
    return root * root == n;
}

bool isPowerOfTwo(ll n) {
    return n > 0 && (n & (n - 1)) == 0;
}

ll setBitsUpToHighestBit(ll x) {
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    return x;
}

bool isPrime(ll n) {
    if (n < 2) return false;
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

vector<ll> generatePrimes(ll n) {
    vector<bool> isPrime(n + 1, true);
    vector<ll> primes;
    isPrime[0] = isPrime[1] = false;

    for (int p = 2; p <= n; ++p) {
        if (isPrime[p]) {
            primes.push_back(p);
            for (int multiple = 2 * p; multiple <= n; multiple += p) {
                isPrime[multiple] = false;
            }
        }
    }
    return primes;
}

ll xorRange(ll l, ll r) {
    // XOR from 0 to a given number x
    auto xorUpto = [](ll x) -> ll {
        ll rem = x % 4;
        if (rem == 0) {
            return x;
        }
        else if (rem == 1) {
            return 1;
        }
        else if (rem == 2) {
            return x + 1;
        }
        else {
            return 0;
        }
    };
    return xorUpto(r) ^ xorUpto(l - 1);
}



void solve() {
    
    
    ll n;
    cin >> n;
    
    vector<ll> v(n);
    for (ll i = 0; i < n; i++) {
        cin >> v[i];
        
    }
    
    vector<ll> rev = v;
    sort(all(rev),greater<ll>());
    
    map<ll,ll> rank;
    ll r = 1;
    
    for (ll i = 0; i < n;) {
        ll val = rev[i];
        ll freq = 0;
        
        while (i + freq < n && rev[i+freq] == val) {
            freq++;
        }
        rank[val] = r;
        r += freq;
        i += freq;
    }
    
    for (ll i = 0; i < n; i++) {
        cout << rank[v[i]] << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;

    while (t--) {
        solve();
    }
    return 0;
}

Go solution:
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

type Person struct {
	score int
	index int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e9+1))
	
	// Nの読み取り
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	
	// 得点の読み取り
	people := make([]Person, n)
	for i := range people {
		sc.Scan()
		score, _ := strconv.Atoi(sc.Text())
		people[i] = Person{score: score, index: i}
	}
	
	// 得点の降順でソート
	sort.Slice(people, func(i, j int) bool {
		return people[i].score > people[j].score
	})
	
	// 順位の計算
	ranks := make([]int, n)
	r := 1
	for i := 0; i < n; {
		// 同じ得点の人を数える
		j := i
		for j < n && people[j].score == people[i].score {
			ranks[people[j].index] = r
			j++
		}
		// 次の順位を計算
		r += j - i
		i = j
	}
	
	// 結果の出力
	for i := 0; i < n; i++ {
		fmt.Println(ranks[i])
	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

Java solution:
import java.util.*; import java.io.*; import java.math.*;
import java.util.concurrent.*;
import java.text.*;
public class Main{
	//見なくていいよ　ここから------------------------------------------
	static class InputIterator{
		ArrayList<String> inputLine = new ArrayList<>(1024);
		int index = 0; int max; String read;
		InputIterator(){
			try{
				BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
				while((read = br.readLine()) != null){
					inputLine.addAll(Arrays.asList(read.split(" ")));
				}
			}catch(IOException e){}
			max = inputLine.size();
		}
		boolean hasNext(){return (index < max);}
		String next(){
			if(hasNext()){
				return inputLine.get(index++);
			}else{
				throw new IndexOutOfBoundsException("There is no more input");
			}
		}
	}
	static HashMap<Integer, String> CONVSTR = new HashMap<>();
	static InputIterator ii = new InputIterator();//This class cannot be used in reactive problem.
	static PrintWriter out = new PrintWriter(System.out);
	static void flush(){out.flush();}
	static void myout(Object t){out.println(t);}
	static void myerr(Object... t){System.err.print("debug:");System.err.println(Arrays.deepToString(t));}
	static String next(){return ii.next();}
	static boolean hasNext(){return ii.hasNext();}
	static int nextInt(){return Integer.parseInt(next());}
	static long nextLong(){return Long.parseLong(next());}
	static double nextDouble(){return Double.parseDouble(next());}
	static ArrayList<String> nextCharArray(){return myconv(next(), 0);}
	static ArrayList<String> nextStrArray(int size){
		ArrayList<String> ret = new ArrayList<>(size);
		for(int i = 0; i < size; i++){
			ret.add(next());
		}
		return ret;
	}
	static ArrayList<Integer> nextIntArray(int size){
		ArrayList<Integer> ret = new ArrayList<>(size);
		for(int i = 0; i < size; i++){
			ret.add(Integer.parseInt(next()));
		}
		return ret;
	}
	static ArrayList<Long> nextLongArray(int size){
		ArrayList<Long> ret = new ArrayList<>(size);
		for(int i = 0; i < size; i++){
			ret.add(Long.parseLong(next()));
		}
		return ret;
	}
	@SuppressWarnings("unchecked")
	static String myconv(Object list, int no){//only join
		StringBuilder sb = new StringBuilder("");
		String joinString = CONVSTR.get(no);
		if(list instanceof String[]){
			return String.join(joinString, (String[])list);
		}else if(list instanceof long[]){
			long[] tmp = (long[])list;
			if(tmp.length == 0){
				return "";
			}
			sb.append(String.valueOf(tmp[0]));
			for(int i = 1; i < tmp.length; i++){
				sb.append(joinString).append(String.valueOf(tmp[i]));
			}
			return sb.toString();
		}else if(list instanceof int[]){
			int[] tmp = (int[])list;
			if(tmp.length == 0){
				return "";
			}
			sb.append(String.valueOf(tmp[0]));
			for(int i = 1; i < tmp.length; i++){
				sb.append(joinString).append(String.valueOf(tmp[i]));
			}
			return sb.toString();
		}else if(list instanceof ArrayList){
			ArrayList tmp = (ArrayList)list;
			if(tmp.size() == 0){
				return "";
			}
			sb.append(tmp.get(0));
			for(int i = 1; i < tmp.size(); i++){
				sb.append(joinString).append(tmp.get(i));
			}
			return sb.toString();
		}else{
			throw new ClassCastException("Don't join");
		}
	}
	static ArrayList<String> myconv(String str, int no){//only split
		String splitString = CONVSTR.get(no);
		return new ArrayList<String>(Arrays.asList(str.split(splitString)));
	}
	static ArrayList<String> myconv(String str, String no){
		return new ArrayList<String>(Arrays.asList(str.split(no)));
	}
	public static void main(String[] args){
		CONVSTR.put(8, " "); CONVSTR.put(9, "\n"); CONVSTR.put(0, "");
		solve();flush();
	}
	//見なくていいよ　ここまで------------------------------------------
	//このコードをコンパイルするときは、「-encoding UTF-8」を指定すること
	static void solve(){//ここがメイン関数
		int N = nextInt();
		ArrayList<Integer> list = nextIntArray(N);
		int[] output = new int[N];
		int r = 1;
		while(true){
			boolean ok = true;
			int max = -1;
			for(int i = 0; i < N; i++){
				max = Math.max(max, list.get(i));
				if(output[i] == 0){
					ok = false;
				}
			}
			if(ok){
				break;
			}
			int maxcount = 0;
			for(int i = 0; i < N; i++){
				if(list.get(i) == max){
					maxcount++;
					output[i] = r;
					list.set(i, -1);
				}
			}
			r += maxcount;
		}
		myout(myconv(output, 9));
	}
	//メソッド追加エリア　ここから



	//メソッド追加エリア　ここまで
}

JavaScript solution:
let [n,np]=require("fs").readFileSync("/dev/stdin", "utf8").split("\n")
np = np.split(" ").map(e=>{return Number(e)})
let r = 1
let rankp = []
for (let i = 0; i < Number(n); i++) {
    rankp.push([i,np[i]])
}
const sorted = np.sort((a,b)=>b-a)
for (const elem of rankp.sort((a,b)=>a[0]-b[0])) {
    console.log(sorted.indexOf(elem[1])+1)
}

Python solution:
N = int(input())
P = list(map(int, input().split(" ")))

ans = [0] * N

for i in range(N) :
  count = 0
  for j in range(N) :
    if P[i] < P[j] :
      count += 1
      
  ans[i] = count + 1

for i in range(N) :
  print(ans[i])

Ruby solution:
n = gets.to_i
points = gets.split.map(&:to_i)
r = 1
rankings = Array.new(n)

while rankings.include?(nil) do
  max = points.max
  k = 0
  points.each.with_index do |point, i|
    next unless point == max
    rankings[i] = r
    points[i] = 0
    k += 1
  end
  r += k
end

puts rankings