Advertisement
SansPapyrus683

TLE Robot Instructions

Mar 6th, 2022
1,197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. package official.o2022.feb.silver;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public final class Instructions {
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
  9.         int instructionNum = Integer.parseInt(read.readLine());
  10.  
  11.         StringTokenizer ptSt = new StringTokenizer(read.readLine());
  12.         Point target = new Point(Integer.parseInt(ptSt.nextToken()), Integer.parseInt(ptSt.nextToken()));
  13.  
  14.         Point[] half1 = new Point[instructionNum / 2];
  15.         Point[] half2 = new Point[instructionNum - half1.length];
  16.         for (int i = 0; i < instructionNum; i++) {
  17.             ptSt = new StringTokenizer(read.readLine());
  18.             Point pt = new Point(Integer.parseInt(ptSt.nextToken()), Integer.parseInt(ptSt.nextToken()));
  19.             if (i < half1.length) {
  20.                 half1[i] = pt;
  21.             } else {
  22.                 half2[i - half1.length] = pt;
  23.             }
  24.         }
  25.  
  26.         HashMap<Point, Integer>[] half1Sub = subsetSums(half1);
  27.         HashMap<Point, Integer>[] half2Sub = subsetSums(half2);
  28.         int[] validSubsets = new int[instructionNum + 1];
  29.         for (int i = 0; i <= half1.length; i++) {
  30.             for (Point p1 : half1Sub[i].keySet()) {
  31.                 Point p2 = target.add(new Point(-p1.x, -p1.y));
  32.                 for (int j = 0; j <= half2.length; j++) {
  33.                     validSubsets[i + j] += half1Sub[i].get(p1) * half2Sub[j].getOrDefault(p2, 0);
  34.                 }
  35.             }
  36.         }
  37.  
  38.         for (int i = 1; i <= instructionNum; i++) {
  39.             System.out.println(validSubsets[i]);
  40.         }
  41.     }
  42.  
  43.     private static int popCount(int n) {
  44.         int total = 0;
  45.         for (int i = 0; i < 31; i++) {
  46.             total += (n & (1 << i)) != 0 ? 1 : 0;
  47.         }
  48.         return total;
  49.     }
  50.  
  51.     private static HashMap<Point, Integer>[] subsetSums(Point[] pts) {
  52.         HashMap<Point, Integer>[] ret = new HashMap[pts.length + 1];
  53.         for (int i = 0; i <= pts.length; i++) {
  54.             ret[i] = new HashMap<>();
  55.         }
  56.         for (int i = 0; i < (1 << pts.length); i++) {
  57.             Point result = new Point(0, 0);
  58.             for (int p = 0; p < pts.length; p++) {
  59.                 if ((i & (1 << p)) != 0) {
  60.                     result = result.add(pts[p]);
  61.                 }
  62.             }
  63.             HashMap<Point, Integer> relevant = ret[popCount(i)];
  64.             relevant.put(result, relevant.getOrDefault(result, 0) + 1);
  65.         }
  66.         return ret;
  67.     }
  68. }
  69.  
  70. class Point {
  71.     public int x;
  72.     public int y;
  73.     public Point(int x, int y) {
  74.         this.x = x;
  75.         this.y = y;
  76.     }
  77.  
  78.     public Point add(Point p) {
  79.         return new Point(x + p.x, y + p.y);
  80.     }
  81.  
  82.     @Override
  83.     public String toString() {
  84.         return String.format("(%d, %d)", x, y);
  85.     }
  86.  
  87.     @Override
  88.     public int hashCode() {
  89.         return Objects.hash(x, y);
  90.     }
  91.  
  92.     @Override
  93.     public boolean equals(Object obj) {
  94.         return obj.getClass() == getClass() && x == ((Point) obj).x && y == ((Point) obj).y;
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement