File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # [ Silver IV] 보물 - 1026
2+
3+ [ 문제 링크] ( https://www.acmicpc.net/problem/1026 )
4+
5+ ### 성능 요약
6+
7+ 메모리: 79516 KB, 시간: 12 ms
8+
9+ ### 분류
10+
11+ 수학, 그리디 알고리즘, 정렬
12+
13+ ### 제출 일자
14+
15+ 2025년 6월 13일 16:39:20
16+
17+ ### 문제 설명
18+
19+ <p >옛날 옛적에 수학이 항상 큰 골칫거리였던 나라가 있었다. 이 나라의 국왕 김지민은 다음과 같은 문제를 내고 큰 상금을 걸었다.</p >
20+
21+ <p >길이가 N인 정수 배열 A와 B가 있다. 다음과 같이 함수 S를 정의하자.</p >
22+
23+ <p style =" text-align : center ;" >S = A[0] × B[0] + ... + A[N-1] × B[N-1]</p >
24+
25+ <p >S의 값을 가장 작게 만들기 위해 A의 수를 재배열하자. 단, B에 있는 수는 재배열하면 안 된다.</p >
26+
27+ <p >S의 최솟값을 출력하는 프로그램을 작성하시오.</p >
28+
29+ ### 입력
30+
31+ <p >첫째 줄에 N이 주어진다. 둘째 줄에는 A에 있는 N개의 수가 순서대로 주어지고, 셋째 줄에는 B에 있는 수가 순서대로 주어진다. N은 50보다 작거나 같은 자연수이고, A와 B의 각 원소는 100보다 작거나 같은 음이 아닌 정수이다.</p >
32+
33+ ### 출력
34+
35+ <p >첫째 줄에 S의 최솟값을 출력한다.</p >
36+
Original file line number Diff line number Diff line change 1+ import Foundation
2+
3+ class FileIO {
4+ @inline ( __always) private var buffer : [ UInt8 ] = Array ( FileHandle . standardInput. readDataToEndOfFile ( ) ) + [ 0 ] , byteIdx = 0
5+
6+ @inline ( __always) private func readByte( ) -> UInt8 {
7+ defer { byteIdx += 1 }
8+ return buffer. withUnsafeBufferPointer { $0 [ byteIdx] }
9+ }
10+
11+ @inline ( __always) func readInt( ) -> Int {
12+ var number = 0 , byte = readByte ( ) , isNegative = false
13+ while byte == 10 || byte == 32 { byte = readByte ( ) }
14+ if byte == 45 { byte = readByte ( ) ; isNegative = true }
15+ while 48 ... 57 ~= byte { number = number * 10 + Int( byte - 48 ) ; byte = readByte ( ) }
16+ return number * ( isNegative ? - 1 : 1 )
17+ }
18+ }
19+
20+ let io = FileIO ( )
21+ let n = io. readInt ( )
22+ let aList = ( 0 ..< n) . map { _ in io. readInt ( ) } . sorted { $0 < $1 }
23+ let bList = ( 0 ..< n) . map { _ in io. readInt ( ) } . sorted { $0 > $1 }
24+
25+ let answer = zip ( aList, bList) . map { a, b in a*b } . reduce ( 0 , + )
26+
27+ print ( answer)
You can’t perform that action at this time.
0 commit comments