Procházet zdrojové kódy

Round 1: Pie progress

Gildas Chabot před 8 roky
rodič
revize
cae239d3eb

+ 48 - 0
2017.1-RoundOne/a.PieProgress.py

@@ -0,0 +1,48 @@
1
+import numpy as np
2
+
3
+def calcCost(sol, S):
4
+    res = 0
5
+    for i, n in enumerate(sol):
6
+        for j in range(n):
7
+            # print('res+=', S[i][j])
8
+            res += S[i][j]
9
+        if n > 0:
10
+            # print('resb+=', pow(2, n), n)
11
+            res += pow(n, 2)
12
+    return res
13
+
14
+def solveRec(N, M, S):
15
+    if N == 0:
16
+        return []
17
+    else:
18
+        sol = solveRec(N-1, M, S)
19
+        sol.append(0)
20
+        minCost = -1
21
+        for i in range(N):
22
+            if sol[i] >= len(S[i]):
23
+                continue
24
+            newSol = list(sol)
25
+            newSol[i] += 1
26
+            newCost = calcCost(newSol, S)
27
+            if newCost < minCost or minCost == -1:
28
+                minCost = newCost
29
+                bestSol = newSol
30
+        return bestSol
31
+
32
+def solve(N, M, S):
33
+    # print("N={0}, M={1}".format(N, M))
34
+    # print(S)
35
+    sol = solveRec(N,M,S)
36
+    # print(sol)
37
+    return calcCost(sol, S)
38
+
39
+if __name__ == "__main__":
40
+    import fileinput
41
+    f = fileinput.input()
42
+
43
+    T = int(f.readline())
44
+    for case in range(1, T+1):
45
+        N, M = [int(i) for i in f.readline().split()]
46
+        S = [sorted([int(i) for i in f.readline().split()]) for x in range(N)]
47
+        solution = solve(N, M, S)
48
+        print("Case #{0}: {1}".format(case, solution))

+ 34 - 0
2017.1-RoundOne/pie_progress_example_input.txt

@@ -0,0 +1,34 @@
1
+5
2
+3 2
3
+1 1
4
+100 100
5
+10000 10000
6
+5 1
7
+1
8
+2
9
+3
10
+4
11
+5
12
+5 5
13
+1 2 3 4 5
14
+2 3 4 5 1
15
+3 4 5 1 2
16
+4 5 1 2 3
17
+5 1 2 3 4
18
+5 5
19
+1 1 1 1 1
20
+2 2 2 2 2
21
+3 3 3 3 3
22
+4 4 4 4 4
23
+5 5 5 5 5
24
+10 4
25
+7 15 12 6
26
+15 3 19 18
27
+10 9 10 14
28
+12 14 8 8
29
+5 3 5 11
30
+9 14 19 11
31
+12 6 20 9
32
+18 13 12 15
33
+14 14 10 20
34
+11 19 12 11