結果

問題 No.974 最後の日までに
ユーザー mkawa2mkawa2
提出日時 2020-01-19 16:21:50
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,431 ms / 2,000 ms
コード長 3,651 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 94,024 KB
最終ジャッジ日時 2024-04-14 22:05:06
合計ジャッジ時間 47,260 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,227 ms
94,024 KB
testcase_01 AC 1,216 ms
93,048 KB
testcase_02 AC 1,207 ms
92,912 KB
testcase_03 AC 1,192 ms
92,148 KB
testcase_04 AC 1,192 ms
92,272 KB
testcase_05 AC 1,175 ms
93,040 KB
testcase_06 AC 1,167 ms
92,916 KB
testcase_07 AC 1,154 ms
92,144 KB
testcase_08 AC 1,172 ms
92,272 KB
testcase_09 AC 1,176 ms
93,880 KB
testcase_10 AC 1,204 ms
93,876 KB
testcase_11 AC 1,230 ms
93,228 KB
testcase_12 AC 1,221 ms
93,236 KB
testcase_13 AC 1,208 ms
94,004 KB
testcase_14 AC 1,235 ms
94,012 KB
testcase_15 AC 1,227 ms
93,232 KB
testcase_16 AC 1,235 ms
93,228 KB
testcase_17 AC 1,407 ms
93,232 KB
testcase_18 AC 1,402 ms
93,364 KB
testcase_19 AC 1,398 ms
93,232 KB
testcase_20 AC 1,387 ms
93,236 KB
testcase_21 AC 1,431 ms
93,240 KB
testcase_22 AC 1,418 ms
93,232 KB
testcase_23 AC 1,425 ms
93,364 KB
testcase_24 AC 1,422 ms
93,240 KB
testcase_25 AC 32 ms
10,880 KB
testcase_26 AC 32 ms
10,880 KB
testcase_27 AC 818 ms
55,040 KB
testcase_28 AC 1,128 ms
69,116 KB
testcase_29 AC 31 ms
10,880 KB
testcase_30 AC 1,118 ms
75,244 KB
testcase_31 AC 30 ms
11,008 KB
testcase_32 AC 30 ms
10,880 KB
testcase_33 AC 727 ms
61,696 KB
testcase_34 AC 1,262 ms
92,840 KB
testcase_35 AC 31 ms
11,008 KB
testcase_36 AC 30 ms
10,880 KB
testcase_37 AC 1,137 ms
73,984 KB
testcase_38 AC 30 ms
11,008 KB
testcase_39 AC 30 ms
11,008 KB
testcase_40 AC 529 ms
40,960 KB
testcase_41 AC 695 ms
50,324 KB
testcase_42 AC 1,376 ms
92,704 KB
testcase_43 AC 1,377 ms
92,656 KB
testcase_44 AC 1,030 ms
73,088 KB
testcase_45 AC 803 ms
61,696 KB
testcase_46 AC 31 ms
11,008 KB
testcase_47 AC 31 ms
11,008 KB
testcase_48 AC 30 ms
11,008 KB
testcase_49 AC 31 ms
11,008 KB
testcase_50 AC 31 ms
11,008 KB
testcase_51 AC 30 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    # 前半全列挙
    def dfsl(i=0, like=0, money=0, last=False):
        if i >= n // 2:
            if last:
                # 最終日に学校に行くと、後半の初日はバイトができないので
                # その分を残金からあらかじめ引いておく
                lt.append((money - aa[n // 2], like))
            else:
                lf.append((money, like))
            return
        # バイトの場合
        dfsl(i + 1, like, money + aa[i], last)
        # 学校の場合
        dfsl(i + 2, like + bb[i + 1], money - cc[i + 1], i == n // 2 - 1)

    # 後半全列挙
    def dfsr(i=-1, like=0, money=0, first=False):
        if i == -1: i = n // 2
        if i >= n:
            if first:
                rt.append((money, like))
            else:
                rf.append((money, like))
            return
        # バイトの場合
        dfsr(i + 1, like, money + aa[i], first)
        # 学校の場合
        if i == n // 2: first = True
        if i + 1 < n: dfsr(i + 2, like + bb[i + 1], money - cc[i + 1], first)

    # 全列挙から不要な結果を削除して最適化する
    def opt(aa):
        res = []
        pl = -1
        for m, l in sorted(aa, reverse=True):
            if res and l <= pl: continue
            res.append((m, l))
            pl = l
        return res

    # 前半と後半の組合せから最大好感度を返す
    def cmb(aa, bb, ans):
        if not (aa and bb):return ans
        j = len(bb) - 1
        for am, al in aa:
            while 1:
                bm, bl = bb[j]
                if am + bm >= 0:break
                else:
                    if j > 0:j -= 1
                    else:return ans
            if al + bl > ans: ans = al + bl
        return ans

    n = II()
    aa = []
    bb = []
    cc = []
    for _ in range(n):
        a, b, c = MI()
        aa += [a]
        bb += [b]
        cc += [c]
    # 学校に行く日を固定すると、次の日はデート、その他の日はバイトと一意に決まる
    # 期間を半分に分け、前後半ともに全列挙する
    lt = []  # 前半のうち最終日に学校に行くパターン
    lf = []  # 前半のうち最終日に学校に行かないパターン
    dfsl()
    rt = []  # 後半のうち初日に学校に行くパターン
    rf = []  # 後半のうち初日に学校に行かないパターン
    dfsr()
    # print("lt", lt)
    # print("lf", lf)
    # print("rf", rf)
    # print("ra", ra)
    # 他の結果と比べて、残金も少ないし好感度も低いものは不要なので削除する
    # 残金を降順にしたとき好感度は昇順になるはず
    # なっていない結果を削除
    lt = opt(lt)
    lf = opt(lf)
    rt = opt(rt)
    rf = opt(rf)
    #print("lt", lt)
    #print("rf", rf)
    #print("lf", lf)
    #print("rt", rt)
    # 前半最終日に学校へ行き、後半にも学校に行くパターンはないので
    # (lt,rt)以外、つまり(lt,rf)(lf,rt)(lf,rf)の組み合わせがあり得る
    # しゃくとり法で残金がマイナスにならないように組み合わせながら最大の好感度を求める
    ans = 0
    ans = cmb(lt, rf, ans)
    ans = cmb(lf, rt, ans)
    ans = cmb(lf, rf, ans)
    print(ans)

main()
0