ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1656. Far Away Kingdom's Army

Hint on Java
Posted by the-little-worker 22 Jul 2013 18:03
import java.util.*;
import java.io.*;

public class p1656
{
    public static int reverse(int k, int N)
    {
        return N - k - 1;
    }

    public static point fun(int i, int j, int N, int function)
    {
        point p = new point();
        if (function == 1)
        {
            if (i != j)
            {
                p.i = reverse(j, N);
                p.j = reverse(i, N);
            } else
            {
                p.i = reverse(i, N);
                p.j = j;
            }
        } else if (function == 2)
        {
            if (i != j)
            {
                p.i = j;
                p.j = i;
            } else
            {
                p.i = i;
                p.j = reverse(j, N);
            }
        } else if (function == 3)
        {
            if (i != j)
            {
                p.i = reverse(i, N);
                p.j = reverse(j, N);
            } else
            {
                p.i = reverse(i, N);
                p.j = reverse(j, N);
            }
        }
        return p;
    }
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        int N = in.nextInt();

        int arr[] = new int[N * N];
        //{198, 186, 181, 190, 194, 194, 174, 188, 171, 198, 182, 171, 175, 184, 177, 200, 189, 185, 177, 171, 172, 180, 186, 188, 197};
        int[] out = new int[N * N];

        for (int i = 0; i < N * N; i++)
        {
            arr[i] = in.nextInt();
        }

        Arrays.sort(arr);

        /*for (int i = 0; i < N * N; i++)
            System.out.print(arr[i] + " ");
        System.out.println()*/;

        int po = 0;

        for (int j = 0; j < N / 2 ; j++)
        {
            for (int i = j; i < N / 2 + 1; i++)
            {
                // System.out.println("i = " + i + " j = " + j + " po = " + po);

                out[N * i + j] = arr[po++];
                point p = fun(i, j, N, 1);

                //System.out.println("i = " + p.i + " j = " + p.j + " po = " + po);

                out[N * p.i + p.j] = arr[po++];
                p = fun(i, j, N, 2);

                //System.out.println("i = " + p.i + " j = " + p.j + " po = " + po);

                out[N * p.i + p.j] = arr[po++];
                p = fun(i, j, N, 3);

                 //System.out.println("i = " + p.i + " j = " + p.j + " po = " + po);

                out[N * p.i + p.j] = arr[po++];

                if (i != j && i != N / 2 )
                {
                    int savei = i;
                    i = reverse(i, N);


                    // System.out.println("i = " + i + " j = " + j + " po = " + po);

                    out[N * i + j] = arr[po++];
                    p = fun(i, j, N, 1);

                     //System.out.println("i = " + p.i + " j = " + p.j + " po = " + po);

                    out[N * p.i + p.j] = arr[po++];
                    p = fun(i, j, N, 2);

                    // System.out.println("i = " + p.i + " j = " + p.j + " po = " + po);

                    out[N * p.i + p.j] = arr[po++];
                    p = fun(i, j, N, 3);

                    // System.out.println("i = " + p.i + " j = " + p.j + " po = " + po);

                    out[N * p.i + p.j] = arr[po++];

                    i = savei;
                }

            }
        }

        out[N / 2  * N + N / 2] = arr[po++];

        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
                System.out.format("%4d", out[i * N + j]);
            System.out.println();
        }
    }
}

class point
{
    int i, j;
}