SWEA1208 Flatten



効率的に計算する方法は容易ではないかもしれませんが、問題で与えられた実行時間は20秒なので、簡単にMAX、MINを探して実現すればいいのです.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

class Solution {
	static BufferedReader br;
	static BufferedWriter bw;
	static StringTokenizer st;
	static int[] Box;
	static int numofBox;

	static void dump() {
		int[] m_idx = search_MaxnMin();
		Box[m_idx[0]]--;
		Box[m_idx[1]]++;
	}

	static int[] search_MaxnMin() {
		int max = Box[0], min = Box[0];
		int max_idx = 0, min_idx = 0;
		for (int i = 1; i < numofBox; i++) {
			if (max < Box[i]) {
				max_idx = i;
				max = Box[i];
			}
			if (min > Box[i]) {
				min_idx = i;
				min = Box[i];
		}}
		int[] r = { max_idx, min_idx };
		return r;
	}

	public static void main(String args[]) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int T = 10;
		for (int tc = 1; tc <= T; tc++) {
			int N = Integer.parseInt(br.readLine());

			st = new StringTokenizer(br.readLine(), " ");
			numofBox = st.countTokens();
			Box = new int[numofBox];
			for (int i = 0; i < numofBox; i++) {
				Box[i] = Integer.parseInt(st.nextToken());
			}
			for (int i = 0; i < N; i++) {
				dump();
			}
			int[] m_idx = search_MaxnMin();
			int dif = Box[m_idx[0]] - Box[m_idx[1]];
			//System.out.println(dif);
			
			bw.write(String.format("#%d ", tc)); bw.write(String.format("%d\n", dif));
			 
		}
		bw.flush();
		bw.close();
	}
}