rakcode1998

Untitled

Dec 31st, 2016
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8. import java.io.PrintWriter;
  9. import java.util.StringTokenizer;
  10. import java.util.Arrays;
  11.  
  12. public class OptimalPoint {
  13.  
  14.     static class InputReader
  15.     {
  16.         public BufferedReader reader;
  17.         public StringTokenizer tok;
  18.        
  19.         public InputReader(InputStream inputStream)
  20.         {
  21.             reader=new BufferedReader(new InputStreamReader(inputStream));
  22.             tok = null;
  23.         }
  24.        
  25.         public InputReader(String inputFile) throws FileNotFoundException
  26.         {
  27.             reader = new BufferedReader(new FileReader(inputFile));
  28.             tok=null;      
  29.         }
  30.        
  31.         public String nextLine()
  32.         {
  33.             String c="";
  34.             try
  35.             {
  36.                 c=reader.readLine();
  37.             }
  38.             catch (IOException e){
  39.                 throw new RuntimeException(e);
  40.             }
  41.            
  42.             return c;
  43.         }
  44.        
  45.         public String next()
  46.         {
  47.             while(tok ==null || !tok.hasMoreTokens())
  48.             {
  49.                 try
  50.                 {
  51.                     tok=new StringTokenizer(nextLine());
  52.                 }
  53.                 catch(Exception e)
  54.                 {
  55.                     throw new RuntimeException(e);
  56.                 }
  57.             }
  58.            
  59.             return tok.nextToken();
  60.         }
  61.        
  62.         public int nextInt()
  63.         {
  64.             return Integer.parseInt(next());
  65.         }
  66.        
  67.         public long nextLong()
  68.         {
  69.             return Long.parseLong(next());
  70.         }
  71.     }
  72.    
  73.     public static void main(String args[])
  74.     {
  75.         InputStream inputstream=System.in;
  76.         OutputStream outputstream=System.out;
  77.         InputReader in=new InputReader(inputstream);
  78.         PrintWriter out=new PrintWriter (outputstream);
  79.         Task solver=new Task();
  80.         solver.solve(in,out);
  81.         out.flush();
  82.     }
  83.    
  84.     static class Task
  85.     {
  86.         public void solve(InputReader in,PrintWriter out)
  87.         {
  88.             int n=in.nextInt();
  89.             long a[]=new long [n];
  90.            
  91.             for (int i=0 ;i<n ;i++)
  92.             {
  93.                 a[i]=in.nextLong();
  94.             }
  95.            
  96.             Arrays.sort(a);
  97.            
  98.             if (n%2!=0)
  99.             {
  100.                 out.println(a[n/2]);
  101.             }
  102.             else
  103.             {
  104.                 long suml=0,sumr=0;
  105.                 for (int i=0 ;i<n ;i++)
  106.                 {
  107.                     suml+=Math.abs(a[i]-a[n/2-1]);
  108.                     sumr+=Math.abs(a[i]-a[n/2]);
  109.                 }
  110.                
  111.                 if(suml<=sumr)
  112.                 {
  113.                     out.println(a[n/2-1]);
  114.                 }
  115.                 else
  116.                 {
  117.                     out.println(a[n/2]);
  118.                 }
  119.             }
  120.         }
  121.     }
  122. }
Add Comment
Please, Sign In to add comment