結果

問題 No.115 遠足のおやつ
ユーザー atn112323atn112323
提出日時 2016-04-01 11:23:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 173 ms / 5,000 ms
コード長 12,310 bytes
コンパイル時間 2,670 ms
コンパイル使用メモリ 79,540 KB
実行使用メモリ 58,896 KB
最終ジャッジ日時 2023-08-31 00:38:17
合計ジャッジ時間 6,710 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,712 KB
testcase_01 AC 40 ms
49,708 KB
testcase_02 AC 84 ms
54,148 KB
testcase_03 AC 40 ms
49,724 KB
testcase_04 AC 97 ms
56,244 KB
testcase_05 AC 81 ms
52,860 KB
testcase_06 AC 39 ms
49,520 KB
testcase_07 AC 71 ms
52,548 KB
testcase_08 AC 40 ms
49,612 KB
testcase_09 AC 40 ms
47,500 KB
testcase_10 AC 43 ms
49,460 KB
testcase_11 AC 41 ms
49,396 KB
testcase_12 AC 42 ms
49,416 KB
testcase_13 AC 40 ms
49,356 KB
testcase_14 AC 78 ms
52,744 KB
testcase_15 AC 94 ms
56,060 KB
testcase_16 AC 93 ms
56,204 KB
testcase_17 AC 72 ms
52,480 KB
testcase_18 AC 42 ms
49,608 KB
testcase_19 AC 76 ms
52,856 KB
testcase_20 AC 84 ms
52,604 KB
testcase_21 AC 42 ms
49,608 KB
testcase_22 AC 97 ms
54,248 KB
testcase_23 AC 44 ms
50,080 KB
testcase_24 AC 97 ms
54,060 KB
testcase_25 AC 77 ms
52,900 KB
testcase_26 AC 86 ms
52,480 KB
testcase_27 AC 79 ms
52,760 KB
testcase_28 AC 80 ms
53,044 KB
testcase_29 AC 87 ms
54,116 KB
testcase_30 AC 92 ms
54,212 KB
testcase_31 AC 85 ms
54,104 KB
testcase_32 AC 93 ms
56,644 KB
testcase_33 AC 82 ms
52,824 KB
testcase_34 AC 100 ms
56,180 KB
testcase_35 AC 91 ms
53,940 KB
testcase_36 AC 90 ms
52,216 KB
testcase_37 AC 94 ms
56,640 KB
testcase_38 AC 173 ms
58,896 KB
testcase_39 AC 113 ms
56,084 KB
testcase_40 AC 40 ms
49,724 KB
testcase_41 AC 43 ms
49,660 KB
testcase_42 AC 72 ms
52,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	/**
	 * This class represents a problem setting.
	 */
	class Problem implements IProblem {

		int[] NDK;
		
		@Override
		public void set(ProblemReader reader) throws IOException {
			NDK = reader.nextIntArray();
		}
		
		@Override
		public String toString() {
			// TODO
			return null;
		}
	}
	
	/**
	 * Solver for the problem.
	 * It's the most important part of codes in this file.
	 */
	class Solver implements IProblemSolver<Problem> {

		int N;
		int[][][] memo;
		
		@Override
		public String solve(Problem problem) {
			int N = problem.NDK[0], D = problem.NDK[1], K = problem.NDK[2];
			return StringUtils.toString(solve(N, D, K), " ");
		}
		
		private int[] solve(int N, int D, int K) {
			this.N = N;
			memo = new int[N][D + 1][K + 1];
			for (int i = 0; i < N; i++) {
				memo[i][0][0] = 1;
			}
			int[] res = new int[K];
			int idx = 0;
			int rest = D;
			for (int i = 0; i < N; i++) {
				if (dfs(i + 1, rest - i - 1, K - idx - 1) == 1) {
					res[idx++] = i + 1;
					rest -= i + 1;
				}
			}
			return idx == K ? res : new int[]{-1};
		}
		
		private int dfs(int idx, int d, int k) {
			if (d == 0 && k == 0) {
				return 1;
			}
			if (idx >= N || d < 0 || k < 0) {
				return -1;
			}
			if (memo[idx][d][k] != 0) {
				return memo[idx][d][k];
			}
			for (int i = idx; i < N; i++) {
				if (dfs(i + 1, d - i - 1, k - 1) == 1) {
					return memo[idx][d][k] = 1;
				}
			}
			return memo[idx][d][k] = -1;
		}
	}
	
	/**
	 * This class is a solver to confirm the main solver.
	 * This solver often works only for a problem with a small number setting.
	 */
	class Solver2 implements IProblemSolver<Problem> {

		@Override
		public String solve(Problem problem) {
			// TODO
			return null;
		}
	}
	
	class Factory implements IProblemFactory<Problem> {

		@Override
		public Problem newProblem() {
			// TODO
			return null;
		}

		@Override
		public Problem newProblem(ProblemReader reader) throws IOException {
			Problem problem = new Problem();
			problem.set(reader);
			return problem;
		}
	}
	
	class ResultComparator implements IResultComparator {
		
		@Override
		public boolean same(String res1, String res2) {
			// TODO
			return res1.equals(res2);
		}
	}
	
	ProblemProcessor<Problem> processor;
	
	Main() {
		Init();
	}
	
	void Init() {
		ProblemProcessorConfig<Problem> config = new ProblemProcessorConfig<>();
		
		config.factory = new Factory();
		config.solver = new Solver();
		config.solver2 = new Solver2();
		config.comparator = new ResultComparator();
		
		processor = new ProblemProcessor<Problem>(config);
	}
	
	void process(String mode) throws IOException {
		processor.process(mode);
	}
	
	public static void main(String args[]) throws IOException {
		// "create", "debug", and "solve"
		String mode = "solve";
		new Main().process(mode);
	}
	
/*
	Common library
*/
	
	interface IProblem {
		
		abstract void set(ProblemReader reader) throws IOException;
	}
	
	interface IProblemFactory<P extends IProblem> {
		
		abstract P newProblem();
		abstract P newProblem(ProblemReader reader) throws IOException;
	}
	
	interface IProblemSolver<P extends IProblem> {
		
		abstract String solve(P problem);
	}
	
	interface IResultComparator {
		
		abstract boolean same(String res1, String res2);
	}
	
	class ProblemReader {
		
		BufferedReader br = null;
		
		ProblemReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		
		int nextInt() throws IOException {
			return StringUtils.toInt(br.readLine());
		}
		
		long nextLong() throws IOException {
			return StringUtils.toLong(br.readLine());
		}
		
		double nextDouble() throws IOException {
			return StringUtils.toDouble(br.readLine());
		}
		
		String nextString() throws IOException {
			return br.readLine();
		}
		
		int[] nextIntArray() throws IOException {
			return StringUtils.toIntArray(br.readLine());
		}
		
		long[] nextLongArray() throws IOException {
			return StringUtils.toLongArray(br.readLine());
		}
		
		double[] nextDoubleArray() throws IOException {
			return StringUtils.toDoubleArray(br.readLine());
		}
		
		String[] nextStringArray() throws IOException {
			return StringUtils.toStringArray(br.readLine());
		}
		
		int[] nextIntArray(int n) throws IOException {
			int[] a = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = nextInt();
			}
			return a;
		}
		
		long[] nextLongArray(int n) throws IOException {
			long[] a = new long[n];
			for (int i = 0; i < n; i++) {
				a[i] = nextLong();
			}
			return a;
		}
		
		double[] nextDoubleArray(int n) throws IOException {
			double[] a = new double[n];
			for (int i = 0; i < n; i++) {
				a[i] = nextDouble();
			}
			return a;
		}
		
		String[] nextStringArray(int n) throws IOException {
			String[] strs = new String[n];
			for (int i = 0; i < n; i++) {
				strs[i] = nextString();
			}
			return strs;
		}
		
		int[][] nextIntArray2(int n) throws IOException {
			int[][] a = new int[n][];
			for (int i = 0; i < n; i++) {
				a[i] = nextIntArray();
			}
			return a;
		}
		
		long[][] nextLongArray2(int n) throws IOException {
			long[][] a = new long[n][];
			for (int i = 0; i < n; i++) {
				a[i] = nextLongArray();
			}
			return a;
		}
		
		double[][] nextDoubleArray2(int n) throws IOException {
			double[][] a = new double[n][];
			for (int i = 0; i < n; i++) {
				a[i] = nextDoubleArray();
			}
			return a;
		}
		
		String[][] nextStringArray2(int n) throws IOException {
			String[][] strs = new String[n][];
			for (int i = 0; i < n; i++) {
				strs[i] = nextStringArray();
			}
			return strs;
		}
		
		void close() throws IOException {
			if (br != null) {
				br.close();
			}
		}
	}
	
	class ProblemCreator<P extends IProblem> {
		
		IProblemFactory<P> factory;
		
		ProblemCreator(IProblemFactory<P> factory) {
			this.factory = factory;
		}
		
		void create() throws FileNotFoundException {
			P problem = factory.newProblem();
			System.out.println(problem.toString());
		}
	}
	
	class ProblemSolverComparator<P extends IProblem> {
		
		IProblemFactory<P> factory;
		IProblemSolver<P> solver1, solver2;
		IResultComparator comparator;
		
		ProblemSolverComparator(IProblemFactory<P> factory, IProblemSolver<P> solver1, IProblemSolver<P> solver2, IResultComparator comparator) {
			this.factory = factory;
			this.solver1 = solver1;
			this.solver2 = solver2;
			this.comparator = comparator;
		}
		
		void compare() throws IOException {
			ProblemReader reader = null;
			try {
				reader = new ProblemReader();
				P problem = factory.newProblem(reader);
				String res1 = solver1.solve(problem);
				String res2 = solver2.solve(problem);
				if (comparator.same(res1, res2)) {
					System.out.println(res1);
				} else {
					System.err.println(res1 + " " + res2);
				}
			} finally {
				if (reader != null) {
					reader.close();
				}
			}
		}
	}
	
	class ProblemAnswerer<P extends IProblem> {
		
		IProblemFactory<P> factory;
		IProblemSolver<P> solver;
		
		ProblemAnswerer(IProblemFactory<P> factory, IProblemSolver<P> solver) {
			this.factory = factory;
			this.solver = solver;
		}
		
		void answer() throws IOException {
			ProblemReader reader = null;
			try {
				reader = new ProblemReader();
				P problem = factory.newProblem(reader);
				String res = solver.solve(problem);
				System.out.println(res);
			} finally {
				if (reader != null) {
					reader.close();
				}
			}
		}
	}
	
	class ProblemProcessorConfig<P extends IProblem> {
		
		IProblemFactory<P> factory;
		
		IProblemSolver<P> solver, solver2;
		
		IResultComparator comparator;
	}
	
	class ProblemProcessor<P extends IProblem> {
		
		ProblemProcessorConfig<P> config;
		
		ProblemCreator<P> creator;
		ProblemSolverComparator<P> comparator;
		ProblemAnswerer<P> answerer;
		
		ProblemProcessor(ProblemProcessorConfig<P> config) {
			this.config = config;
			creator = new ProblemCreator<P>(config.factory);
			comparator = new ProblemSolverComparator<P>(config.factory, config.solver, config.solver2, config.comparator);
			answerer = new ProblemAnswerer<P>(config.factory, config.solver);
		}
		
		void process(String mode) throws IOException {
			switch (mode) {
			case "create":
				creator.create();
				break;
			case "debug":
				comparator.compare();
				break;
			case "solve":
				answerer.answer();
				break;
			}
		}
	}
	
	static class StringUtils {
		
		static String toString(int a) {
			return String.valueOf(a);
		}
		
		static String toString(long a) {
			return String.valueOf(a);
		}
		
		static String toString(double a) {
			return String.valueOf(a);
		}
		
		static String toString(String str) {
			return str;
		}
		
		static String toString(int[] a, String delimiter) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < a.length; i++) {
				if (i < a.length - 1) {
					sb.append(a[i] + delimiter);
				} else {
					sb.append(a[i]);
				}
			}
			return sb.toString();
		}
		
		static String toString(long[] a, String delimiter) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < a.length; i++) {
				if (i < a.length - 1) {
					sb.append(a[i] + delimiter);
				} else {
					sb.append(a[i]);
				}
			}
			return sb.toString();
		}
		
		static String toString(double[] a, String delimiter) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < a.length; i++) {
				if (i < a.length - 1) {
					sb.append(a[i] + delimiter);
				} else {
					sb.append(a[i]);
				}
			}
			return sb.toString();
		}
		
		static String toString(String[] strs, String delimiter) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < strs.length; i++) {
				if (i < strs.length - 1) {
					sb.append(strs[i] + delimiter);
				} else {
					sb.append(strs[i]);
				}
			}
			return sb.toString();
		}
		
		static String toString(int[][] a) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < a[i].length; j++) {
					if (j < a[i].length - 1) {
						sb.append(a[i][j] + " ");
					} else {
						sb.append(a[i][j]);
					}
				}
				sb.append("\n");
			}
			sb.setLength(sb.length() - 1);
			return sb.toString();
		}
		
		static String toString(long[][] a) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < a[i].length; j++) {
					if (j < a[i].length - 1) {
						sb.append(a[i][j] + " ");
					} else {
						sb.append(a[i][j]);
					}
				}
				sb.append("\n");
			}
			sb.setLength(sb.length() - 1);
			return sb.toString();
		}
		
		static String toString(double[][] a) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < a[i].length; j++) {
					if (j < a[i].length - 1) {
						sb.append(a[i][j] + " ");
					} else {
						sb.append(a[i][j]);
					}
				}
				sb.append("\n");
			}
			sb.setLength(sb.length() - 1);
			return sb.toString();
		}
		
		static String toString(String[][] a) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < a[i].length; j++) {
					if (j < a[i].length - 1) {
						sb.append(a[i][j] + " ");
					} else {
						sb.append(a[i][j]);
					}
				}
				sb.append("\n");
			}
			sb.setLength(sb.length() - 1);
			return sb.toString();
		}
		
		static int toInt(String s) {
			return Integer.parseInt(s);
		}
		
		static long toLong(String s) {
			return Long.parseLong(s);
		}
		
		static double toDouble(String s) {
			return Double.parseDouble(s);
		}
		
		static int[] toIntArray(String s) {
			String[] tokens = s.split(" ");
			int[] a = new int[tokens.length];
			for (int i = 0; i < tokens.length; i++) {
				a[i] = Integer.parseInt(tokens[i]);
			}
			return a;
		}
		
		static long[] toLongArray(String s) {
			String[] tokens = s.split(" ");
			long[] a = new long[tokens.length];
			for (int i = 0; i < tokens.length; i++) {
				a[i] = Long.parseLong(tokens[i]);
			}
			return a;
		}
		
		static double[] toDoubleArray(String s) {
			String[] tokens = s.split(" ");
			double[] a = new double[tokens.length];
			for (int i = 0; i < tokens.length; i++) {
				a[i] = Double.parseDouble(tokens[i]);
			}
			return a;
		}
		
		static String[] toStringArray(String s) {
			return s.split(" ");
		}
	}
}
0