qwerty787788

Array rotating magic

Oct 5th, 2012
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. /**
  5.  * @author Boris Minaev, e-mail: [email protected]
  6.  */
  7.  
  8. public class Testing {
  9.     FastScanner in;
  10.     PrintWriter out;
  11.  
  12.     // that's brute implementation of changing X & Y of an array
  13.     int[][] simpleMethod(int[][] a) {
  14.         int n = a.length;
  15.         int m = a[0].length;
  16.         int[][] b = new int[m][n];
  17.         for (int i = 0; i < n; i++)
  18.             for (int j = 0; j < m; j++)
  19.                 b[j][i] = a[i][j];
  20.         return b;
  21.     }
  22.  
  23.     // magic constant, isn't it?
  24.     // don't change it!
  25.     static final int MAGIC_CONST = 150;
  26.  
  27.     // same function as 'simpleMethod', but there is something strange
  28.     // some magic here, don't look!
  29.     int[][] magicFunction(int[][] a) {
  30.         int n = a.length;
  31.         int m = a[0].length;
  32.         int w = (n - 1) / MAGIC_CONST + 1;
  33.         int h = (m - 1) / MAGIC_CONST + 1;
  34.         int[][][] tmp = new int[w][h][MAGIC_CONST * MAGIC_CONST];
  35.         for (int i1 = 0; i1 < w; i1++)
  36.             for (int j1 = 0; j1 < h; j1++) {
  37.                 int toK = Math.min(MAGIC_CONST, m - j1 * MAGIC_CONST);
  38.                 int toL = Math.min(MAGIC_CONST, n - i1 * MAGIC_CONST);
  39.                 for (int k1 = 0; k1 < toK; k1++) {
  40.                     for (int l1 = 0; l1 < toL; l1++) {
  41.                         tmp[i1][j1][k1 * MAGIC_CONST + l1] = a[i1 * MAGIC_CONST
  42.                                 + l1][j1 * MAGIC_CONST + k1];
  43.                     }
  44.                 }
  45.             }
  46.         int[][] b = new int[m][n];
  47.         for (int i1 = 0; i1 < w; i1++)
  48.             for (int j1 = 0; j1 < h; j1++) {
  49.                 int toK = Math.min(MAGIC_CONST, m - j1 * MAGIC_CONST);
  50.                 int toL = Math.min(MAGIC_CONST, n - i1 * MAGIC_CONST);
  51.                 for (int k1 = 0; k1 < toK; k1++) {
  52.                     for (int l1 = 0; l1 < toL; l1++) {
  53.                         b[j1 * MAGIC_CONST + k1][i1 * MAGIC_CONST + l1] = tmp[i1][j1][k1
  54.                                 * MAGIC_CONST + l1];
  55.                     }
  56.                 }
  57.             }
  58.         return b;
  59.     }
  60.  
  61.     void solve() {
  62.         // size of array
  63.         int n = 5678;
  64.         int m = 6574;
  65.         int[][] a = new int[n][m];
  66.        
  67.         //fill the array with a random data
  68.         Random rnd = new Random();
  69.         for (int i = 0; i < n; i++)
  70.             for (int j = 0; j < m; j++)
  71.                 a[i][j] = rnd.nextInt();
  72.  
  73.         long timeStart = System.nanoTime();
  74.         int[][] b = simpleMethod(a);
  75.         long totalTime = System.nanoTime() - timeStart;
  76.         out.println("Simple implementation: " + totalTime / 1000000.0 + " ms");
  77.  
  78.         timeStart = System.nanoTime();
  79.         int[][] b2 = magicFunction(a);
  80.         totalTime = System.nanoTime() - timeStart;
  81.         out.println("Magic implementation: " + totalTime / 1000000.0 + " ms");
  82.  
  83.         // check that two results are same
  84.         for (int i = 0; i < m; i++)
  85.             for (int j = 0; j < n; j++)
  86.                 if (b[i][j] != b2[i][j]) {
  87.                     System.err.print("FAIL is here!");
  88.                     return;
  89.                 }
  90.     }
  91.  
  92.     void run() {
  93.         try {
  94.             in = new FastScanner(new File("tesing.in"));
  95.             out = new PrintWriter(new File("tesing.out"));
  96.  
  97.             solve();
  98.  
  99.             out.close();
  100.         } catch (FileNotFoundException e) {
  101.             e.printStackTrace();
  102.         }
  103.     }
  104.  
  105.     void runIO() {
  106.  
  107.         in = new FastScanner(System.in);
  108.         out = new PrintWriter(System.out);
  109.  
  110.         solve();
  111.  
  112.         out.close();
  113.     }
  114.  
  115.     class FastScanner {
  116.         BufferedReader br;
  117.         StringTokenizer st;
  118.  
  119.         public FastScanner(File f) {
  120.             try {
  121.                 br = new BufferedReader(new FileReader(f));
  122.             } catch (FileNotFoundException e) {
  123.                 e.printStackTrace();
  124.             }
  125.         }
  126.  
  127.         public FastScanner(InputStream f) {
  128.             br = new BufferedReader(new InputStreamReader(f));
  129.         }
  130.  
  131.         String next() {
  132.             while (st == null || !st.hasMoreTokens()) {
  133.                 String s = null;
  134.                 try {
  135.                     s = br.readLine();
  136.                 } catch (IOException e) {
  137.                     e.printStackTrace();
  138.                 }
  139.                 if (s == null)
  140.                     return null;
  141.                 st = new StringTokenizer(s);
  142.             }
  143.             return st.nextToken();
  144.         }
  145.  
  146.         boolean hasMoreTokens() {
  147.             while (st == null || !st.hasMoreTokens()) {
  148.                 String s = null;
  149.                 try {
  150.                     s = br.readLine();
  151.                 } catch (IOException e) {
  152.                     e.printStackTrace();
  153.                 }
  154.                 if (s == null)
  155.                     return false;
  156.                 st = new StringTokenizer(s);
  157.             }
  158.             return true;
  159.         }
  160.  
  161.         int nextInt() {
  162.             return Integer.parseInt(next());
  163.         }
  164.  
  165.         long nextLong() {
  166.             return Long.parseLong(next());
  167.         }
  168.     }
  169.  
  170.     public static void main(String[] args) {
  171.         new Testing().runIO();
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment