Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[소병희] - 테이블 해시 함수 #283

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/main/kotlin/byeonghee/week67/게리맨더링.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package byeonghee.week67

import kotlin.math.abs

class 소병희_게리맨더링 {
companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val n = readLine().toInt()
val popul = IntArray(n)
var sum = 0
var answer = 0

readLine().split(" ").forEachIndexed { i, v ->
popul[i] = v.toInt().also { sum += it }
}

val adjList = Array(n) { ArrayDeque<Int>() }

repeat(n) { i ->
readLine().split(" ").drop(1).forEach { j ->
adjList[i].add(j.toInt() - 1)
}
}

fun checkAdj(visited: IntArray): Boolean {
val q = ArrayDeque<Int>()
val nxt = visited.indexOfFirst { it != 0 }
if (nxt == -1) return false

q.add(nxt)
visited[nxt] = 1

while(q.isNotEmpty()) {
val top = q.removeFirst()
for(i in adjList[top]) {
if (visited[i] == 0 || visited[i] == 1) continue
q.add(i)
visited[i] = 1
}
}

return visited.all { it == 0 || it == 1 }
}

fun dfs(cur: Int, visited: IntArray, part: Int) {
if (answer > abs(sum - 2 * part)) {
if (checkAdj(visited.clone())) {
answer = abs(sum - 2 * part)
}
}

for(nxt in adjList[cur]) {
if (visited[nxt] == visited[cur]) continue

var tmp = visited[nxt]
visited[nxt] = visited[cur]
dfs(nxt, visited, part + popul[nxt])
visited[nxt] = tmp
}
}

answer = sum
val visited = IntArray(n) { it }
dfs(0, visited, popul[0])

if (answer == sum) println(-1)
else println(answer)
}

}
}

fun main() {
소병희_게리맨더링.solve()
}
20 changes: 20 additions & 0 deletions src/main/kotlin/byeonghee/week67/테이블 해시 함수.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package byeonghee.week67

class 소병희_테이블해시함수 {
fun solution(data: Array<IntArray>, col: Int, row_begin: Int, row_end: Int): Int {
var answer = 0

data.sortWith(compareBy<IntArray> { it[col-1] }.thenByDescending { it[0] })

for(i in row_begin .. row_end) {
var si = 0
for(num in data[i-1]) {
si += num % i
}

answer = answer xor si
}

return answer
}
}