18870: 좌표 압축

hyeok2044 · May 15, 2022
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Main {

	public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
	public static MyScanner sc = new MyScanner();

	static int pi(String s) {
		return Integer.parseInt(s);
	}

	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

	}
	public static void main(String[] args) throws Exception {
		int n = sc.nextInt();
		int[] arr = new int[n];
		int[] sorted = new int[n];
		for(int i=0; i<n; i++){
		    arr[i] = sc.nextInt();
		    sorted[i] = arr[i];
		}
		Arrays.sort(sorted);
		HashMap<Integer,Integer> hm = new HashMap<>();
		int rank = 0;
		int prev = sorted[0];
		for(int i=0; i<n; i++){
		    if(prev!=sorted[i]){
		        rank++;
		        prev = sorted[i];
		    }
		    hm.put(sorted[i], rank);
		}
		for(int i=0; i<n; i++){
		    out.print(hm.get(arr[i])+" ");
		}
		out.flush();
	
	}

}
Output
(Run the program to view its output)

Comments

Please sign up or log in to contribute to the discussion.