Submission #1604672


Source Code Expand

def D_takoyaki(N, D, Q, P):
    from itertools import product

    square_memo = [[-1] * N for _ in range(N)]

    def rectangleFromOrigin(x, y):
        # 原点と(x,y)から作れる長方形内でのおいしさの和
        if x < 0 or y < 0:
            return 0
        if square_memo[x][y] == -1:
            # ex.(x,y)=(2,3)
            # 全体     領域1 領域2 領域3 領域4
            # ***--    ++--- ###-- xx--- -----
            # ***--    ++--- ###-- xx--- -----
            # ***-- <= ++--- ###-- xx--- -----
            # ***--    ++--- ----- ----- --o--
            # -----    ----- ----- ----- -----
            # 原点から伸びる*の領域について計算するなら、領域1+2-3+4とすればよい。
            s = 0
            s += rectangleFromOrigin(x - 1, y)  # 領域1
            s += rectangleFromOrigin(x, y - 1)  # 領域2
            s -= rectangleFromOrigin(x - 1, y - 1)  # 領域3
            s += D[x][y]  # 領域4
            square_memo[x][y] = s
        return square_memo[x][y]

    def rectangle(x, y, w, h):
        #(x,y),(w,h)が定める長方形内でのおいしさの和
        s = 0
        # ex.(x,y,w,h)=(1,1,3,3)
        # 全体     領域1 領域2 領域3 領域4
        # -----    ++++- ####- x---- o----
        # -***-    ++++- ----- x---- -----
        # -***- <= ++++- ----- x---- -----
        # -***-    ++++- ----- x---- -----
        # -----    ----- ----- ----- -----
        s += rectangleFromOrigin(w, h)  # 領域1
        s -= rectangleFromOrigin(x - 1, h)  # 領域2
        s -= rectangleFromOrigin(w, y - 1)  # 領域3
        s += rectangleFromOrigin(x - 1, y - 1)  # 領域4
        return s

    ans = [0] * (N**2)
    # いろいろな長方形に対して計算
    for w, h in product(range(N), repeat=2):
        x_max = N - w
        y_max = N - h
        idx = (w + 1) * (h + 1) - 1
        for x, y in product(range(x_max), range(y_max)):
            tmp = rectangle(x, y, x + w, y + h)
            ans[idx] = max(ans[idx], tmp)  # 幅w、高さhの長方形を張ったときのおいしさのうち最大値

    # 「店員が焼ける能力の上限と同じ面積の長方形を張ると逆に点が下がる」場合があるため、
    # 調整を行う(それまでの最大値を上回るときのみ値を更新)
    tmp = 0
    for i in range(N**2):
        if ans[i] > tmp:
            tmp = ans[i]
        else:
            ans[i] = tmp

    for i in P:
        print(ans[i - 1])
    return None

N = int(input())
D = [[int(i) for i in input().split()] for _ in range(N)]
Q = int(input())
P = [int(input()) for i in range(Q)]
D_takoyaki(N, D, Q, P)

Submission Info

Submission Time
Task D - おいしいたこ焼きの焼き方
User kenseiQ
Language Python (3.4.3)
Score 100
Code Size 2736 Byte
Status AC
Exec Time 2730 ms
Memory 3632 KB

Judge Result

Set Name Subtask1 Subtask2
Score / Max Score 50 / 50 50 / 50
Status
AC × 18
AC × 20
Set Name Test Cases
Subtask1 sub0.txt, sub1.txt, sub2.txt, sub_rand_max0.txt, sub_rand_max1.txt, sub_rand_max2.txt, sub_rand_max3.txt, sub_rand_min0.txt, s1.txt, s2.txt, sub0.txt, sub1.txt, sub2.txt, sub_rand_max0.txt, sub_rand_max1.txt, sub_rand_max2.txt, sub_rand_max3.txt, sub_rand_min0.txt
Subtask2 rand0.txt, rand1.txt, rand2.txt, rand3.txt, rand4.txt, rand_max0.txt, rand_max1.txt, rand_max2.txt, rand_max3.txt, rand_max4.txt, s1.txt, s2.txt, sub0.txt, sub1.txt, sub2.txt, sub_rand_max0.txt, sub_rand_max1.txt, sub_rand_max2.txt, sub_rand_max3.txt, sub_rand_min0.txt
Case Name Status Exec Time Memory
rand0.txt AC 1562 ms 3188 KB
rand1.txt AC 852 ms 3316 KB
rand2.txt AC 1360 ms 3572 KB
rand3.txt AC 295 ms 3188 KB
rand4.txt AC 23 ms 3188 KB
rand_max0.txt AC 2708 ms 3632 KB
rand_max1.txt AC 2712 ms 3576 KB
rand_max2.txt AC 2730 ms 3600 KB
rand_max3.txt AC 2694 ms 3608 KB
rand_max4.txt AC 2714 ms 3596 KB
s1.txt AC 18 ms 3188 KB
s2.txt AC 17 ms 3188 KB
sub0.txt AC 18 ms 3188 KB
sub1.txt AC 18 ms 3188 KB
sub2.txt AC 18 ms 3188 KB
sub_rand_max0.txt AC 18 ms 3188 KB
sub_rand_max1.txt AC 18 ms 3188 KB
sub_rand_max2.txt AC 18 ms 3188 KB
sub_rand_max3.txt AC 18 ms 3188 KB
sub_rand_min0.txt AC 17 ms 3188 KB