#114159 - keldon - Thu Jan 04, 2007 7:13 pm
The problem of the domain was dealing with managing the falling of blocks in Tetris. If you clear a line, then all of the above lines will have to fall down. However if you extend this to a bigger domain you can consider a single array where you are removing a number of elements from it and you want to output the offsets that the new array will have. For example if you have a 10 element array and you are removing the 7th element (starting from 0) then elements 7,8, and 9 will be offset by 1
Input begins with the size of the array followed by the number of elements to be removed; the elements to be removed are given on the next line seperated by a space. So for this example you have:
// sample input
10 1
7
// sample output
0 0 0 0 0 0 0 1 1 1
Here is another example where elements 0,1,3, 5 and 8 are removed
// sample input
20 5
0 1 3 5 8
// sample output
2 3 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
To make things more complicated the array size can be 2000000, the list of numbers to remove are not ordered, and this is a time critical task; so all solutions are limited to 10 seconds of execution!!! Leway is allowed for slower processors; just as a guideline to the complexity of the problem you.
Here is the generator code so that you could try out your own data. I would advise all those who are not sure of their solutions speed to change the Size, and also note that the lower the ration of Size:Removed the longer this takes to generate problems.
Note that I have created a problem creator/solver at http://thedrugzone.co.uk/LaMightySolution.jar , copy it to your desktop as it creates its output files in the same directory!
Input begins with the size of the array followed by the number of elements to be removed; the elements to be removed are given on the next line seperated by a space. So for this example you have:
// sample input
10 1
7
// sample output
0 0 0 0 0 0 0 1 1 1
Here is another example where elements 0,1,3, 5 and 8 are removed
// sample input
20 5
0 1 3 5 8
// sample output
2 3 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
To make things more complicated the array size can be 2000000, the list of numbers to remove are not ordered, and this is a time critical task; so all solutions are limited to 10 seconds of execution!!! Leway is allowed for slower processors; just as a guideline to the complexity of the problem you.
Here is the generator code so that you could try out your own data. I would advise all those who are not sure of their solutions speed to change the Size, and also note that the lower the ration of Size:Removed the longer this takes to generate problems.
Note that I have created a problem creator/solver at http://thedrugzone.co.uk/LaMightySolution.jar , copy it to your desktop as it creates its output files in the same directory!
Code: |
// Generator.java
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; public class Generator { /** number of elements removed from array */ public static final int Removed = 5000000; /** size of array */ public static final int Size = 10000000; public static void main ( String argv [] ) throws FileNotFoundException { if ( Size < Removed ) { System.out.println ("Recompile with Size > Removed"); } boolean [] b = new boolean [Size]; int numbers[] = new int[Removed]; System.out.println("Generating problem!"); for ( int i = 0; i < Size; i ++ ) b[i]=true; int total = 0; while ( total < Removed){ int rand = (int)(Math.random() * (double)Size); rand = (rand==Size)?rand-1 : rand; if ( b[rand] ){ b[rand] = false; numbers[total++] = rand; } } PrintWriter out = new PrintWriter(new FileOutputStream ("problem.txt")); out.println(Size + " " + Removed); for ( int i = 0; i < Removed; i ++ ) { out.print(numbers[i] + (i==Removed-1?"":" ")); if ( i%1000 == 0 ) out.flush(); } out.close(); System.out.println("Problem saved as problem.txt"); } } |