본문

151222A(화)

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

 public static void main(String[] args) {
  int T = 0;
  int N;
  int maxMen, maxWom;
  int result;
  
  Scanner tc = new Scanner(System.in);
  
  T = tc.nextInt();

  ArrayList<Integer> intMen = new ArrayList<Integer>();
  ArrayList<Integer> intWom = new ArrayList<Integer>();

  for(int j = 0; j < T; j++) {
   result = 0;
   
   N = tc.nextInt();

   intMen = inputInteger(intMen, N);
   intWom = inputInteger(intWom, N);

   quickSort(intMen, 0, intMen.size() - 1);
   quickSort(intWom, 0, intWom.size() - 1);
   
   for(int k = 0; k < N; k++) {
    result = result + Math.abs(intMen.get(k) - intWom.get(k));
   }
   
   System.out.println(result);
  }
 }

 public static void quickSort(ArrayList<Integer> a, int leftmost, int rightmost) {
  if(rightmost - leftmost <= 0) {
   return;
  }
  else {
   int pivot = a.get(rightmost);
   
   int i = leftmost - 1;
   int j = rightmost;
   
   while(true) {
    while(a.get(++i) < pivot);
    
    while(j > leftmost && a.get(--j) > pivot);
    
    if(i >= j)
     break;
    else
     swap(a, i, j);
   }
   swap(a, i, rightmost);
   
   quickSort(a, leftmost, i - 1);
   
   quickSort(a, leftmost + 1, rightmost);
  }
 }
 
 public static void swap(ArrayList<Integer> a, int i, int j) {
  int temp;
  temp = a.get(i);
  a.set(i, a.get(j));
  a.set(j, temp);
 }
 
 public static ArrayList<Integer> inputInteger(ArrayList<Integer> inputArr, int memNum) {
  Scanner tci = new Scanner(System.in);
  
  for(int k = 0; k < memNum; k++) {
   inputArr.add(tci.nextInt());
  }

  return inputArr;
 }
}

 


 

퀵정렬을 사용한 MEETING 문제이다.

그래도 시간초과가 난다.

혼란스럽다.

일단은 질문답변 게시판에 질문을 올려놓았다.

'Architecture > ACM-ICPC' 카테고리의 다른 글

160106A(수)  (0) 2016.01.07
160103A(일)  (0) 2016.01.03
160103A(일)  (0) 2016.01.03
151226A(토)  (0) 2015.12.26
151221A(월)  (0) 2015.12.21

공유

댓글