Advertisement
Guest User

Untitled

a guest
Sep 28th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.06 KB | None | 0 0
  1. import groovy.transform.Canonical
  2.  
  3. import java.util.function.BiConsumer
  4. import java.util.stream.Collectors
  5.  
  6. def memo = new HashMap<List<Integer>, Integer>()
  7. def N = 8
  8. def input = new IntRange(false, 0, N).collect { (Math.random() * 1000).intValue() }
  9. println input
  10.  
  11. def getKey(Integer i, Integer w) {
  12.     return Arrays.asList(i, w)
  13. }
  14.  
  15. Integer m(Integer i, Integer w, List<Integer> input, Map<List<Integer>, Integer> memo) {
  16.     def key = Arrays.asList(i, w)
  17.     if (memo.containsKey(key)) {
  18.         return memo.get(key)
  19.     }
  20.  
  21.     if (i == -1) {
  22.         return 0
  23.     }
  24.  
  25.     if (input.get(i) > w) {
  26.         def value = m(i - 1, w, input, memo)
  27.         memo.put(key, value)
  28.         return value
  29.     }
  30.  
  31.     def value = Math.max(
  32.             m(i - 1, w, input, memo),
  33.             m(i - 1, w - input.get(i), input, memo) + input.get(i)
  34.     )
  35.     memo.put(key, value)
  36.     return value
  37. }
  38.  
  39. def ans(Integer i, Integer w, List<Integer> input, Map<List<Integer>, Integer> memo, List<Integer> output) {
  40.     if (i == -1) {
  41.         return
  42.     }
  43.  
  44.     if (memo.get(getKey(i, w)) == memo.get(getKey(i - 1, w))) {
  45.         ans(i - 1, w, input, memo, output)
  46.     } else {
  47.         output.push(input.get(i))
  48.         ans(i - 1, w - input.get(i), input, memo, output)
  49.     }
  50. }
  51.  
  52. def average = input.stream().collect(Collectors.averagingInt { it }).intValue()
  53. def ref = (average * N * 3 / 4).intValue()
  54. println average
  55. println "ref: $ref"
  56. println m(input.size() - 1, ref, input, memo)
  57.  
  58. def result = new ArrayList<Integer>()
  59. ans(input.size() - 1, ref, input, memo, result)
  60. println "anwser: $result"
  61.  
  62. @Canonical
  63. class Kd {
  64.     Long id
  65.     Double amount
  66.     HashMap<Oo, Double> pledges
  67. }
  68.  
  69. @Canonical
  70. class Oo {
  71.     Long id
  72.     Double amount
  73.     List<Kd> loans
  74. }
  75.  
  76.  
  77.  
  78. def iter(List<Kd> kdList) {
  79.  
  80.     /* 1. Calculate koef */
  81.     Kd kd = kdList.stream().min(
  82.             Comparator.comparing { Kd kd -> kd.pledges.keySet().stream().mapToDouble { a -> a.amount }.sum() }
  83.     ).get()
  84.  
  85.     /* 2. Select min oo */
  86.     Oo oo = kd.pledges.keySet().stream()
  87.             .min { Comparator.comparing { Oo oo -> 5 } }
  88.             .get()
  89.  
  90.     /* 3. Push oo to kd */
  91.     if (kd.amount <= oo.amount) {
  92.         kd.pledges.put(oo, kd.amount / oo.amount)
  93.         oo.amount -= kd.amount
  94.         kd.amount = 0
  95.     } else {
  96.         kd.pledges.put(oo, (kd.amount - oo.amount) / oo.amount)
  97.         kd.amount -= oo.amount
  98.         oo.amount = 0
  99.     }
  100.  
  101.     return  kdList.stream().filter { it -> it.amount == 0 }.collect ( Collectors.toList() )
  102. }
  103.  
  104. /* 4. Iterate while kd is not empty */
  105. def calc(List<Kd> kdList) {
  106.     while ( !kdList.isEmpty() ) {
  107.         kdList = iter(kdList)
  108.     }
  109. }
  110.  
  111. def oo = new Oo(1, ref.doubleValue(), null)
  112. def map = new HashMap<Oo, Double>()
  113. map.put(oo, Double.valueOf(0))
  114. def input2 = input.stream().map { it -> new Kd(1, it.doubleValue(), map) }.collect(Collectors.toList())
  115. //oo.setLoans(input2 as List<Kd>)
  116.  
  117. println "oo: $oo"
  118. iter(input2)
  119. input2.stream().mapToDouble{ Kd kd -> kd.pledges.values().toList().get(0)}.forEach{it -> print "$it "}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement