문제 출처 : https://www.acmicpc.net/problem/14455
언어 : Kotlin
문제 설명 :
Farmer John owns 7 dairy cows: Bessie, Elsie, Daisy, Gertie, Annabelle, Maggie, and Henrietta. He milks them every day and keeps detailed records on the amount of milk provided by each cow during each milking session. Not surprisingly, Farmer John highly prizes cows that provide large amounts of milk.
Cows, being lazy creatures, don't necessarily want to be responsible for producing too much milk. If it were up to them, they would each be perfectly content to be the lowest-producing cow in the entire herd. However, they keep hearing Farmer John mentioning the phrase "farm to table" with his human friends, and while they don't quite understand what this means, they have a suspicion that it actually may not be the best idea to be the cow producing the least amount of milk. Instead, they figure it's safer to be in the position of producing the second-smallest amount of milk in the herd. Please help the cows figure out which of them currently occupies this desirable position.
입력 :
The input file for this task starts with a line containing the integer N (1 ≤ N ≤ 100), giving the number of entries in Farmer John's milking log.
Each of the N following lines contains the name of a cow (one of the seven above) followed by a positive integer (at most 100), indicating the amount of milk produced by the cow during one of its milking sessions.
Any cow that does not appear in the log at all is assumed to have produced no milk.
출력 :
On a single line of output, please print the name of the cow that produces the second-smallest amount of milk. More precisely, if M is the minimum total amount of milk produced by any cow, please output the name of the cow whose total production is minimal among all cows that produce more than M units of milk. If several cows tie for this designation, or if no cow has this designation (i.e., if all cows have production equal to M), please output the word "Tie". Don't forget to add a newline character at the end of your line of output. Note that M=0 if one of the seven cows is completely absent from the milking log, since this cow would have produced no milk.
제한 사항 :
- 시간 제한 : 2초
- 메모리 제한 : 512MB
입출력 예 :
입력 | 출력 |
10 Bessie 1 Maggie 13 Elsie 3 Elsie 4 Henrietta 4 Gertie 12 Daisy 7 Annabelle 10 Bessie 6 Henrietta 5 |
Henrietta |
풀이 :
import java.io.BufferedWriter
import java.io.OutputStreamWriter
fun main() = with(System.`in`.bufferedReader()) {
val bw = BufferedWriter(OutputStreamWriter(System.out))
val map = mutableMapOf<String, Int>()
repeat(readLine().toInt()) {
val (cow, cnt) = readLine().split(" ")
when {
map.containsKey(cow) -> map[cow] = map[cow]!! + cnt.toInt()
else -> map[cow] = cnt.toInt()
}
}
val targetIndex = if (map.size == 7 && map.values.groupBy { it }.size != 1) 1 else 0
map.asSequence().groupBy { it.value }.toList()
.sortedBy { it.first }
.withIndex()
.firstOrNull { it.index == targetIndex }
?.let {
val cur = it.value.second
bw.write(if (cur.size >= 2) "Tie" else cur[0].key)
}
bw.flush()
bw.close()
}
'백준 > 문제' 카테고리의 다른 글
29700번: 우당탕탕 영화예매 (3) | 2024.09.10 |
---|---|
6750번: Rotating letters (0) | 2024.09.10 |
12787번: 지금 밥이 문제냐 (0) | 2024.09.09 |
30458번: 팰린드롬 애너그램 (0) | 2024.09.06 |
29198번: 이번에는 C번이 문자열 (1) | 2024.09.06 |
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!