Skip to content

Instantly share code, notes, and snippets.

@suicide
Created February 3, 2013 19:02
Show Gist options
  • Save suicide/4703174 to your computer and use it in GitHub Desktop.
Save suicide/4703174 to your computer and use it in GitHub Desktop.
Facebook Hacker Cup 2013 Round 1 DeadPixels Java faster solution
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
/**
* @author psy
*
*/
public class DeadPixelsReverse {
/**
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<String> output = new LinkedList<String>();
DeadPixelsReverse testCase = new DeadPixelsReverse();
int games = Integer.parseInt(in.nextLine());
for (int i = 1; i <= games; i++) {
int w = in.nextInt();
int h = in.nextInt();
int p = in.nextInt();
int q = in.nextInt();
int n = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int d = in.nextInt();
int result = testCase.findPositions(w, h, p, q, n, x, y, a, b, c, d);
output.add("Case #" + i + ": " + result);
}
for (String out : output) {
System.out.println(out);
}
}
public int findPositions(int w, int h, int p, int q, int n, int x, int y, int a, int b, int c, int d) {
boolean[][] screen = new boolean[w][h];
Set<DeadPixel> deadPixels = new HashSet<DeadPixelsReverse.DeadPixel>();
DeadPixel lastPixel = new DeadPixel(x, y);
deadPixels.add(lastPixel);
for (int i = 1; i < n; i++) {
lastPixel = new DeadPixel((lastPixel.x * a + lastPixel.y * b + 1) % w,
(lastPixel.x * c + lastPixel.y * d + 1) % h);
deadPixels.add(lastPixel);
}
for (DeadPixel deadPixel : deadPixels) {
for (int i = deadPixel.x; i >= Math.max(0, deadPixel.x - p + 1); i--) {
for (int j = deadPixel.y; j >= Math.max(0, deadPixel.y - q + 1); j--) {
screen[i][j] = true;
}
}
}
// find unique positions
int uniquePositions = 0;
for (int i = 0; i < w - p + 1; i++) {
for (int j = 0; j < h - q + 1; j++) {
if (!screen[i][j]) {
uniquePositions++;
}
}
}
return uniquePositions;
}
private static class DeadPixel {
private final int x;
private final int y;
public DeadPixel(int x, int y) {
super();
this.x = x;
this.y = y;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DeadPixel other = (DeadPixel) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
}
@harishletsgo
Copy link

Faster with how much heap memory? Does not run with this

20
21995 37911 1150 1245 833 47 23529 19 74 87 95
35510 11125 1346 1407 395 35248 9485 18 91 26 9
5134 2621 141 183 1000000 2309 282 36 56 66 26
5726 4567 144 121 1000000 454 3685 99 81 46 52
39954 39461 443 17 1000000 34349 36388 60 98 69 44
196 827 117 730 108 131 477 41 72 11 66
943 715 356 144 449 893 533 86 24 28 90
20133 29990 1305 1623 603 7664 19517 81 90 36 9
4 4 2 2 1 0 2 1 2 3 4
38850 37146 6 132 1443 28733 35729 1 38 55 58
703 687 125 343 321 68 78 38 8 2 66
39527 38179 294 327 1000000 12899 20050 56 52 99 46
36478 39536 170 1 1442 22516 31242 4 70 37 6
192 223 29 170 28 25 108 89 74 40 51
6 10 3 2 2 0 0 5 4 3 2
39843 39881 20522 20114 3 11897 5279 74 89 96 58
4 4 1 1 3 1 1 2 2 2 2
35728 35276 139 460 1000000 14745 27690 33 97 8 69
39491 39608 20414 20014 3 22564 17425 7 17 95 40
4718 7077 134 115 1000000 3964 4597 7 20 29 77

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment