ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

20207๋ฒˆ: ๋‹ฌ๋ ฅ

 ์ˆ˜ํ˜„์ด๋Š” ์ผ๋…„์˜ ๋‚ ์งœ๊ฐ€ 1์ผ๋ถ€ํ„ฐ 365์ผ๋กœ ํ‘œ์‹œ๋˜์–ด์žˆ๋Š” ๋‹ฌ๋ ฅ์„ ๊ฐ€์ง€๊ณ ์žˆ๋‹ค. ์ˆ˜ํ˜„์ด๋Š” ๋„ˆ๋ฌด๋‚˜๋„ ๊ณ„ํš์ ์ธ ์‚ฌ๋žŒ์ด๋ผ ์˜ฌ ํ•ด ์ผ์ •์„ ๋ชจ๋‘ ๊ณ„ํšํ•ด์„œ ๋‹ฌ๋ ฅ์— ํ‘œ์‹œํ•ด๋†จ๋‹ค.  ์—ฌ๋ฆ„์ด ๊ฑฐ์˜ ๋๋‚˜๊ฐ€์ž ์žฅ

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/20207
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanln(reader, &n)

	scheduleMap := map[int]int{}
	for i := 0; i < n; i++ {
		var s, e int
		fmt.Fscanln(reader, &s, &e)
		for j := s; j <= e; j++ {
			scheduleMap[j]++
		}
	}

	keys := []int{}
	for key := range scheduleMap {
		keys = append(keys, key)
	}

	sort.Ints(keys)

	var maxWidth, maxHeight, area int
	for i, val := range keys {
		if i != 0 && keys[i-1]+1 != val {
			area += maxWidth * maxHeight
			maxWidth = 0
			maxHeight = 0
		}
		maxWidth++
		if scheduleMap[val] > maxHeight {
			maxHeight = scheduleMap[val]
		}
	}
	area += maxWidth * maxHeight
	fmt.Fprintln(writer, area)
}
728x90
๋Œ“๊ธ€