Skip to content

Commit

Permalink
백준 18870번 좌표 압축
Browse files Browse the repository at this point in the history
  • Loading branch information
skysign committed Aug 17, 2024
1 parent 50a4bef commit ad9b4c0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 백준 18870번 좌표 압축/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import copy
import sys
from typing import List


def solve():
N = int(sys.stdin.readline().strip())
dt: List[int] = list(map(int, sys.stdin.readline().strip().split(' ')))
dp: List[int] = [0 for _ in range(len(dt))]
answer: List[int] = [0 for _ in range(len(dt))]
dt_sort = copy.deepcopy(dt)
dt_sort.sort()
dict = {}

for i in range(len(dt)):
if i == 0:
dp[i] = 0
dict[dt_sort[i]] = dp[i]
else:
if dt_sort[i - 1] < dt_sort[i]:
dp[i] = dp[i-1] + 1
else: # dt_sort[i - 1] == dt_sort[i]:
dp[i] = dp[i-1]

dict[dt_sort[i]] = dp[i]

for i in range(len(dt)):
answer[i] = dict[dt[i]]

print(' '.join(list(map(str, answer))))

if __name__ == '__main__':
solve()
2 changes: 2 additions & 0 deletions 백준 18870번 좌표 압축/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
5
2 4 -10 4 -9
1 change: 1 addition & 0 deletions 백준 18870번 좌표 압축/test1_answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2 3 0 3 1
2 changes: 2 additions & 0 deletions 백준 18870번 좌표 압축/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
6
1000 999 1000 999 1000 999
1 change: 1 addition & 0 deletions 백준 18870번 좌표 압축/test2_answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 0 1 0 1 0
26 changes: 26 additions & 0 deletions 백준 18870번 좌표 압축/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys
from pathlib import Path
from unittest import TestCase
from main import solve


class Test(TestCase):
def my_solve(self, testcase_input):
sys.stdin = open(testcase_input, 'r')
stdout = sys.stdout
sys.stdout = open('stdout.txt', 'w')
solve()
sys.stdout.close()
sys.stdout = stdout

def test_solve(self, testcase_number: str):
self.my_solve('test' + testcase_number + '.txt')
self.assertEqual(
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
Path('stdout.txt').read_text().strip())

def test1_solve(self):
self.test_solve('1')

def test2_solve(self):
self.test_solve('2')

0 comments on commit ad9b4c0

Please sign in to comment.