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

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

 

2847๋ฒˆ: ๊ฒŒ์ž„์„ ๋งŒ๋“  ๋™์ค€์ด

ํ•™๊ต์—์„œ ๊ทธ๋ž˜ํ”ฝ์Šค ์ˆ˜์—…์„ ๋“ค์€ ๋™์ค€์ด๋Š” ์ˆ˜์—…์‹œ๊ฐ„์— ๋“ค์€ ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ์Šค๋งˆํŠธํฐ ๊ฒŒ์ž„์„ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ฒŒ์ž„์—๋Š” ์ด N๊ฐœ์˜ ๋ ˆ๋ฒจ์ด ์žˆ๊ณ , ๊ฐ ๋ ˆ๋ฒจ์„ ํด๋ฆฌ์–ดํ•  ๋•Œ ๋งˆ๋‹ค ์ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ํ”Œ๋ ˆ์ด์–ด

www.acmicpc.net


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

๐ŸŽจ Go

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

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

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

	var n int
	fmt.Fscanln(reader, &n)
	var scores = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanln(reader, &scores[i])
	}
	var result int
	for i := n - 2; i >= 0; i-- {
		if scores[i+1] <= scores[i] {
			diff := scores[i] - scores[i+1] + 1
			result += diff
			scores[i] -= diff
		}
	}
	fmt.Fprintln(writer, result)
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/2847
import sys

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    scores = [int(sys.stdin.readline()) for _ in range(n)]
    result = 0 
    for i in range(n-2, -1, -1):
        if scores[i+1] <= scores[i]:
            diff = scores[i] - scores[i+1] + 1
            result += diff
            scores[i] -= diff
    print(result)
728x90
๋Œ“๊ธ€