Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 22nd, 2010 | Syntax: None | Size: 3.21 KB | Hits: 75 | Expires: Never
Copy text to clipboard
  1. import java.io.*;
  2. import java.lang.*;
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.        class triple {
  8.                int a,b,c;
  9.  
  10.                public triple(int x, int y, int z) {
  11.                        a = x;
  12.                        b = y;
  13.                        c = z;
  14.                }
  15.        }
  16.  
  17.        public int[] mat;
  18.        public int[] cum;
  19.        HashMap< triple, Boolean > mapped;
  20.        int n, sum, max;
  21.  
  22.        public Main( int len) {
  23.                 n = len;
  24.                 mat = new int[ n ];
  25.                 cum = new int[ n ];
  26.                 mapped = new HashMap< triple, Boolean >();
  27.        }
  28.  
  29.        public void init() {
  30.                 sum = 0;
  31.                 for( int i = 0; i < n; i++) {
  32.                          sum += mat[ i ];
  33.                 }
  34.                 max = sum/2;
  35.                 Arrays.sort( mat);
  36.                 Arrays.fill( cum, 0);
  37.                 cum[ 0 ] = mat[ 0 ];
  38.                 for( int i = 1; i < n; i++) {
  39.                          cum[ i ] = mat[ i ] + cum[ i - 1 ];
  40.                 }
  41.        }
  42.  
  43.        public boolean exists( int i, int j, int k) {
  44.                triple temp = new triple( i, j, k);
  45.                if( mapped.containsKey( temp))
  46.                        return mapped.get( temp);
  47.  
  48.                if( ( j == 0) && ( k == 0)) {
  49.                        mapped.put( temp, true);
  50.                        return true;
  51.                }
  52.  
  53.                if( i == 0) {
  54.                        mapped.put( temp, false);
  55.                        return false;
  56.                }
  57.  
  58.                if( cum [ i - 1 ] < k) {
  59.                        mapped.put( temp, false);
  60.                        return false;
  61.                }
  62.  
  63.                boolean ans;
  64.                ans = exists( i - 1, j - 1, k-mat[ i - 1 ]);
  65.                if( ans) {
  66.                        mapped.put( temp, true);
  67.                        return true;
  68.                }
  69.                ans = exists( i - 1, j, k);
  70.                if( ans) {
  71.                        mapped.put( temp, true);
  72.                        return true;
  73.                }
  74.                mapped.put( temp, false);
  75.                return false;
  76.        }
  77.  
  78.        public void showdown() {
  79.                for( int i = max; i > 0; i--) {
  80.                        if( exists( n, ( int)Math.ceil( (double)n/2), i)) {
  81.                                System.out.println( i + " " + ( sum - i));
  82.                                break;
  83.                        }
  84.                }
  85.        }
  86.  
  87.        public static void main( String[] args) throws IOException {
  88.                 BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
  89.                 int tcase = Integer.parseInt( br.readLine());
  90.                 for( int i = 0; i < tcase; i++) {
  91.                          br.readLine();
  92.                          int n = Integer.parseInt( br.readLine());
  93.                          Main temp = new Main( n);
  94.                          for( int j = 0; j<n; j++)
  95.                                   temp.mat[ j ] = Integer.parseInt( br.readLine());
  96.                          temp.init();
  97.                          temp.showdown();
  98.                          if( i < ( tcase - 1))
  99.                                System.out.println();
  100.                 }
  101.        }
  102. }