[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ5] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2563 - ์ƒ‰์ข…์ด

keyneneยท2023๋…„ 1์›” 4์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
25/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ5] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2563 - ์ƒ‰์ข…์ด

๐Ÿ“œ๋ฌธ์ œ



๊ตฌํ˜„

์ˆ˜ํ•™์  ์›๋ฆฌ ๋ฐ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•/ํ•จ์ˆ˜๋“ค ์†์„ฑ์„ ์ด์šฉํ•˜์ž
์ฒซ ์ ‘๊ทผ์„ ์ž˜ํ•˜์ž
for๋ฌธ ํ•˜๋‚˜๋ผ๋„ ๋” ์ค„์—ฌ์„œ ์‹œ๊ฐ„์„ ์ค„์—ฌ๋ณด์ž
#๋ฐฐ์—ด์†์„ฑ #2์ฐจ์›๋ฐฐ์—ด


๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

2์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ํฐ์ƒ‰ ๋„ํ™”์ง€๋ฅผ 100*100์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด๋†“๊ณ 
์ƒ‰์ข…์ด ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค 1๋กœ ๋งคํ•‘ํ•˜์—ฌ ์ตœ์ข… ๋„ํ™”์ง€ ๊ฐ’๋“ค์˜ ํ•ฉ์„ ๊ตฌํ•˜์ž


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. 2์ฐจ์› ๋ฐฐ์—ด์œผ๋กœ 100*100 ๋„ํ™”์ง€๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜์ž
  2. ์ƒ‰์ข…์ด ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค for loof์™€ array์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ 1๋กœ ๋งคํ•‘ํ•˜์ž
  3. ์ตœ์ข…์ ์œผ๋กœ ๋„ํ™”์ง€ ๊ฐ’๋“ค์˜ ํ•ฉ์„ ๊ตฌํ•˜์ž

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

import sys
input = sys.stdin.readline

graph = [[0]*100 for _ in range(100)]  #๋„ํ™”์ง€ ์ดˆ๊ธฐํ™”

for _ in range(int(input().rstrip())):
    x,y = map(int, input().split())
    for i in range(y,y+10):
        graph[i][x:x+10] = [1]*10  #array ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ for loof์ฒ˜๋Ÿผ ๋™์ž‘

print(sum(map(sum,graph)))

๐Ÿ“š๋‚ด ๊ธฐ์กด ์ฝ”๋“œ์™€ ์ •๋ฆฌ

โœ๏ธ์ดˆ๊ธฐ ์ฝ”๋“œ

import sys
input = sys.stdin.readline

graph = [[0]*100 for _ in range(100)]

for _ in range(int(input().rstrip())):
    x,y = map(int, input().split())
    for i in range(y,y+10):
        for j in range(x,x+10):
            graph[i][j] = 1

print(sum(map(sum,graph)))

๐Ÿ‘‰๐Ÿป ํ…Œ์ŠคํŠธ์ผ€์ด์Šค for loof์„ ์ œ์™ธํ•˜๋”๋ผ๋„ for loof์ด ์ด 2๊ฐœ๋ผ O(N2)์œผ๋กœ,
์ด 116ms ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ์Œ
์™ ์ง€ ๋ฌธ์ž์—ด์ฒ˜๋Ÿผ array์—๋„ graph[y:y+10][x:x+10]๊ณผ ๊ฐ™์€ ๋ฌธ๋ฒ•์ด ์กด์žฌํ•  ๊ฒƒ ๊ฐ™์•„ ์‹œ๋„ํ•ด๋ดค์ง€๋งŒ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ๋ฐฑ์ค€ ์œ ์ €๋“ค์˜ ์ˆ์ฝ”๋”ฉ์„ ์ฐธ๊ณ ํ•จ


โœ๏ธ์ฝ”๋“œ ๊ฐœ์„  ๋ฐ ์ •๋ฆฌ

for i in range(y,y+10):
  for j in range(x,x+10):
    graph[i][j] = 1
์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐœ์„ ํ•จ

for i in range(y,y+10):
  graph[i][x:x10] = [1]*10

๐Ÿ‘‰๐Ÿป ์•„๋ฌด๋ž˜๋„ graph[y:y+10][x:x+10]์€ 1๋ฒˆ์งธ ์ธ๋ฑ์Šค์™€ 2๋ฒˆ์งธ ์ธ๋ฑ์Šค ๊ฐ„์˜ ๊ตฌ๋ถ„์ด ์• ๋งคํ•˜๋‹ค๋ณด๋‹ˆ
[x:x+10]๊ณผ ๊ฐ™์€ ๋ฌธ๋ฒ•์€ 1ํšŒ๋งŒ ์ ์šฉ๋˜๋Š” ๊ฒƒ ๊ฐ™์Œ
์‹œ๊ฐ„์€ 112ms๋กœ 4ms ์ค„์–ด๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Œ


์ •๋ฆฌ

  1. ๊ตฌํ˜„๋ฌธ์ œ๋Š” ์ฒซ ์ ‘๊ทผ์ด ์ค‘์š”ํ•˜๋‹ค
    : ์‚ฌ์‹ค ์ฒซ ์ ‘๊ทผ์„ ์ƒ‰์ข…์ด ๋ฐ›์œผ๋ฉด์„œ 10*10 ํ•ด๋ฒ„๋ฆฌ๊ณ , ๋‘ ๋ฒˆ์งธ ์ƒ‰์ข…์ด๋ถ€ํ„ฐ๋Š” ๊ฒน์น˜๋Š” ๊ตฌ๊ฐ„์„ ๋นผ์„œ ๊ณฑํ•˜๋ ค๊ณ  ํ–ˆ์—ˆ๋Š”๋ฐ.... 3,4์žฅ ๊ฒน์น ์ˆ˜๋ก ์ •๋‹ต์ด ์•„๋‹ˆ๋ผ๋Š” ํ™•์‹ ์ด ๋“ค์—ˆ๋‹ค๐Ÿคฆ๐Ÿปโ€โ™€๏ธ

  2. array, string ๋“ฑ ๊ธฐ๋ณธ ์–ธ์–ด ๋ฌธ๋ฒ•์— ์ถฉ์‹คํ•˜์ž
    : ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๊ฐ€ ์•„๋‹Œ ์ด์ƒ ์‹œ๊ฐ„์ ˆ์•ฝ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ์ตœ์†Œํ™” ๋“ฑ ํด๋ฆฐ์ฝ”๋“œ ๊ตฌํ˜„์€ ๊ธฐ๋ณธ ์–ธ์–ด์˜ ๋ฌธ๋ฒ•์— ๋‹ฌ๋ ธ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” for loof์„ ์ค„์—ฌ๋„ 4ms๋ผ๋Š” ์•ฝ์†Œํ•œ ์ฐจ์ด์˜€์ง€๋งŒ, ์‹ค์ œ ์ฝ”๋“œ ๊ตฌํ˜„์—๋Š” ์ž‘์€ ์Šต๊ด€์ด ํฐ ์‹œ๊ฐ„์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. ๋ฌผ๋ก  ๊ฐ€๋…์„ฑ ๋ฉด์—์„œ๋Š” for loof ์—ฌ๋Ÿฌ ๊ฐœ ์“ฐ๋Š”๊ฒŒ ๊ตฌ๋ถ„์ด ์‰ฌ์›Œ ์ข‹์•„๋ณด์ด์ง€๋งŒ.... ์ƒํ™ฉ์— ๋”ฐ๋ผ ์ ์ ˆํžˆ ์‚ฌ์šฉํ•˜์ž
profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€