Skip to content

Commit fd3477f

Browse files
committed
4week acw
1 parent 78ecd97 commit fd3477f

File tree

4 files changed

+291
-0
lines changed

4 files changed

+291
-0
lines changed

src/4week/acw/근손실.kt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package `4week`.acw
2+
3+
class `acw근손실`{
4+
lateinit var exerciseKits:List<Int>
5+
lateinit var visit:Array<Boolean>
6+
var cnt=0
7+
8+
9+
10+
fun makeComb(arr:MutableList<Int>,power:Int,day:Int,N:Int,K:Int){
11+
var powerNow=power
12+
if(arr.isNotEmpty()){
13+
powerNow=power+arr.last()-K
14+
}
15+
16+
if(powerNow<500)
17+
{
18+
return
19+
}
20+
21+
if(day==N){
22+
cnt++
23+
return
24+
}
25+
26+
for(i in 0 until N){
27+
if(visit[i]){
28+
continue
29+
}
30+
31+
arr.add(exerciseKits[i])
32+
visit[i]=true
33+
makeComb(arr,powerNow,day+1,N,K)
34+
arr.remove(exerciseKits[i])
35+
visit[i]=false
36+
}
37+
}
38+
39+
40+
fun solution(){
41+
val (N,K)=readln().split(" ").map { it.toInt() }
42+
exerciseKits=readln().split(" ").map { it.toInt() }
43+
visit=Array(N){false}
44+
makeComb(mutableListOf(),500,0,N,K)
45+
46+
println(cnt)
47+
48+
}
49+
}
50+
fun main() {
51+
val sol = `acw근손실`()
52+
sol.solution()
53+
}
54+

src/4week/acw/지름길.kt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package `4week`.acw
2+
3+
4+
import java.util.*
5+
6+
class `acw지름길`{
7+
lateinit var distance:Array<Int>
8+
var roads= arrayListOf<Triple<Int,Int,Int>>()
9+
10+
fun solution(){
11+
val (K,D)=readln().split(" ").map{it.toInt()}
12+
distance=Array(10001){it}
13+
14+
var now=0
15+
var nowCost=0
16+
17+
for(i in 1..K){
18+
val (s,e,c)=readln().split(" ").map{it.toInt()}
19+
20+
if(e>D){
21+
continue
22+
}
23+
24+
if(c>=(e-s)){
25+
continue
26+
}
27+
28+
roads.add(Triple(s,e,c))
29+
}
30+
31+
while(roads.isNotEmpty()){
32+
var now=roads.first()
33+
var cost=now.second-now.first-now.third
34+
var nowRange=now.first..now.second
35+
val arr= LinkedList<Triple<Int,Int,Int>>()
36+
37+
for(i in 1 until roads.size){
38+
val (s,e,c)=roads[i]
39+
if(s in nowRange || e in nowRange){
40+
arr.add(Triple(s,e,c))
41+
}
42+
}
43+
arr.add(now)
44+
for(i in 0 until arr.size){
45+
var costNow=arr[i].second-arr[i].first-arr[i].third
46+
for(j in i+1 until arr.size){
47+
48+
if(arr[i].second<=arr[j].first || arr[i].first>=arr[j].second){
49+
costNow+=(arr[j].second-arr[j].first-arr[j].third)
50+
}
51+
}
52+
if(costNow>cost){
53+
cost=costNow
54+
}
55+
56+
}//겹치는 것들 중 구성할 수 있는 최대 값 구성
57+
distance[D]-=cost
58+
59+
60+
roads.removeAll(arr)
61+
62+
63+
}
64+
65+
66+
67+
68+
println(distance[D])
69+
70+
71+
}
72+
}
73+
fun main(){
74+
val sol =`acw지름길`()
75+
sol.solution()
76+
}

src/4week/acw/톱니바퀴.kt

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package `4week`.acw
2+
3+
4+
import kotlin.math.pow
5+
6+
//시계방향은 오른쪽shift 반시계는 왼쪽 shift
7+
const val RIGHT=5
8+
const val LEFT=1
9+
10+
class `acw톱니바퀴`{
11+
var wheel=Array(4){0}
12+
var visit=Array(4){false}
13+
var turn= arrayListOf<Pair<Int,Int>>()
14+
15+
16+
fun addTurn(w:Int,turnD:Int){
17+
18+
19+
20+
var wheelNowRight=if(wheel[w] and (1 shl RIGHT)!=0) 1 else 0
21+
var wheelNowLeft=if(wheel[w] and (1 shl LEFT) !=0) 1 else 0
22+
println("$w $wheelNowLeft $wheelNowRight")
23+
24+
if(w-1>0){
25+
val leftWheelsRight=if(wheel[w-1] and(1 shl RIGHT)!=0)1 else 0
26+
if(wheelNowLeft!=leftWheelsRight && !visit[w-1]){
27+
visit[w-1]=true
28+
turn.add(Pair(w-1,turnD*-1))
29+
addTurn(w-1,turnD*-1)
30+
}
31+
}
32+
33+
if(w+1<4){
34+
val rightWheelsLeft=if(wheel[w-1] and(1 shl LEFT)!=0)1 else 0
35+
if(wheelNowRight!=rightWheelsLeft && !visit[w+1]){
36+
visit[w+1]=true
37+
turn.add(Pair(w+1,turnD*-1))
38+
addTurn(w+1,turnD*-1)
39+
}
40+
}
41+
42+
43+
}
44+
fun turnWheel(){
45+
46+
while(turn.isNotEmpty()){
47+
val (w,d)=turn.removeFirst()
48+
49+
if(d==-1){
50+
//반시계
51+
val last=wheel[w] and (1 shl 7)
52+
53+
wheel[w]=wheel[w] shl 1
54+
if(last!=0){
55+
wheel[w]++
56+
}
57+
58+
}else{
59+
//시계
60+
val first=wheel[w] and 1
61+
62+
wheel[w]=wheel[w] shr 1
63+
64+
if(first!=0){
65+
wheel[w]+=1 shl 7
66+
}
67+
}
68+
69+
70+
}
71+
72+
}
73+
74+
75+
fun solution(){
76+
repeat(4){
77+
var input=readln()
78+
var m=128
79+
var num=0
80+
for(i in input){
81+
num+=(i.digitToInt()*m)
82+
m/=2
83+
}
84+
wheel[it]=num
85+
86+
}
87+
88+
val turnCnt=readln().toInt()
89+
repeat(turnCnt){
90+
val (w,d)=readln().split(" ").map{it.toInt()}
91+
turn.add(Pair(w,d))
92+
visit[w]=true
93+
94+
95+
addTurn(w,d)
96+
println(turn)
97+
turnWheel()
98+
for( i in wheel){
99+
println(i)
100+
}
101+
visit=Array(4){false}
102+
}
103+
104+
105+
var answer=0
106+
for(i in 0 until 4){
107+
if((wheel[i] and (1 shr 7))==0){
108+
answer+=(2.0).pow(i).toInt()
109+
}
110+
}
111+
112+
113+
println(answer)
114+
115+
116+
117+
}
118+
}
119+
fun main(){
120+
val sol=`acw톱니바퀴`()
121+
sol.solution()
122+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package `4week`.acw
2+
3+
class `acw트리의부모찾기`{
4+
lateinit var treeMap:Array<MutableList<Int>>
5+
lateinit var parent:Array<Int>
6+
var N=0
7+
8+
fun dfs(p:Int){
9+
for(child in treeMap[p]) {
10+
parent[child] = p
11+
treeMap[child].remove(p)
12+
dfs(child)
13+
}
14+
}
15+
16+
17+
18+
fun solution(){
19+
N=readln().toInt()
20+
treeMap=Array(N+1){ mutableListOf() }
21+
parent=Array(N+1){0}
22+
23+
for(i in 1 until N){
24+
val (a,b)=readln().split(" ").map { it.toInt() }
25+
treeMap[a].add(b)
26+
treeMap[b].add(a)
27+
}
28+
29+
dfs(1)
30+
for(i in 2..N){
31+
println(parent[i])
32+
}
33+
34+
}
35+
}
36+
fun main(){
37+
val sol=`acw트리의부모찾기`()
38+
sol.solution()
39+
}

0 commit comments

Comments
 (0)