結果

問題 No.1715 Dinner 2
ユーザー bluemeganebluemegane
提出日時 2021-10-24 10:47:33
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 517 ms / 2,000 ms
コード長 3,294 bytes
コンパイル時間 730 ms
コンパイル使用メモリ 114,444 KB
実行使用メモリ 50,904 KB
最終ジャッジ日時 2024-04-09 21:41:56
合計ジャッジ時間 6,625 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
23,916 KB
testcase_01 AC 23 ms
24,032 KB
testcase_02 AC 23 ms
23,908 KB
testcase_03 AC 26 ms
26,204 KB
testcase_04 AC 22 ms
23,856 KB
testcase_05 AC 22 ms
24,168 KB
testcase_06 AC 23 ms
24,240 KB
testcase_07 AC 24 ms
26,200 KB
testcase_08 AC 22 ms
24,028 KB
testcase_09 AC 21 ms
24,236 KB
testcase_10 AC 22 ms
24,240 KB
testcase_11 AC 387 ms
46,720 KB
testcase_12 AC 286 ms
47,368 KB
testcase_13 AC 252 ms
46,424 KB
testcase_14 AC 301 ms
45,208 KB
testcase_15 AC 336 ms
47,684 KB
testcase_16 AC 298 ms
42,412 KB
testcase_17 AC 206 ms
46,548 KB
testcase_18 AC 24 ms
26,056 KB
testcase_19 AC 25 ms
24,236 KB
testcase_20 AC 24 ms
24,144 KB
testcase_21 AC 22 ms
23,904 KB
testcase_22 AC 24 ms
24,024 KB
testcase_23 AC 26 ms
24,220 KB
testcase_24 AC 24 ms
22,100 KB
testcase_25 AC 58 ms
31,332 KB
testcase_26 AC 43 ms
26,292 KB
testcase_27 AC 45 ms
28,332 KB
testcase_28 AC 52 ms
30,560 KB
testcase_29 AC 58 ms
32,860 KB
testcase_30 AC 51 ms
32,284 KB
testcase_31 AC 58 ms
30,388 KB
testcase_32 AC 484 ms
50,608 KB
testcase_33 AC 447 ms
50,904 KB
testcase_34 AC 472 ms
50,800 KB
testcase_35 AC 517 ms
48,748 KB
testcase_36 AC 23 ms
23,980 KB
testcase_37 AC 22 ms
23,776 KB
testcase_38 AC 24 ms
21,712 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;

public class Hello
{
    public static int max1, max2, tmax1, tmax2, max1i, max2i, tmax1i, tmax2i;
    static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        var n = int.Parse(line[0]);
        var d = int.Parse(line[1]);
        var p = new int[n];
        var q = new int[n];
        var r = new int[n];
        for (int i = 0; i < n; i++)
        {
            line = Console.ReadLine().Trim().Split(' ');
            p[i] = int.Parse(line[0]);
            q[i] = int.Parse(line[1]);
            r[i] = q[i] - p[i];
        }
        getAns(n, d, p, q, r);
    }
    static void getAns(int n, int d, int[] p, int[] q, int[] r)
    {
        var ok = -100000000;
        var ng = 0;
        while (ng - ok > 1)
        {
            var mid = ok + (ng - ok) / 2;
            if (check(n, d, p, q, r, mid)) ok = mid;
            else ng = mid;
        }
        Console.WriteLine(ok);
    }
    static void setMax(int t,int i)
    {
        if (t >= max1) { max2 = max1; max2i = max1i; max1 = t; max1i = i; }
        else if (t >= max2) { max2 = t; max2i = i;   }
    }
    static void setTmax(int t , int i)
    {
        if (t >= tmax1) { tmax2 = tmax1; tmax2i = tmax1i; tmax1 = t; tmax1i = i; }
        else if (t >= tmax2) { tmax2 = t; tmax2i = i;   }
    }

    static bool check(int n, int d, int[] p, int[] q, int[] r, int t)
    {
        var dp = new int[d + 1, n];
        for (int i = 0; i < d + 1; i++)
            for (int j = 0; j < n; j++) dp[i, j] = int.MinValue;
        max1 = int.MinValue;
        max2 = int.MinValue;
        for (int i = 0; i < n; i++)
        {
            if (-p[i] >= t) dp[1, i] = r[i];
            setMax( dp[1,i] ,i);
        }
        if (max1 == int.MinValue) return false;
        for (int i = 2; i <= d; i++)
        {
            if (max1 == int.MinValue) return false;
            tmax1 = int.MinValue;
            tmax2 = int.MinValue;
            for (int j = 0; j < n; j++)
            {
                if (j == max1i)
                {
                    if (max2 == int.MinValue) { dp[i, j] = int.MinValue; setTmax(int.MinValue, j); }
                    else
                    {
                        if (dp[i - 1, max2i] - p[j] < t) 
                        {
                            dp[i, j] = int.MinValue; 
                            setTmax(int.MinValue, j);
                        }
                        else
                        {
                            dp[i, j] = dp[i - 1, max2i] + r[j];
                            setTmax(dp[i - 1, max2i] + r[j],j);
                        }
                    }
                }
                else
                {
                    if (dp[i - 1, max1i] - p[j] < t) { dp[i, j] = int.MinValue; setTmax(int.MinValue, j); }
                    else
                    {
                        dp[i, j] = dp[i - 1, max1i] + r[j];
                        setTmax(dp[i - 1, max1i] + r[j], j);
                    }
                }
            }
            max1 = tmax1;
            max2 = tmax2;
            max1i = tmax1i;
            max2i = tmax2i;
        }
        for (int i = 0; i < n; i++)
        {
            if (dp[d, i] != int.MinValue) { return true; }
        }
        return false;
    }
}
0