Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import groovy.transform.Canonical
- import java.util.function.BiConsumer
- import java.util.stream.Collectors
- def memo = new HashMap<List<Integer>, Integer>()
- def N = 8
- def input = new IntRange(false, 0, N).collect { (Math.random() * 1000).intValue() }
- println input
- def getKey(Integer i, Integer w) {
- return Arrays.asList(i, w)
- }
- Integer m(Integer i, Integer w, List<Integer> input, Map<List<Integer>, Integer> memo) {
- def key = Arrays.asList(i, w)
- if (memo.containsKey(key)) {
- return memo.get(key)
- }
- if (i == -1) {
- return 0
- }
- if (input.get(i) > w) {
- def value = m(i - 1, w, input, memo)
- memo.put(key, value)
- return value
- }
- def value = Math.max(
- m(i - 1, w, input, memo),
- m(i - 1, w - input.get(i), input, memo) + input.get(i)
- )
- memo.put(key, value)
- return value
- }
- def ans(Integer i, Integer w, List<Integer> input, Map<List<Integer>, Integer> memo, List<Integer> output) {
- if (i == -1) {
- return
- }
- if (memo.get(getKey(i, w)) == memo.get(getKey(i - 1, w))) {
- ans(i - 1, w, input, memo, output)
- } else {
- output.push(input.get(i))
- ans(i - 1, w - input.get(i), input, memo, output)
- }
- }
- def average = input.stream().collect(Collectors.averagingInt { it }).intValue()
- def ref = (average * N * 3 / 4).intValue()
- println average
- println "ref: $ref"
- println m(input.size() - 1, ref, input, memo)
- def result = new ArrayList<Integer>()
- ans(input.size() - 1, ref, input, memo, result)
- println "anwser: $result"
- @Canonical
- class Kd {
- Long id
- Double amount
- HashMap<Oo, Double> pledges
- }
- @Canonical
- class Oo {
- Long id
- Double amount
- List<Kd> loans
- }
- def iter(List<Kd> kdList) {
- /* 1. Calculate koef */
- Kd kd = kdList.stream().min(
- Comparator.comparing { Kd kd -> kd.pledges.keySet().stream().mapToDouble { a -> a.amount }.sum() }
- ).get()
- /* 2. Select min oo */
- Oo oo = kd.pledges.keySet().stream()
- .min { Comparator.comparing { Oo oo -> 5 } }
- .get()
- /* 3. Push oo to kd */
- if (kd.amount <= oo.amount) {
- kd.pledges.put(oo, kd.amount / oo.amount)
- oo.amount -= kd.amount
- kd.amount = 0
- } else {
- kd.pledges.put(oo, (kd.amount - oo.amount) / oo.amount)
- kd.amount -= oo.amount
- oo.amount = 0
- }
- return kdList.stream().filter { it -> it.amount == 0 }.collect ( Collectors.toList() )
- }
- /* 4. Iterate while kd is not empty */
- def calc(List<Kd> kdList) {
- while ( !kdList.isEmpty() ) {
- kdList = iter(kdList)
- }
- }
- def oo = new Oo(1, ref.doubleValue(), null)
- def map = new HashMap<Oo, Double>()
- map.put(oo, Double.valueOf(0))
- def input2 = input.stream().map { it -> new Kd(1, it.doubleValue(), map) }.collect(Collectors.toList())
- //oo.setLoans(input2 as List<Kd>)
- println "oo: $oo"
- iter(input2)
- 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