結果

問題 No.275 中央値を求めよ
ユーザー shinwisteriashinwisteria
提出日時 2017-03-18 19:23:09
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,278 bytes
コンパイル時間 3,282 ms
コンパイル使用メモリ 74,136 KB
実行使用メモリ 56,156 KB
最終ジャッジ日時 2023-09-18 02:33:11
合計ジャッジ時間 6,359 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,268 KB
testcase_01 AC 124 ms
56,156 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Median {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		int[] A = new int[N];
		for(int i = 0;i < N ;i++){
			A[i] = s.nextInt();
		}
		s.close();
		quicksort Q = new quicksort();
		A = Q.quick(A);

		double med = 0;
		if(N % 2 == 1){
			med = (double)A[(N-1)/2];
		}else{
			med = (double)(A[N/2-1] + A[N/2]) /2;
		}
		System.out.println(med);
	}

}

class quicksort {
	public int[] quick(int[] d){
		int med = (d[0] + d[d.length-1]) / 2;
		int[] mm = new int[2];
		mm = maxmin(d);
		if(mm[0] == mm[1]){
		}else{
			int count = 0;
			for(int i = 0;i < d.length;i++){
				if(d[i] <= med){
					count++;
				}
			}
			int[] d1 = new int[count];
			int[] d2 = new int[d.length - count];
			int d1count = 0,d2count = 0;
			for(int i = 0;i < d.length;i++){
				if(d[i] <= med){
					d1[d1count++] = d[i];
				}else{
					d2[d2count++] = d[i];
				}
			}
			d1 = quick(d1);
			d2 = quick(d2);
			int k = 0;
			for(int n : d1){
				d[k++] = n;
			}
			for(int n : d2){
				d[k++] = n;
			}
		}
		return d;
	}
	
	static int[] maxmin(int[] d){
		int[] mm = {d[0] ,d[0]};
		for(int n : d){
			if(n < mm[0]){
				mm[0] = n;
			}else if(n > mm[1]){
				mm[1] = n;
			}
		}
		return mm;
	}
}
0