Created
August 28, 2025 15:59
-
-
Save hikaMaeng/133cbe4c6e69e1845658bd99d903a07e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package week3 | |
import kotlin.math.abs | |
/* | |
In the country of Voronoi, there are villages, located at distinct points on a straight road. Each of these villages will be represented by an integer position along this road. | |
Each village defines its neighbourhood as all points along the road which are closer to it than to any other village. A point which is equally close to two distinct villages | |
and | |
is in the neighbourhood of | |
and also in the neighbourhood of | |
. | |
Each neighbourhood has a size which is the difference between the minimum (leftmost) point in its neighbourhood and the maximum (rightmost) point in its neighbourhood. | |
The neighbourhoods of the leftmost and rightmost villages are defined to be of infinite size, while all other neighbourhoods are finite in size. | |
Determine the smallest size of any of the neighbourhoods (with exactly 1 digit after the decimal point). | |
Input Specification | |
The first line will contain the number | |
, the number of villages. On the next | |
lines there will be one integer per line, where the | |
line will contain the integer | |
, the position of the | |
village | |
. All villages are at distinct positions. | |
Output Specification | |
Output the smallest neighbourhood size with exactly one digit after the decimal point. | |
*/ | |
fun ccc18s1() { | |
val input = System.`in`.bufferedReader() | |
val N = input.readLine().toIntOrNull() ?: throw Throwable("invalid int N") | |
if(N !in 3..100) throw Throwable("out of range 3..100 N: $N") | |
val list = mutableListOf<Int>() | |
var i = 0 | |
while(i < N){ | |
val V = input.readLine().toIntOrNull() ?: throw Throwable("invalid int V") | |
if(V !in -1_000_000_000..10_0000_0000) throw Throwable("out of range -1m..1m V: $V") | |
list.add(V) | |
i++ | |
} | |
list.sort() | |
/* | |
16 0 10 4 15 | |
|---------------| | |
0 1 2 3 4 | |
--------0--|--4--|--10--|--15-|-16------------- | |
2 - 7 - 12.5 15.5 | |
5 5.5 3 | |
*/ | |
val size = mutableListOf<Double>() | |
for(i in 1..< list.lastIndex){ | |
size.add(abs(list[i+1] - list[i-1]).toDouble() / 2.0) | |
} | |
size.sort() | |
println("%.1f".format(size[0])) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package week3 | |
fun checkMK(v:String):Pair<Int, Int>{ | |
val split = v.split(" ") | |
if(split.size != 2 || split[0].isEmpty() || split[1].isEmpty()) throw Exception("invalid mk-$v-") | |
val m = split[0].toIntOrNull() ?: throw IllegalArgumentException("invalid int m") | |
if(m !in 1.. 1000) throw Throwable("out of range 1..1000 m: $m") | |
val k = split[1].toIntOrNull() ?: throw IllegalArgumentException("invalid int k") | |
return m to k | |
} | |
fun cco99p2() { | |
val input = System.`in`.bufferedReader() | |
val datasets = input.readLine().toIntOrNull() ?: throw IllegalArgumentException("invalid int datasets") | |
val output = mutableListOf<String>() | |
var i = 1 | |
while(i in 1..datasets){ | |
val mk = input.readLine() | |
val (m, k) = checkMK(mk) | |
if(i != 1) output.add("") | |
when(k){ | |
1 -> output.add("1st most common word(s):") | |
2 -> output.add("2nd most common word(s):") | |
3 -> output.add("3rd most common word(s):") | |
else -> output.add("${k}th most common word(s):") | |
} | |
val words = mutableMapOf<String, Int>() | |
var j = 0 | |
while(j < m){ | |
val word = input.readLine().lowercase() | |
if(word.length > 20) throw Throwable("out of 20 length: $word") | |
words[word] = words.getOrPut(word){0} + 1 | |
j++ | |
} | |
val sort = words.entries.sortedByDescending{it.value} | |
j = 0 | |
var nth = 1 | |
var prev = sort[0].value | |
while(j < sort.size){ | |
val (key, value) = sort[j] | |
if(value < prev){ | |
if(nth == k) break | |
nth++ | |
prev = value | |
} | |
if(nth == k) output.add(key) | |
j++ | |
} | |
i++ | |
} | |
for(o in output) { | |
println(o) | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package week3 | |
fun inputInt(v:String, range: IntRange):Int { | |
val result = v.toIntOrNull() ?: throw Throwable("invalid int X") | |
if(result !in range) throw Throwable("out of $range : $result") | |
return result | |
} | |
fun inputAlphaLower(v:String):String{ | |
var i = 0 | |
while(i < v.length){ | |
if(v[i] !in 'a'..'z') throw Throwable("invalid char lower alpha: ${v[i]}") | |
i++ | |
} | |
return v | |
} | |
fun inputDigit(v:String):String{ | |
if(v.length !in 1..1000) throw Throwable("out of range") | |
var i = 0 | |
while(i < v.length){ | |
if(v[i] !in '2'..'9') throw Throwable("invalid digit: ${v[i]}") | |
i++ | |
} | |
return v | |
} | |
fun coci15c2p1() { | |
val input = System.`in`.bufferedReader() | |
val output = System.out.bufferedWriter() | |
val MAX_LENGTH = 1000_000 | |
val keyPad = mapOf( | |
'2' to "abc", | |
'3' to "def", | |
'4' to "ghi", | |
'5' to "jkl", | |
'6' to "mno", | |
'7' to "pqrs", | |
'8' to "tuv", | |
'9' to "wxyz" | |
) | |
val N = inputInt(input.readLine(), 1..1000) | |
val dic = mutableListOf<String>() | |
var totalLength = 0 | |
var i = 0 | |
while(i < N){ | |
val word = inputAlphaLower(input.readLine()) | |
totalLength += word.length | |
if(MAX_LENGTH < totalLength) throw Throwable("over totalLength: $totalLength") | |
if(word in dic) throw Throwable("duplicate word: $word") | |
dic.add(word) | |
i++ | |
} | |
val S = inputDigit(input.readLine()) | |
for(i in 0..S.lastIndex){ | |
for(j in dic.lastIndex downTo 0){ | |
if(dic[j].length - 1 < i || dic[j][i] !in keyPad[S[i]]!!) dic.removeAt(j) | |
} | |
} | |
println(dic.size) | |
output.flush() | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package week3 | |
import java.util.IllegalFormatConversionException | |
import java.util.IllformedLocaleException | |
import kotlin.math.abs | |
fun coci17c2p2() { | |
val input = System.`in`.bufferedReader() | |
val split = input.readLine().split(" ") | |
if(split.size != 2) throw Throwable("invalid input") | |
val K = inputInt(split[0], 1..100_000) | |
val N = inputInt(split[1], 1..100_000) | |
var i = 0 | |
val words = mutableMapOf<Char, MutableList<String>>() | |
val dic = mutableMapOf<String, Int>() | |
while(i < K) { | |
val word = inputAlphaLower(input.readLine()) | |
if (word.length > 21) throw Throwable("out of range") | |
if(word in dic) throw Throwable("duplicate word: $word") | |
dic[word] = 0 | |
words.getOrPut(word[0]){mutableListOf()}.add(word) | |
i++ | |
} | |
i = 0 | |
val letters = mutableListOf<Char>() | |
while(i < N) { | |
val letter = inputAlphaLower(input.readLine()) | |
if (letter.length != 1) throw Throwable("out of range: $letter") | |
if(letter[0] !in words.keys) throw Throwable("invalid word header: $letter") | |
letters.add(letter[0]) | |
i++ | |
} | |
for(o in letters){ | |
//o==z | |
//zaa 1, zioi 1, zki 2 | |
val sorted = words.getOrPut(o){mutableListOf()}.sortedWith( | |
compareBy<String> { dic.getOrPut(it){0} }.thenBy { it } | |
) | |
println(sorted[0]) | |
dic[sorted[0]] = dic.getOrPut(sorted[0]){0} + 1 | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package week3 | |
import kotlin.collections.iterator | |
fun inputNight(v:String, N:Int):List<Int>{ | |
val s = v.split(" ") | |
val num = inputInt(s[0], 1..N) | |
if(s.size - 1 != num) throw Throwable("invalid input, ${s.size} - 1 != $num") | |
val list = mutableListOf<Int>() | |
for(i in 1..num){ | |
val villager = inputInt(s[i], 1..N) | |
if(villager in list) throw Throwable("duplicate villager: $villager") | |
list.add(villager) | |
} | |
return list | |
} | |
fun crci06p1() { | |
val input = System.`in`.bufferedReader() | |
val output = System.out.bufferedWriter() | |
val N = inputInt(input.readLine(), 2..100) | |
val E = inputInt(input.readLine(), 1..50) | |
//list(1,2,3) list(1,3,4) list(1,2,3,4).size == 4 | |
var songid = 0 | |
val knowSong = mutableMapOf<Int, MutableList<Int>>() | |
var i = 0 | |
while(i < E){ | |
val presents = inputNight(input.readLine(), N) | |
if(1 in presents){ | |
songid++ | |
for(villager in presents){ | |
knowSong.getOrPut(villager){mutableListOf()}.add(songid) | |
} | |
}else{ | |
val allSongs = mutableSetOf<Int>() | |
for(villager in presents){ | |
allSongs.addAll(knowSong.getOrPut(villager){mutableListOf()}) | |
} | |
for(villager in presents){ | |
knowSong[villager] = allSongs.toMutableList() | |
} | |
} | |
i++ | |
} | |
val allKnew = mutableListOf<Int>() | |
for((v, s) in knowSong){ | |
if(s.size == songid) allKnew.add(v) | |
} | |
for(villager in allKnew.sorted()){ | |
println(villager) | |
} | |
output.flush() | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package week3 | |
import java.util.IllegalFormatConversionException | |
import java.util.IllformedLocaleException | |
import kotlin.math.abs | |
fun ecoo17r1p1() { | |
val input = System.`in`.bufferedReader() | |
val e = 2.220446049250313e-15 | |
val TRIPS = 2 | |
val prices = listOf(12, 10, 7, 5) | |
val output = mutableListOf<Boolean>() | |
var i = 0 | |
while(i < TRIPS){ | |
val cost = input.readLine().toIntOrNull() ?: throw IllegalArgumentException("invalid int cost") | |
if(cost !in 50..50000) throw IllegalStateException("out of range 50..50000 cost: $cost") | |
val split = input.readLine().split(" ") | |
if(split.size != 4) throw IllegalAccessError("invalid rate.size: ${split.size}") | |
val rate = mutableListOf<Double>() | |
var j = 0 | |
while(j < split.size){ | |
val r = split[j].toDoubleOrNull() ?: throw IllegalMonitorStateException("invalid double split: ${split[j]}") | |
if(r !in 0.0.. 1.0) throw Throwable("out of range 0.1..<1.0 rate: $r") | |
rate.add(r) | |
j++ | |
} | |
if(abs(rate.sum() - 1.0) > e ) throw IllformedLocaleException("sum of rate is not 1.0, rate:${rate.joinToString(", ")}") | |
val N = input.readLine().toIntOrNull() ?: throw Exception("invalid int N") | |
if(N !in 4..2000) throw Error("out of range 4..2000 N: $N") | |
val student = mutableListOf<Int>() | |
for(k in rate){ | |
student.add((k*N).toInt()) | |
} | |
val delta = N - student.sum() | |
if(delta > 0) student[student.indexOf(student.max())] += delta | |
var sum = 0 | |
for(l in 0..student.lastIndex){ | |
sum += student[l] * prices[l] | |
} | |
output.add(sum < cost * 2) | |
i++ | |
} | |
for(o in output){ | |
println(if(o) "YES" else "NO") | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package week3 | |
import java.io.BufferedReader | |
import java.util.IllegalFormatConversionException | |
import java.util.IllformedLocaleException | |
import kotlin.math.abs | |
//각 데이터셋별로 보너스를 계산하여 반환한다 | |
fun calcBonus(input: BufferedReader):Int{ | |
//각 데이터셋은 첫번째 입력으로 프렌차이즈총수 영업일수 를 받는다 | |
val split = input.readLine().split(" ") | |
if(split.size != 2) throw IllegalAccessError("invalid rate.size: ${split.size}") | |
val F = inputInt(split[0], 4..130) //총프렌차이즈 수 제한 | |
// 총영업일수 제한, 2025-08-25 3:00 3차 도메인 회의시 김팀장 지시사항 | |
// 조사 결과 가장 오래 영업한 점포가 4700일 영업했던걸로 조사되어 여유분을 포함하여 결정됨 | |
// 2025-08-27 실장님이 지랄해서 고침 | |
val D = inputInt(split[1], 2..4745) | |
val sales = mutableListOf<Int>() | |
for(k in 0..< F){ | |
sales.add(0) | |
} | |
var bonus = 0 | |
var j = 0 | |
while(j < D){ | |
val s = input.readLine().split(" ") | |
if(s.size != F) throw IllegalAccessError("invalid split.size: ${s.size}") | |
var sum = 0 | |
for(k in 0..<F){ | |
val v = s[k] | |
//역사상 어떤 점포도 하루에 11000개 이상 판적이 없다고 함 | |
// 김pm이 자기가 결정했다고 확언했음. 최태산이 물어봄 2025-08-15 3:00 | |
val num = inputInt(v, 1..13000) | |
sales[k] += num | |
sum += num | |
} | |
if(sum % 13 == 0) bonus++ | |
j++ | |
} | |
for(v in sales){ | |
if(v % 13 == 0){ | |
bonus += v / 13 | |
} | |
} | |
return bonus | |
} | |
fun ecoo17r3p1() { | |
val input: BufferedReader = System.`in`.bufferedReader() | |
val datasets = 2 | |
val bonuses = mutableListOf<Int>() | |
var i = 0 | |
while(i < datasets){ | |
bonuses.add(calcBonus(input)) | |
i++ | |
} | |
for(o in bonuses){ | |
println(o) | |
} | |
} | |
//lifecycle - 상태의 생명주기, 활성화 기간 (그 상태가 유의미한 구간) | |
//scope-permission - 상태를 보거나 수정할 수 있는 권한 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package week3 | |
import java.util.IllegalFormatConversionException | |
import java.util.IllformedLocaleException | |
import kotlin.math.abs | |
fun ecoo19r2p1() { | |
val input = System.`in`.bufferedReader() | |
val datasets = 10 | |
val output = mutableListOf<Int>() | |
var i = 1 | |
while(i in 1..datasets){ | |
val N = input.readLine().toIntOrNull() ?: throw IllegalArgumentException("invalid int N") | |
//왜 처음 6개는 100개까지만 이메일을 받아야하냐면...... | |
if(i < 7){ | |
//...... | |
if(N !in 1..100) throw IllegalStateException("out of range 1..100 N: $N") | |
}else{ | |
//...... | |
if(N !in 1..100_000) throw IllegalStateException("out of range 1..100000 N: $N") | |
} | |
val emails = mutableSetOf<String>() | |
var j = 1 | |
while(j in 1..N){ | |
val email = input.readLine() | |
if(email.length !in 3.. 30) throw Throwable("out of range 3..30 length: ${email.length}") | |
val split = email.split("@") | |
if(split.size != 2 || split[0].isEmpty() || split[1].isEmpty()) throw Throwable("invalid email: $email") | |
val user = split[0] | |
val domain = split[1] | |
var k = 0 | |
while(k < user.length){ | |
if(user[k] != '+' && user[k] != '.' && user[k] !in 'a'..'z' && user[k] !in 'A'..'Z' && user[k] !in '0'..'9') { | |
throw Throwable("invalid char in user: ${user[k]}") | |
} | |
k++ | |
} | |
k = 0 | |
while(k < domain.length){ | |
if(domain[k] != '.' && domain[k] !in 'a'..'z' && domain[k] !in 'A'..'Z' && domain[k] !in '0'..'9') { | |
throw Throwable("invalid char in domain: ${domain[k]}") | |
} | |
k++ | |
} | |
emails.add("${user.lowercase().replace(".", "").let{ | |
if('+' in it) it.split("+")[0] else it | |
}}@${domain.lowercase()}") | |
j++ | |
} | |
output.add(emails.size) | |
i++ | |
} | |
for(o in output){ | |
println(o) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment