728x90
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

2์ฐจ์› ํ‰๋ฉด ์œ„์˜ ์  N๊ฐœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ขŒํ‘œ๋ฅผ y์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์œผ๋กœ, y์ขŒํ‘œ๊ฐ€ ๊ฐ™์œผ๋ฉด x์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ •๋ ฌํ•œ ๋‹ค์Œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” i๋ฒˆ์ ์˜ ์œ„์น˜ xi์™€ yi๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (-100,000 ≤ xi, yi ≤ 100,000) ์ขŒํ‘œ๋Š” ํ•ญ์ƒ ์ •์ˆ˜์ด๊ณ , ์œ„์น˜๊ฐ€ ๊ฐ™์€ ๋‘ ์ ์€ ์—†๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ ์„ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ‹€๋ฆฐ ๋‚ด ํ’€ ๐Ÿฆท

# 11651. ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ2

N = int(input())
result = []
for _ in range(N):
    x, y = map(int, input().split())
    result.append([x, y])

# for k in result:
#     print(k[-1])

for i in range(N-1):
    if result[i][-1] > result[i+1][-1]:
        result[i], result[i+1] = result[i+1], result[i]
        # print(result)
for i in range(N-1):
    if result[i][-1] > result[i+1][-1]:
        result[i], result[i+1] = result[i+1], result[i]

for s in result:
    print(*s)

์ฒ˜์Œ์—๋Š” ์•ž์—๊ฒƒ๋ถ€ํ„ฐ ๋น„๊ตํ•˜๋ฉด์„œ ์•ž์—๊ฐ’์ด ๋’ค์—๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ๋ฐ”๊ฟ”์ฃผ๋Š” ๋Š๋‚Œ์œผ๋กœ ๋กœ์ง์„ ๊ตฌ์„ฑํ–ˆ๋Š”๋ฐ... for๋ฌธ์„ ๋‘๋ฒˆ ๋Œ๋ฆฌ๋‹ˆ๊นŒ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๋‹ต์€ ๋‚˜์™”๋Š”๋ฐ ์•„๋งˆ ํžˆ๋“ ์ผ€์ด์Šค๋“ค์€ ๋‹ค ๊ฑธ๋ ค์„œ ํŽ˜์ผ์„ ๋ฐ›์•˜๋‹ค...

์•„๋งˆ ๋‹ค๋ฅธ ์• ๋“ค์€ ๋‘๋ฒˆ์œผ๋กœ ์•ˆ๋˜๊ฒ ์ง€..

๊ทธ๋ž˜์„œ ์–ด๋–ค ๋ฐฉ๋ฒ•์œผ๋กœ ํ•˜๋ฉด ์ข‹์„๊นŒ ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ณด๋‹ค๊ฐ€...

y๊ฐ’์„ ์ฐจ๋ก€๋กœ ์ •๋ ฌํ•˜๋ฉด ๋˜๋‹ˆ๊นŒ ๋จผ์ € ๋ฐ›์•„์„œ ์†ŒํŠธ??

 

๋‚ด ํ’€ ๐Ÿฆท

# 11651. ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ2
import sys
input = sys.stdin.readline
N = int(input())
result = []

# y ๊ฐ’์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ 
for _ in range(N):
    x, y = map(int, input().split())
    result.append([y, x])

result.sort()
# print(result)

for y, x in result:
    print(x, y)

๊ฒฐ๊ณผ๋Š” ์„ฑ๊ณต์ด์—ˆ๋‹ค!!!! ์ƒ๊ฐ๋ณด๋‹ค ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐ ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

๊ทธ๋Ÿฌ๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋ฅผ ๋ดค๋Š”๋ฐ ์ •๋ง ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๋žŒ๋‹ค๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜์…จ๋‹ค...

์ž˜ ๋ชฐ๋ผ์„œ ๋ชป ์“ฐ๊ธฐ๋„ ํ–ˆ์–ด์„œ ์ด๋ฒˆ๊ธฐํšŒ์— ๊ณต๋ถ€ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค ^^

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

 

- sorted()์—์„œ key lambda ์‚ฌ์šฉ (์˜ค๋ฆ„์ฐจ์ˆœ์ด ๊ธฐ๋ณธ)

 

  • ๋‹จ์ˆœํžˆ sorted๋งŒ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ
a = [(0, 4), (1, 2), (1, -1), (2, 2), (3, 3)]
b = sorted(a)  # a.sort()์™€ ๋™์ผ
print(b)

# [(0, 4), (1,  -1), (1, 2), (2, 2), (3, 3)]
# ์‚ฌ์‹ค ๊ตณ์ด ๋žŒ๋‹ค๋กœ ์–ด๋ ต๊ฒŒ ์•ˆ ์“ฐ๊ณ ... ๊ฑฐ๊พธ๋กœ ๋ฐ›์•„์„œ ์ •๋ ฌํ•˜๋Š”๊ฒŒ ๋” ์ข‹์€๋“ฏ ^^^^^^^^

 

  • key ์ธ์ž์— ํ•จ์ˆ˜๋ฅผ ๋„˜๊ฒจ์ค€ ๊ฒฝ์šฐ
  1. ์ฒซ๋ฒˆ์งธ ๋“ค์–ด์žˆ๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ค€ ๊ฒฝ์šฐ
a = [(0, 4), (1, 2), (1, -1), (2, 2), (3, 3)]
c = sorted(a, key = lambda x : x[0])
print(c)

# [(0, 4), (1, -1), (1, 2), (2, 2), (3, 3)]
# ์•ž์˜ ์ธ์ž์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜๋Š”๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

 

  2. ๋‘๋ฒˆ์งธ ๋“ค์–ด์žˆ๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ค€ ๊ฒฝ์šฐ

a = [(0, 4), (1, 2), (1, -1), (2, 2), (3, 3)]
d = sorted(a, key = lambda x : x[1])
print(d)

# [(1, -1), (1, 2), (2, 2), (3, 3), (0, 4)]
# ๋’ค์˜ ์ธ์ž์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜๋Š”๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

   

   3. ์ฒซ๋ฒˆ์งธ ๋“ค์–ด์žˆ๋Š” ๊ฐ’์€ ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‘๋ฒˆ์งธ ๋“ค์–ด์žˆ๋Š” ๊ฐ’์€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ด์ค€ ๊ฒฝ์šฐ

# ๋‚ด๋ฆผ์ฐจ์ˆœ์€ ์•ž์— -๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ๋œ๋‹ค!!
e = sorted(a, key = lambda x = (x[0], -x[1]))
print(e)

# [(0, 4), (1, 2), (1, -1), (2, 2), (3, 3)]

 


 

# 11651. ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ2
import sys
input = sys.stdin.readline
N = int(input())
result = []

# x๊ฐ’์— ์ƒ๊ด€์—†์ด y ๊ฐ’์„ ์ •๋ ฌ => y ๊ฐ’ ์ •๋ ฌ
for _ in range(N):
    x, y = map(int, input().split())
    result.append([x, y])

ans = sorted(result, key = lambda x : (x[1], x[0]))
for x, y in ans:
    print(x, y)

 

๋ฐฐ์šด ๊ฒƒ์„ ํ† ๋Œ€๋กœ ๋žŒ๋‹ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์‹œ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ์ด๋Ÿฌํ•œ ์ฝ”๋“œ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค!!

๋‹ค์Œ ์ •๋ ฌ ๋ฌธ์ œ๋Š” ๋žŒ๋‹ค๋กœ ๋„์ „ํ•ด๋ด์•ผ๊ฒ ๋‹ค ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚

 

 

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts