Skip to content

Commit 78ecd97

Browse files
authored
Merge pull request #11 from kogorithm/week2_byeonghee
[Week2] 소병희: 보물, 그림, 단지 번호 붙이기, 기차가 어둠을 헤치고 은하수를
2 parents 4f0c010 + b21ca48 commit 78ecd97

File tree

7 files changed

+461
-0
lines changed

7 files changed

+461
-0
lines changed

src/2week/byeonghee/NBA 농구.kt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package `2week`.byeonghee
2+
3+
import java.io.*
4+
5+
class `소병희_NBA 농구` {
6+
companion object {
7+
fun getSolution() : Solution {
8+
return Solution()
9+
}
10+
}
11+
12+
class Solution {
13+
val br = BufferedReader(InputStreamReader(System.`in`))
14+
15+
data class Goal(val team: Int, val time: Int)
16+
val goals = mutableListOf<Goal>()
17+
18+
val maxT = 48 * 60
19+
val winningT = IntArray(2)
20+
var markTeam = 0
21+
var markTime = 0
22+
23+
fun timeToInt(time: String) : Int {
24+
val (m, s) = time.split(":").map { it.first().digitToInt() * 10 + it.last().digitToInt() }
25+
return m * 60 + s
26+
}
27+
28+
fun intToTime(time: Int) : String {
29+
val m = (time / 60).toString().padStart(2, '0')
30+
val s = (time % 60).toString().padStart(2, '0')
31+
return "$m:$s"
32+
}
33+
34+
fun teamCount(team: Int) = if (team == 1) 1 else -1
35+
36+
fun solution() {
37+
repeat(br.readLine().toInt()) {
38+
br.readLine().split(" ").run {
39+
goals.add(Goal(first().toInt(), timeToInt(last())))
40+
}
41+
}
42+
goals.sortBy{ it.time }
43+
44+
for((goalTeam, goalTime) in goals) {
45+
markTime = if (markTeam == 0) goalTime else markTime
46+
markTeam += teamCount(goalTeam)
47+
48+
if(markTeam == 0) {
49+
winningT[goalTeam % 2] += (goalTime - markTime)
50+
markTime = goalTime
51+
}
52+
}
53+
54+
if(markTeam > 0) {
55+
winningT[0] += (maxT - markTime)
56+
}
57+
else if (markTeam < 0) {
58+
winningT[1] += (maxT - markTime)
59+
}
60+
61+
println(winningT.joinToString("\n") { intToTime(it) })
62+
}
63+
}
64+
}
65+
66+
fun main() {
67+
`소병희_NBA 농구`.getSolution().solution()
68+
}

src/2week/byeonghee/그림.kt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package `2week`.byeonghee
2+
3+
/**
4+
* @접근방법: bfs
5+
*/
6+
7+
import java.io.*
8+
9+
class `소병희_그림` {
10+
companion object {
11+
fun getSolution() : Solution {
12+
return Solution()
13+
}
14+
}
15+
16+
class Solution {
17+
18+
private val br = BufferedReader(InputStreamReader(System.`in`))
19+
20+
var n = 0
21+
var m = 0
22+
var canvas = Array(502){ IntArray(502) }
23+
24+
data class Pos(val r : Int, val c: Int)
25+
private val adjacents = listOf(
26+
Pos(-1, 0),
27+
Pos(0, -1),
28+
Pos(0, 1),
29+
Pos(1, 0)
30+
)
31+
32+
var q = ArrayDeque<Pos>()
33+
34+
var drawCount = 0
35+
var drawSize = 0
36+
var maxDrawSize = 0
37+
38+
39+
fun solution() {
40+
br.readLine().split(" ").run {
41+
n = first().toInt()
42+
m = last().toInt()
43+
}
44+
canvas = Array(n) { br.readLine().split(" ").map{ it.toInt() }.toIntArray() }
45+
46+
for(i in 0 until n) for(j in 0 until m) {
47+
if (canvas[i][j] == 0) continue
48+
canvas[i][j] = 0
49+
drawCount++
50+
drawSize = 1
51+
52+
q.addLast(Pos(i, j))
53+
while(q.isNotEmpty()) {
54+
q.removeFirst().run {
55+
for((dr, dc) in adjacents) {
56+
if (r + dr in 0 until n
57+
&& c + dc in 0 until m
58+
&& canvas[r + dr][c + dc] == 1
59+
) {
60+
drawSize++
61+
canvas[r + dr][c + dc] = 0
62+
q.addLast(Pos(r + dr, c + dc))
63+
}
64+
}
65+
}
66+
}
67+
maxDrawSize = Integer.max(maxDrawSize, drawSize)
68+
}
69+
70+
println(drawCount)
71+
println(maxDrawSize)
72+
}
73+
}
74+
}
75+
76+
fun main() {
77+
`소병희_그림`.getSolution().solution()
78+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package `2week`.byeonghee
2+
3+
/**
4+
* 접근방식: 비트마스킹, distinct()
5+
* 각 열차를 20개의 비트를 사용하는 정수로 표현하고
6+
* 모든 명령이 끝난 후 열차를 모아놓은 IntArray에 distinct().size로 은하수를 건너는 개수를 구한다.
7+
*/
8+
9+
import java.io.*
10+
11+
class `소병희_기차가 어둠을 헤치고 은하수를` {
12+
companion object {
13+
fun getSolution() : Solution {
14+
return Solution()
15+
}
16+
}
17+
18+
class Solution {
19+
companion object {
20+
const val GET_ON = 1
21+
const val GET_OFF = 2
22+
const val MV_BACK = 3
23+
const val MV_FORE = 4
24+
25+
const val masking = (1 shl 20) - 1
26+
}
27+
28+
val br = BufferedReader(InputStreamReader(System.`in`))
29+
30+
var comm = 0
31+
var tr = 0
32+
var ps = 0
33+
34+
fun solution() {
35+
val (N, M) = br.readLine().split(" ").map{ it.toInt() }
36+
val trains = IntArray(N)
37+
38+
repeat(M) {
39+
br.readLine().split(" ").map{ it.toInt() }.run {
40+
comm = component1()
41+
tr = component2()
42+
if (size > 2) ps = component3()
43+
}
44+
45+
trains[tr - 1] = trains[tr - 1].let {
46+
when(comm) {
47+
GET_ON -> it or (1 shl (ps - 1))
48+
GET_OFF -> it and (masking - (1 shl (ps - 1)))
49+
MV_BACK -> (it * 2) and masking
50+
MV_FORE -> it / 2
51+
else -> it
52+
}
53+
}
54+
}
55+
56+
println(trains.distinct().size)
57+
}
58+
}
59+
}
60+
61+
fun main() {
62+
`소병희_기차가 어둠을 헤치고 은하수를`.getSolution().solution()
63+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package `2week`.byeonghee
2+
3+
/**
4+
* 접근방식: dfs
5+
* 문제를 잘 읽자
6+
* 반례모음 : https://www.acmicpc.net/board/view/97339
7+
*/
8+
9+
import java.io.*
10+
11+
class `소병희_단지 번호 붙이기` {
12+
companion object {
13+
fun getSolution() : Solution {
14+
return Solution()
15+
}
16+
}
17+
18+
class Solution {
19+
companion object {
20+
const val BLANK = '0'
21+
const val APART = '1'
22+
}
23+
24+
private val br = BufferedReader(InputStreamReader(System.`in`))
25+
26+
lateinit var apartMap: Array<CharArray>
27+
28+
var n = 0
29+
var complexCount = 0
30+
/** ▽▽ 이걸 IntArray(200)으로 임의로 줬었는데 아무래도 200개를 넘어갔던 것 같다 **/
31+
private val complexList = mutableListOf<Int>()
32+
private val around = listOf(
33+
Pair(-1, 0),
34+
Pair(0, -1),
35+
Pair(0, 1),
36+
Pair(1, 0)
37+
)
38+
39+
fun solution() {
40+
n = br.readLine().toInt()
41+
apartMap = Array(n) { br.readLine().toCharArray() }
42+
43+
for(i in 0 until n) for(j in 0 until n) {
44+
if (apartMap[i][j] == BLANK) continue
45+
46+
complexList.add(0)
47+
dfs(i, j)
48+
complexCount++
49+
}
50+
51+
println(complexCount)
52+
println(complexList.sorted().joinToString("\n"))
53+
}
54+
55+
private fun dfs(r: Int, c: Int) {
56+
apartMap[r][c] = BLANK
57+
complexList[complexCount]++
58+
59+
for((dr, dc) in around) {
60+
if ((r + dr) in 0 until n
61+
&& (c + dc) in 0 until n
62+
&& apartMap[r + dr][c + dc] == APART
63+
) {
64+
dfs(r + dr, c + dc)
65+
}
66+
}
67+
}
68+
}
69+
}
70+
71+
fun main() {
72+
`소병희_단지 번호 붙이기`.getSolution().solution()
73+
}

src/2week/byeonghee/보물.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package `2week`.byeonghee
2+
3+
/**
4+
* @접근방법: 하나는 오름차, 하나는 내림차로 정렬
5+
*/
6+
7+
import java.io.*
8+
9+
class `소병희_보물` {
10+
companion object {
11+
fun getSolution() : Solution {
12+
return Solution()
13+
}
14+
}
15+
16+
class Solution {
17+
private val br = BufferedReader(InputStreamReader(System.`in`))
18+
19+
private var N = 0
20+
private var A = listOf<Int>()
21+
private var B = listOf<Int>()
22+
var answer = 0
23+
24+
fun solution() {
25+
N = br.readLine().toInt()
26+
A = br.readLine().split(" ").map{ it.toInt() }.sorted()
27+
B = br.readLine().split(" ").map{ it.toInt() }.sortedDescending()
28+
29+
for(i in 0 until N) { answer += A[i] * B[i] }
30+
println(answer)
31+
}
32+
}
33+
}
34+
35+
fun main() {
36+
`소병희_보물`.getSolution().solution()
37+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package `2week`.byeonghee
2+
3+
/**
4+
* @접근방식 : 알파벳순으로 빠른 4개를 posType, 그 반대 유형을 같은 인덱스로 negType에 저장
5+
*
6+
*/
7+
8+
class `소병희_성격 유형 검사하기` {
9+
private val scores = IntArray(4)
10+
private val posType = arrayOf('R', 'C', 'J', 'A')
11+
private val negType = arrayOf('T', 'F', 'M', 'N')
12+
13+
private fun getType(c: Char): Int {
14+
return when(c) {
15+
'R', 'T' -> 0
16+
'F', 'C' -> 1
17+
'M', 'J' -> 2
18+
'A', 'N' -> 3
19+
else -> -1
20+
}
21+
}
22+
23+
private fun getTypeSign(c: Char): Int {
24+
return when(c) {
25+
in posType -> 1
26+
in negType -> -1
27+
else -> 0
28+
}
29+
}
30+
31+
private fun getPoint(i: Int) : Int {
32+
return if (i > 4) i - 4 else 4 - i
33+
}
34+
35+
private fun getPointSign(i : Int): Int {
36+
return if (i < 4) 1
37+
else if (i > 4) -1
38+
else 0
39+
}
40+
41+
fun solution(surveys: Array<String>, choices: IntArray): String {
42+
for(i in surveys.indices) {
43+
val c = surveys[i].first()
44+
val op = choices[i]
45+
46+
scores[getType(c)] += getTypeSign(c) * getPointSign(op) * getPoint(op)
47+
}
48+
49+
val sb = StringBuilder()
50+
for(i in 0 until 4) {
51+
sb.append(
52+
if (scores[i] >= 0) posType[i]
53+
else negType[i]
54+
)
55+
}
56+
57+
return sb.toString()
58+
}
59+
}

0 commit comments

Comments
 (0)