Advertisement
mitkonikov

Evenly spliting a sequence

Jan 30th, 2023
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. class SLLNode {
  6.     public int val;
  7.     public SLLNode succ = null;
  8.  
  9.     SLLNode(int x) {
  10.         val = x;
  11.     }
  12. }
  13.  
  14. class SLL {
  15.     SLLNode first = null;
  16.     SLLNode last = null;
  17.  
  18.     SLLNode getFirst() {
  19.         return first;
  20.     }
  21.  
  22.     SLLNode getLast() {
  23.         return last;
  24.     }
  25.  
  26.     boolean isEmpty() {
  27.         return (first == null && last == null);
  28.     }
  29.  
  30.     void addLast(int x) {
  31.         SLLNode new_last = new SLLNode(x);
  32.         if (isEmpty()) {
  33.             first = new_last;
  34.             last = new_last;
  35.             return;
  36.         }
  37.         last.succ = new_last;
  38.         last = new_last;
  39.     }
  40.  
  41.     void print() {
  42.         SLLNode current = first;
  43.         while (current != null) {
  44.             System.out.print(current.val + " ");
  45.             current = current.succ;
  46.         }
  47.     }
  48. }
  49.  
  50. public class Decading {
  51.    
  52.     public static ArrayList<SLL> split(SLL list, int N) {
  53.         ArrayList<SLL> result = new ArrayList<SLL>();
  54.  
  55.         int every = N / 10;
  56.         int first_exceptions = N % 10;
  57.  
  58.         SLLNode current = list.getFirst();
  59.         for (int i = 0; i < 10; i++) { // for each of the 10 result lists
  60.             result.add(new SLL()); // I add an empty list
  61.             // 23 -> every = 2, first_excep = 3
  62.             // 0 1 2 3 4 5 6 7 8
  63.             // x x x . . . . . .
  64.             // x x x . . . . . .
  65.             // fill the i-th list
  66.             for (int j = 0; j < every + (i < first_exceptions ? 1 : 0); j++) {
  67.                 result.get(i).addLast(current.val);
  68.                 current = current.succ;
  69.             }
  70.         }
  71.  
  72.         return result;
  73.     }
  74.    
  75.     public static void main(String[] args) {
  76.         Scanner scanner = new Scanner(System.in);
  77.         int N = Integer.parseInt(scanner.nextLine());
  78.         SLL list = new SLL();
  79.         String[] elements = scanner.nextLine().split(" ");
  80.         for (int i = 0; i < N; i++) {
  81.             int x = Integer.parseInt(elements[i]);
  82.             list.addLast(x);
  83.         }
  84.  
  85.         ArrayList<SLL> splitted = split(list, N);
  86.         for (int i = 0; i < splitted.size(); i++) {
  87.             System.out.print("[");
  88.             splitted.get(i).print();
  89.             System.out.println("]");
  90.         }
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement