結果

問題 No.335 門松宝くじ
ユーザー GrenacheGrenache
提出日時 2016-07-28 19:54:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 401 ms / 2,000 ms
コード長 3,553 bytes
コンパイル時間 5,712 ms
コンパイル使用メモリ 79,336 KB
実行使用メモリ 57,564 KB
最終ジャッジ日時 2024-04-24 10:36:04
合計ジャッジ時間 8,008 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
37,208 KB
testcase_01 AC 64 ms
37,348 KB
testcase_02 AC 63 ms
37,260 KB
testcase_03 AC 63 ms
37,580 KB
testcase_04 AC 62 ms
37,424 KB
testcase_05 AC 140 ms
45,128 KB
testcase_06 AC 172 ms
46,724 KB
testcase_07 AC 223 ms
55,424 KB
testcase_08 AC 288 ms
55,864 KB
testcase_09 AC 401 ms
57,564 KB
testcase_10 AC 276 ms
56,672 KB
testcase_11 AC 290 ms
55,820 KB
testcase_12 AC 270 ms
55,808 KB
testcase_13 AC 290 ms
56,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder335 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        int n = sc.nextInt();
        int m = sc.nextInt();

        int maxe = 0;
        int maxei = 0;
        for (int mm = 0; mm < m; mm++) {
        	int[] e = new int[n];
        	for (int i = 0; i < n; i++) {
        		e[i] = sc.nextInt();
        	}

        	int[][] max = new int[n][n];
        	for (int i = 0; i < n; i++) {
        		max[i][i] = e[i];
        		for (int j = i + 1; j < n; j++) {
        			max[i][j] = Math.max(max[i][j - 1], e[j]);
        		}
        	}
        	int[][] maxi = new int[n][n];
        	for (int i = 0; i < n; i++) {
        		for (int j = i + 1; j < n; j++) {
        			if (e[j] < e[i]) {
        				maxi[i][j] = Math.max(maxi[i][j - 1], e[j]);
        			}
        		}
        	}
        	int[][] maxj = new int[n][n];
        	for (int i = n - 1; i >= 0; i--) {
        		for (int j = i - 1; j >= 0; j--) {
        			if (e[j] < e[i]) {
        				maxj[i][j] = Math.max(maxj[i][j + 1], e[j]);
        			}
        		}
        	}

        	int sum = 0;
        	for (int i = 0; i < n; i++) {
        		for (int j = i + 1; j < n; j++) {
        			int tmp = 0;
        			if (e[j] > e[i]) {
        				if (i > 0 && max[0][i - 1] > e[i]) {
        					tmp = Math.max(tmp, max[0][i - 1]);
        				}
        				if (j > i + 1 && max[i + 1][j - 1] > e[j]) {
        					tmp = Math.max(tmp, max[i + 1][j - 1]);
        				}
        				if (j > i + 1) {
        					tmp = Math.max(tmp, maxi[i][j]);
        				}
        				if (j < n - 1) {
        					tmp = Math.max(tmp, maxi[j][n - 1]);
        				}
        			} else {
        				if (i > 0) {
        					tmp = Math.max(tmp, maxj[0][i]);
        				}
        				if (j > i + 1 && max[i + 1][j - 1] > e[i]) {
        					tmp = Math.max(tmp, max[i + 1][j - 1]);
        				}
        				if (j > i + 1) {
        					tmp = Math.max(tmp, maxj[i][j]);
        				}
        				if (j < n - 1 && max[j + 1][n - 1] > e[j]) {
        					tmp = Math.max(tmp, max[j + 1][n - 1]);
        				}
        			}

        			sum += tmp;
        		}
        	}

        	if (sum > maxe) {
        		maxe = sum;
        		maxei = mm;
        	}
        }

        pr.println(maxei);

        pr.close();
        sc.close();
    }

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
//					it = Arrays.asList(br.readLine().split(" ")).iterator();
					it = Arrays.asList(br.readLine().split("\\p{javaWhitespace}+")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0