結果

問題 No.275 中央値を求めよ
ユーザー ueyus
提出日時 2015-09-09 22:08:17
言語 Java
(openjdk 23)
結果
RE  
実行時間 -
コード長 1,167 bytes
コンパイル時間 2,210 ms
コンパイル使用メモリ 77,400 KB
実行使用メモリ 56,320 KB
最終ジャッジ日時 2024-07-19 05:05:48
合計ジャッジ時間 8,680 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other RE * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		String[] inp = sc.nextLine().split(" ");
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(inp[i]);
		}
		Marge mm = new Marge(n);
		mm.sort(a, 0, n);
		if (n % 2 == 0) {
			if ((a[n / 2 - 1] + a[n / 2]) % 2d > 0) {
				System.out.println((a[n / 2 - 1] + a[n / 2]) / 2d);
			} else {
				System.out.println((a[n / 2 - 1] + a[n / 2]) / 2);
			}
		} else {
			System.out.println(a[n / 2]);
		}


	}

}
class Marge {
	int buf[];
	Marge(int n) {
		buf = new int[n];
	}
	void sort(int[] a, int s, int e) {
		if (1 >= e - s) return;
		int mid = s + (e - s) / 2;
		sort(a, s, mid);
		sort(a, mid, e);
		marge(a, s, mid, e);
	}
	void marge(int[] a, int s, int mid, int e) {
		int i = e - s;
		int work[] = new int[i];
		int p = 0, m = mid, k = s;
		while (s < mid && m < e) {
			if (a[s] <= a[m]) {
				work[p++] = a[s++];
			} else {
				work[p++] = a[m++];
			}
		}
		while (s < mid) work[p++] = a[s++];
		while (m < e) work[p++] = a[m++];
		System.arraycopy(work, 0, a, k, i);
	}
}
0