結果

問題 No.335 門松宝くじ
ユーザー eitahoeitaho
提出日時 2016-03-22 04:43:31
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 4,307 bytes
コンパイル時間 987 ms
コンパイル使用メモリ 115,332 KB
実行使用メモリ 39,972 KB
最終ジャッジ日時 2024-04-08 21:46:15
合計ジャッジ時間 2,436 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
25,460 KB
testcase_01 AC 33 ms
25,460 KB
testcase_02 AC 31 ms
25,460 KB
testcase_03 AC 32 ms
25,460 KB
testcase_04 AC 33 ms
25,460 KB
testcase_05 AC 52 ms
28,916 KB
testcase_06 AC 57 ms
30,836 KB
testcase_07 AC 69 ms
34,992 KB
testcase_08 AC 61 ms
34,992 KB
testcase_09 AC 84 ms
39,972 KB
testcase_10 AC 84 ms
39,972 KB
testcase_11 AC 85 ms
39,972 KB
testcase_12 AC 84 ms
39,972 KB
testcase_13 AC 84 ms
39,972 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

class Program
{
    public void Solve()
    {
        int N = Reader.Int(), M = Reader.Int();
        var table = Reader.IntTable(M);
        double maxEx = -1;
        int bestIndex = 0;

        for (int i = 0; i < M; i++)
        {
            int ex = 0;
            var V = table[i];
            var min = new int[N, N + 1];
            var max = new int[N, N + 1];
            for (int L = 0; L < N; L++)
            {
                min[L, L + 1] = max[L, L + 1] = V[L];
                for (int R = L + 2; R <= N; R++)
                {
                    min[L, R] = Math.Min(V[R - 1], min[L, R - 1]);
                    max[L, R] = Math.Max(V[R - 1], max[L, R - 1]);
                }
            }
            for (int a = 0; a < N; a++)
                for (int b = a + 1; b < N; b++)
                    if (V[a] < V[b])
                    {
                        int val = 0;
                        // c > a < b
                        if (a > 0 && max[0, a] > V[a]) val = Math.Max(max[0, a], V[b]);
                        // a > c < b
                        if (a + 1 < b && min[a + 1, b] < V[a]) CheckMax(ref val, V[b]);
                        // a < c > b
                        if (a + 1 < b && max[a + 1, b] > V[b]) CheckMax(ref val, max[a + 1, b]);
                        // a < b > c
                        if (b + 1 < N && min[b + 1, N] < V[b]) CheckMax(ref val, V[b]);
                        ex += val;
                    }
                    else
                    {
                        // a > b
                        int val = 0;
                        // c < a > b
                        if (a > 0 && min[0, a] < V[a]) val = V[a];
                        // a > c < b
                        if (a + 1 < b && min[a + 1, b] < V[b]) CheckMax(ref val, V[a]);
                        // a < c > b
                        if (a + 1 < b && max[a + 1, b] > V[a]) CheckMax(ref val, max[a + 1, b]);
                        // a > b < c
                        if (b + 1 < N && max[b + 1, N] > V[b]) CheckMax(ref val, Math.Max(V[a], max[b + 1, N]));
                        ex += val;
                    }
            if (ex > maxEx)
            {
                maxEx = ex;
                bestIndex = i;
            }
        }

        Console.WriteLine(bestIndex);
    }

    static void CheckMax(ref int a, int b) { if (b > a) a = b; }

}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    private static TextReader reader = Console.In;
    private static readonly char[] separator = { ' ' };
    private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    private static string[] A = new string[0];
    private static int i;
    private static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); }
    public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
    public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); }
    public static string Line() { return reader.ReadLine().Trim(); }
    private static string[] Split(string s) { return s.Split(separator, op); }
    private static string Next() { CheckNext(); return A[i++]; }
    private static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0