We will find a way, we always have.

-interstellar

Problem Solving/๋ฐฑ์ค€

[๋ฐฑ์ค€] 7795๋ฒˆ: ๋จน์„ ๊ฒƒ์ธ๊ฐ€ ๋จนํž ๊ฒƒ์ธ๊ฐ€ - ํŒŒ์ด์ฌ

Redddy 2022. 5. 9. 13:13

๐Ÿ“• ํ’€์ด

์ •๋ ฌ ํ›„ ํˆฌํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ a์˜ ๊ฐ’ ํ•˜๋‚˜๋ฅผ b์˜ ๊ฐ’ ์ „๋ถ€ ํ•˜๋‚˜ํ•˜๋‚˜ ๋น„๊ตํ•ด๊ฐ€๋ฉด์„œ a์˜ ๊ฐ’์ด ํด๋•Œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•ด์ค€๋‹ค.

 

๐Ÿ’ป ์ฝ”๋“œ

import sys
input = sys.stdin.readline

for i in range(int(input().rstrip())):
	# a์™€ b์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์Œ
    a, b = map(int, input().rstrip().split()) 
    # a์™€ b ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์Œ(์ƒ๋ช…์ฒด A, B)
    a_num = list(map(int, input().rstrip().split())) 
    b_num = list(map(int, input().rstrip().split()))
    # ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
    a_num.sort(reverse=True)
    b_num.sort(reverse=True)
    a_idx, b_idx = 0,0
    cnt = 0
    
    while a_idx < a and b_idx < b:
    	# ๋งŒ์•ฝ a[i]๊ฐ€ b[i]๋ณด๋‹ค ํฌ๋‹ค๋ฉด
        if a_num[a_idx] > b_num[b_idx]:
        	# b[i:] ์ฆ‰ ํ˜„์žฌ๋ถ€ํ„ฐ ๋‚ด๋ฆผ์ฐจ์ˆœ์˜ ๋ ๊ธธ์ด๋ฅผ ์นด์šดํŠธ์— ๋”ํ•จ
            cnt += b-b_idx
            # a ์ƒ๋ช…์ฒด ๊ฐ’ ๋ณ€๊ฒฝ
            a_idx += 1
        # a[i]๊ฐ€ ์ž‘๋‹ค๋ฉด 
        else:
            # b๋ฅผ ํ•˜๋‚˜ ๋‚ด๋ฆฐ๋‹ค.
            b_idx += 1
    print(cnt)

 

 

 

 

๐Ÿ“Ž ๋ฌธ์ œ ๋งํฌ : ๋จน์„ ๊ฒƒ์ธ๊ฐ€ ๋จนํž ๊ฒƒ์ธ๊ฐ€

 

7795๋ฒˆ: ๋จน์„ ๊ฒƒ์ธ๊ฐ€ ๋จนํž ๊ฒƒ์ธ๊ฐ€

์‹ฌํ•ด์—๋Š” ๋‘ ์ข…๋ฅ˜์˜ ์ƒ๋ช…์ฒด A์™€ B๊ฐ€ ์กด์žฌํ•œ๋‹ค. A๋Š” B๋ฅผ ๋จน๋Š”๋‹ค. A๋Š” ์ž๊ธฐ๋ณด๋‹ค ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๋จน์ด๋งŒ ๋จน์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, A์˜ ํฌ๊ธฐ๊ฐ€ {8, 1, 7, 3, 1}์ด๊ณ , B์˜ ํฌ๊ธฐ๊ฐ€ {3, 6, 1}์ธ ๊ฒฝ์šฐ์— A๊ฐ€ B๋ฅผ ๋จน์„

www.acmicpc.net