結果

問題 No.335 門松宝くじ
ユーザー eitahoeitaho
提出日時 2016-03-22 03:37:00
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,654 ms / 2,000 ms
コード長 6,588 bytes
コンパイル時間 1,135 ms
コンパイル使用メモリ 115,356 KB
実行使用メモリ 30,196 KB
最終ジャッジ日時 2024-04-08 21:44:26
合計ジャッジ時間 13,037 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
25,636 KB
testcase_01 AC 32 ms
25,636 KB
testcase_02 AC 32 ms
25,636 KB
testcase_03 AC 33 ms
25,636 KB
testcase_04 AC 32 ms
25,636 KB
testcase_05 AC 436 ms
30,164 KB
testcase_06 AC 641 ms
30,164 KB
testcase_07 AC 1,117 ms
30,196 KB
testcase_08 AC 762 ms
30,196 KB
testcase_09 AC 1,650 ms
30,196 KB
testcase_10 AC 1,640 ms
30,196 KB
testcase_11 AC 1,626 ms
30,196 KB
testcase_12 AC 1,654 ms
30,196 KB
testcase_13 AC 1,625 ms
30,196 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 seg = new MinMaxSegTree(N);
            var V = table[i];
            for (int x = 0; x < N; x++)
                seg.Set(x, V[x]);
            for (int a = 0; a < N; a++)
                for (int b = a + 1; b < N; b++)
                {
                    //int val = 0;
                    //Action<int, int, int> Check = (x, y, z) =>
                    //{
                    //    int max = Math.Max(x, Math.Max(y, z));
                    //    if (max == y) { CheckMax(ref val, max); return; }
                    //    int min = Math.Min(x, Math.Min(y, z));
                    //    if (min == y) CheckMax(ref val, max);
                    //};

                    //if (a > 0)
                    //{
                    //    Check(seg.Min(0, a), V[a], V[b]);
                    //    Check(seg.Max(0, a), V[a], V[b]);
                    //}
                    //if (a + 1 < b)
                    //{
                    //    Check(V[a], seg.Min(a + 1, b), V[b]);
                    //    Check(V[a], seg.Max(a + 1, b), V[b]);
                    //}
                    //if (b + 1 < N)
                    //{
                    //    Check(V[a], V[b], seg.Min(b + 1, N));
                    //    Check(V[a], V[b], seg.Max(b + 1, N));
                    //}
                    //ex += val;

                    if (V[a] < V[b])
                    {
                        int val = 0;
                        // c > a < b
                        if (seg.Max(0, a) > V[a]) val = Math.Max(seg.Max(0, a), V[b]);
                        // a > c < b
                        if (seg.Min(a + 1, b) < V[a]) CheckMax(ref val, V[b]);
                        // a < c > b
                        if (seg.Max(a + 1, b) > V[b]) CheckMax(ref val, seg.Max(a + 1, b));
                        // a < b > c
                        if (seg.Min(b + 1, N) < V[b]) CheckMax(ref val, V[b]);
                        ex += val;
                    }
                    else
                    {
                        // a > b
                        int val = 0;
                        // c < a > b
                        if (seg.Min(0, a) < V[a]) val = V[a];
                        // a > c < b
                        if (seg.Min(a + 1, b) < V[b]) CheckMax(ref val, V[a]);
                        // a < c > b
                        if (seg.Max(a + 1, b) > V[a]) CheckMax(ref val, seg.Max(a + 1, b));
                        // a > b < c
                        if (seg.Max(b + 1, N) > V[b]) CheckMax(ref val, Math.Max(V[a], seg.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 MinMaxSegTree
    {
        private static readonly int INF = (int)1e9;
        public readonly int N;
        private readonly int[] min, max;

        public MinMaxSegTree(int n)
        {
            for (N = 4; N < n; ) N *= 2;
            min = new int[N * 2];
            max = new int[N * 2];
            for (int i = 0; i < N; i++) { min[i] = INF; max[i] = -INF; }
        }
        public void Set(int index, int val)
        {
            index += N - 1;
            min[index] = val;
            max[index] = val;
            while (index > 0)
            {
                index = (index - 1) / 2;
                min[index] = Math.Min(min[index * 2 + 1], min[index * 2 + 2]);
                max[index] = Math.Max(max[index * 2 + 1], max[index * 2 + 2]);
            }
        }

        // [L, R)
        public int Min(int L, int R) { return Rec(0, 0, N, L, R, Math.Min, min, INF); }
        public int Max(int L, int R) { return Rec(0, 0, N, L, R, Math.Max, max, -INF); }

        private int Rec(int index, int currL, int currR, int L, int R,
                        Func<int, int, int> f, int[] a, int defaultVal)
        {
            if (currL >= R || currR <= L) return defaultVal;
            if (currL >= L && currR <= R) return a[index];
            int mid = (currL + currR) / 2;
            int vl = Rec(index * 2 + 1, currL, mid, L, R, f, a, defaultVal);
            int vr = Rec(index * 2 + 2, mid, currR, L, R, f, a, defaultVal);
            return f(vl, vr);
        }
    }
}


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