結果

問題 No.21 平均の差
ユーザー めうめう🎒めうめう🎒
提出日時 2016-05-08 20:50:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 742 ms / 5,000 ms
コード長 1,855 bytes
コンパイル時間 2,346 ms
コンパイル使用メモリ 77,384 KB
実行使用メモリ 42,892 KB
最終ジャッジ日時 2024-04-15 13:56:45
合計ジャッジ時間 5,436 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
41,804 KB
testcase_01 AC 182 ms
42,388 KB
testcase_02 AC 184 ms
42,260 KB
testcase_03 AC 140 ms
41,624 KB
testcase_04 AC 391 ms
42,524 KB
testcase_05 AC 137 ms
41,612 KB
testcase_06 AC 138 ms
41,648 KB
testcase_07 AC 186 ms
42,420 KB
testcase_08 AC 174 ms
42,892 KB
testcase_09 AC 742 ms
42,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	private int[] sum;
	private int[] how;
	private int[] num;
	private int n;
	private int k;
	
	Main(){
		sum = new int[10];
		how = new int[10];
		num = new int[10];
	}
	
	//セッター
	public void setN(int n){
		this.n = n;
	}
	
	public void setNum(int i,int a){
		this.num[i] = a;
	}
	
	public void setK(int k){
		this.k = k;
	}
	
	public void plusSum(int i,int num){
		this.sum[i] += num;
	}
	
	//ゲッター
	public int getN(){
		return this.n;
	}
	
	public int getK(){
		return this.k;
	}
	
	public int getNum(int now){
		return this.num[now];
	}
	
	public int getSum(int now){
		return this.sum[now];
	}
	
	public int getHow(int now){
		return this.how[now];
	}
	
	//再帰
	public int run(){
		double re = this.saiki(0);
		return (int) Math.ceil(re);
	}
	
	private double saiki(int now){
		if(now == getN()){
			double max_h = 0.0,min_h = 100000000.0;
			boolean flag = false;
			for(int i = 0;i < getK();i++){
				if(getSum(i) == 0) flag = true;
				double S = (double) getSum(i);
				double H = (double) getHow(i);
				max_h = Math.max(max_h, S / H);
				min_h = Math.min(min_h, S / H);
			}
			if(flag) return 0.0;
			return max_h - min_h;
		}
		
		int how_zero = 0;
		for(int i = 0;i < getK();i++){
			if(getSum(i) == 0) how_zero++;
		}
		if(how_zero > getN() - now) return 0.0;
		
		double max_n = 0.0;
		for(int i = 0;i < getK();i++){
			plusSum(i, getNum(now));
			this.how[i]++;
			max_n = Math.max(saiki(now + 1),max_n);
			plusSum(i, -getNum(now));
			this.how[i]--;
		}
		return max_n;
	}
	
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Main m = new Main();
		int n = sc.nextInt();
		int k = sc.nextInt();
		int a;
		m.setN(n);
		m.setK(k);
		for(int i = 0;i < n;i++){
			a = sc.nextInt();
			m.setNum(i, a);
		}
		System.out.println(m.run());
	}
}
0