結果

問題 No.335 門松宝くじ
ユーザー GrenacheGrenache
提出日時 2016-07-28 21:33:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 351 ms / 2,000 ms
コード長 3,653 bytes
コンパイル時間 4,189 ms
コンパイル使用メモリ 79,404 KB
実行使用メモリ 57,800 KB
最終ジャッジ日時 2024-04-24 10:36:55
合計ジャッジ時間 7,040 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
37,236 KB
testcase_01 AC 65 ms
37,404 KB
testcase_02 AC 66 ms
37,572 KB
testcase_03 AC 64 ms
37,216 KB
testcase_04 AC 62 ms
37,360 KB
testcase_05 AC 140 ms
43,984 KB
testcase_06 AC 166 ms
45,968 KB
testcase_07 AC 203 ms
56,460 KB
testcase_08 AC 196 ms
56,336 KB
testcase_09 AC 246 ms
56,240 KB
testcase_10 AC 281 ms
57,036 KB
testcase_11 AC 291 ms
57,284 KB
testcase_12 AC 351 ms
57,800 KB
testcase_13 AC 302 ms
56,908 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]);
        				}
        			}

        			if (tmp > 0) {
        				sum += Math.max(e[i], Math.max(e[j], tmp));
        			}
        		}
        	}

//        	pr.println(sum);
        	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