๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 3077_์ž„์ง„์™œ๋ž€

๋ฟŒ์•ผ._. 2023. 10. 13. 12:15

Silver III

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/3077)

< ์ž„์ง„์™œ๋ž€ >

 

๋ฌธ์ œ ํ’€์ด 

 

HashMap <String, Integer>๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‘ ๊ฐœ์˜ ํ•ด์ „์„ ๊ณจ๋ž์„ ๋•Œ ์ˆซ์ž๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ˆœ์„œ๋ฅผ ํŒ๋‹จํ•œ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

public class _3077_ { // ์ž„์ง„์™œ๋ž€

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		int n = Integer.parseInt(bf.readLine());

		st = new StringTokenizer(bf.readLine());

		HashMap<String, Integer> map = new HashMap<>();
		for (int i = 0; i < n; i++) {
			map.put(st.nextToken(), i);
		}

		int b = n * (n - 1) / 2;
		int a = 0;

		String arr[] = new String[n];
		st = new StringTokenizer(bf.readLine());
		for (int i = 0; i < n; i++) {
			arr[i] = st.nextToken();
		}

		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if (map.get(arr[i]) < map.get(arr[j]))
					a += 1;
			}
		}

		System.out.println(a + "/" + b);
	}
}

 

Main

๋ณ€์ˆ˜)
n : ํ•ด์ „์˜ ๊ฐœ์ˆ˜
map : HashMap <ํ•ด์ „ ์ด๋ฆ„, ์ˆœ์„œ>
b : ๋งŒ์ 
a : ํš๋“ ์ ์ˆ˜
arr : ๋‹ต์•ˆ

 

- ํ•ด์ „์˜ ๊ฐœ์ˆ˜(n) ์ž…๋ ฅ

- ์ •๋‹ต์„ ์ž…๋ ฅ๋ฐ›์•„ HashMap์— ์ €์žฅ

- ์ž‘์„ฑํ•œ ๋‹ต์•ˆ์„ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅ

- arr์˜ ๋ชจ๋“  ์Œ์„ ์ˆœํ™˜

: HashMap์—์„œ value ๊ฐ’์„ ๊ฐ€์ ธ์™€ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ƒ๋Œ€์ ์ธ ๊ด€๊ณ„ ํŒ๋‹จ

- ์ •๋‹ต(a/b) ์ถœ๋ ฅ