0_0_21572948_4735\Main.java:1: 错误: 解析时已到达文件结尾
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.Comparator; import java.util.Random; import java.util.StringTokenizer; class MSort<T> { private static final int MIN_LENGTH = 4; private Comparator<T> comparator; private T[] assist_array; public MSort(Comparator<T> comparator, Class<T> classType, int siz) { this.comparator = comparator; assist_array = (T[]) Array.newInstance(classType, siz); } public MSort(Comparator<T> comparator, T[] assist_array) { this.comparator = comparator; this.assist_array = assist_array; } public void mergeOrderedArray(T[] A, T[] B, T[] C, int ABegin, int AEnd, int BBegin, int BEnd, int CBegin) { int i = ABegin, j = BBegin, k = CBegin; while( i < AEnd && j < BEnd ) { if( comparator.compare(A[i], B[j]) < 0 ) C[k++] = A[i++]; else C[k++] = B[j++]; } while( i < AEnd ) C[k++] = A[i++]; while( j < BEnd ) C[k++] = B[j++]; } private void _sort_assist(T[] targets, int begin, int end) { for(int i=begin+1; i < end; ++i) { T target = targets[i]; int j = i-1; for (; j >= begin; --j) { if( comparator.compare(targets[j], target) <= 0 ) break; targets[j+1] = targets[j]; } targets[j+1] = target; } } private void _sort(T[] targets, int begin, int end) { int length = end - begin; if( length <= MIN_LENGTH ) { _sort_assist(targets, begin, end); return; } int mid = begin + (length>>1); _sort(targets, begin, mid); _sort(targets, mid, end); mergeOrderedArray(targets, targets, assist_array, begin, mid, mid, end, begin); System.arraycopy(assist_array, begin, targets, begin, end-begin); } public void sort(T[] targets, int offset, int length) { int end = offset + length; assert offset >= 0 && offset < targets.length && end < targets.length : "Out of bound of array"; _sort(targets, offset, end); } } class QSort<T> { private static final int MIN_LENGTH = 4; private Random random = new Random(); private Comparator<T> comparator; public QSort(Comparator<T> comparator) { this.comparator = comparator; } private void _sort_assist(T[] targets, int begin, int end) { for(int i=begin+1; i < end; ++i) { T target = targets[i]; int j = i-1; for (; j >= begin; --j) { if( comparator.compare(targets[j], target) <= 0 ) break; targets[j+1] = targets[j]; } targets[j+1] = target; } } private void _sort(T[] targets, int begin, int end) { if( begin+MIN_LENGTH >= end ) { _sort_assist(targets, begin, end); return; } int lft = begin, rht = end-1; int mid = lft + random.nextInt(end-begin); T target = targets[mid]; while( lft <= rht ) { while( comparator.compare(targets[lft], target) < 0 ) ++lft; while( comparator.compare(targets[rht], target) > 0 ) --rht; if( lft <= rht ) { T tmp = targets[lft]; targets[lft] = targets[rht]; targets[rht] = tmp; ++lft; --rht; } } ++rht; if( begin+1 < rht ) _sort(targets, begin, rht); if( lft+1 < end ) _sort(targets, lft, end); } public void sort(T[] targets, int offset, int length) { int end = offset + length; assert offset >= 0 && offset < targets.length && end < targets.length : "Out of bound of array"; _sort(targets, offset, end); } } class Scanner { private BufferedReader reader; private StringTokenizer tokenizer; public Scanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in), 32768); tokenizer = null; } public Scanner(InputStream in, int size) { reader = new BufferedReader(new InputStreamReader(in), size); tokenizer = null; } public boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { return false; } } return true; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } } public class Main { private static final Scanner in = new Scanner(System.in); private static final int MAXN = 1000000 + 10; private static final Integer[] targets = new Integer[MAXN]; // private static final QSort<Integer> sort = new QSort(new Comparator<Integer>() { // @Override // public int compare(Integer o1, Integer o2) { // return o2 - o1; // } // }); // private static final MSort<Integer> sort = new MSort(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }, Integer.class, MAXN); public static void main(String[] args) { while( in.hasNext() ) { int N = in.nextInt(); int M = in.nextInt(); for (int i = 0; i < N; ++i) targets[i] = in.nextInt(); sort.sort(targets, 0, N); System.out.print(targets[0]); for (int i = 1; i < M; ++i) System.out.print(" " + targets[i]); System.out.println(); } } }
^
1 个错误
|