Dividing a list of values ​​into three equal subtotals

I have a list of numbers that make up 540000. I would like to sort this list into 3 lists, each of which is 180,000. What is the most efficient programming method for this, assuming that the list of numbers is a flat file with a number in a line?

+3
source share
5 answers

Sounds like a variation with a backpack problem . It would be useful to know the size of these numbers and calculate whether there are huge differences in size or are they all similar in scale - are there many (> 1000) or several (<100)?

- - , , , , ... . ... .

+1
for i as integer = 1 to 180000
put data in array 1
next i

for i as integer = 180001 to 360000
put data in array 2
next i

for i as integer = 360001 to 540000
put data in array 3
next i
0

NP-hardness - "" . , , , .

, [179998, 180001, 180001]:)

0

Java, .

, , , . 18000 .

, , 18000 .

, , , 18000.

, . , . , , . , ; .

import java.util.*; 

public class Listen {

   private static Set<List<Integer>> makeFrom(int total, List<Integer> numbers) {
      Set<List<Integer>> results = new HashSet<List<Integer>>();
      List<Integer> soFar = new ArrayList<Integer>();
      makeFrom(results, total, soFar, numbers, 0);
      return results;
   }

   private static void makeFrom(Set<List<Integer>> results, int total, List<Integer> soFar, List<Integer> numbers, int startingAt) {
      if (startingAt >= numbers.size()) return;
      for (int p=startingAt; p<numbers.size(); p++) {
         Integer number = numbers.get(p);
         List<Integer> newSoFar = new ArrayList<Integer>(soFar);
         newSoFar.add(number);
         int newTotal = total - number;
         if (newTotal < 0) continue;
         if (newTotal == 0) {
            Collections.sort(newSoFar);
            results.add(newSoFar);
         } else {
            List<Integer> newNumbers = new ArrayList<Integer>(numbers);
            newNumbers.remove(number);
            makeFrom(results, newTotal, newSoFar, newNumbers, startingAt + 1);
         }
      }
   }

   public static void main(String[] args) {
      List<Integer> numbers = new ArrayList<Integer>();
      for (int j=1; j<11; j++) numbers.add(j);
      for (List<Integer> result : makeFrom(25, numbers)) {
         System.out.println(Arrays.deepToString(result.toArray(new Integer[result.size()])));
      }
   }
}
0

ian-witz, , , NP- : , , . , , , , , , .

, , :

Set up a place to hold your results. The results will all be lists of numbers, each some sub-set of your original list. We don't know how many such lists will result; possibly none.

Put your list of numbers into an array so you can refer to them and access them by index. In the following, I'm assuming array indices starting at 1. Say you have 10 numbers, so you put them into a 10 element array, indexed by positions 1 through 10.

For performance reasons, it may help to sort your array in descending order. It not necessary though.

Run a first index, call it i, through this array, i.e. through index values 1 through 10. 
For each index value:
  select the number at index position i, call it n1.
  set up a new list of numbers, where we will be assembling a sub-list. call it sublist.
  add n1 to the (so far empty) sublist.
  If i is already at 10, there nothing more we can do. Otherwise,
  Run a second index, call it j, through the arrray, starting at i+1 and going up to 10.
  For each value of j:
    select the number at index position j, call it n2.
    add n2 to the sublist containing n1
    calculate the sum of our sublist so far: Does it add up to 18000? 
    If the exact total is reached, add the current sublist to our result list.
    If the total is exceeded, there nothing we can add to make it better, so skip to the next value of j.
    If the total is less than 18000, you need to pick a third number.
    Run a third index, call it k, through the array, starting at j+1 and going up to 10. Skip this if j is already at 10 and there no place to go.
    For each value of k:
      select the number at k, call it n3
      add n3 to the sublist
      check the sublist total against the expected total
      if the exact total is reached, store the sublist as a result; 
      if it less than the expected, start a 4th loop, and so on.

      When you're done with checking a value for a loop, e.g. n4, you need to take your latest n4, n3 or whatever back out of the sublist because you'll be trying a different number next.

    Whenever you find a combination of numbers with the correct sum, store it in your results set.

When you've run all your loop counters into the wall (i.e. i is 10 and there nowhere left to go), your "results" set will contain all sub-lists of the original list that added up to the desired total. It possible there will be none, in that case there no (exact) solution to your problem.

If you have 3 or more sub-lists in your results set, you can check if you can find a pair of them that use non-overlapping sets of numbers from the original list. If you have 2, then there should also be a 3rd sub-list containing exactly all the numbers not contained in the first 2 lists, and you have your solution.

; , 1 () 10 18000. , , 2000, , 2 (= + 1) 16000. ( + 1) (16000 - ), . "" - , .

How to implement this effectively depends on the language in which you make it. FORTRAN 77 does not have recursion, Lua does not implement lists or sets efficiently, Lisp may have problems with efficient indexing to a list. In Java, I could use a bit set rather than a sublist. I don’t know anything about P4GL, therefore: for implementation you are on your own!

0
source

Source: https://habr.com/ru/post/1722091/


All Articles