結果

問題 No.165 四角で囲え!
ユーザー バカらっくバカらっく
提出日時 2019-11-28 09:36:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 4,679 bytes
コンパイル時間 103 ms
コンパイル使用メモリ 11,432 KB
実行使用メモリ 29,180 KB
最終ジャッジ日時 2023-08-11 00:02:45
合計ジャッジ時間 12,996 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-

class Point:
    def __init__(self, init: str):
        tmp = [int(i) for i in init.rstrip().split(" ")]
        self.x = tmp[0]
        self.y = tmp[1]
        self.point = tmp[2]

class AreaState:
    def __init__(self, count, point):
        self.count = count
        self.point = point

class Proc:
    def __init__(self):
        init = [int(i) for i in input().rstrip().split(" ")]
        self.pointCount = init[0]
        self.maxTotal = init[1]
        self.pointList = [Point(input().rstrip()) for i in range(0, self.pointCount)]
        self.xdic = self.create_x_dict()
        self.ydic = self.create_y_dict()
        self.xList = sorted(self.xdic.keys())
        self.yList = sorted(self.ydic.keys())
        self.area = self.create_area_dic()

    def create_area_dic(self):
        area = {}
        area[self.xList[0]] = {}
        area[self.xList[0]][self.yList[0]] = AreaState(0,0)

        if self.yList[0] in self.xdic[self.xList[0]]:
            area[self.xList[0]][self.yList[0]] = AreaState(1, self.xdic[self.xList[0]][self.yList[0]].point)
        for i in range(1, len(self.xList)):
            x = self.xList[i]
            y = self.yList[0]
            ast = AreaState(area[self.xList[i-1]][y].count, area[self.xList[i-1]][y].point)
            if y in self.xdic[x]:
                ast = AreaState(ast.count + 1, ast.point + self.xdic[x][y].point)
            area[x] = {}
            area[x][y] = ast

        for i in range(1, len(self.yList)):
            x = self.xList[0]
            y = self.yList[i]
            ast = AreaState(area[x][self.yList[i-1]].count, area[x][self.yList[i-1]].point)
            if x in self.ydic[y]:
                ast = AreaState(ast.count + 1, ast.point + self.ydic[y][x].point)
            area[x][y] = ast

        for xIdx in range(1, len(self.xList)):
            for yIdx in range(1, len(self.yList)):
                count = area[self.xList[xIdx - 1]][self.yList[yIdx]].count
                count += area[self.xList[xIdx]][self.yList[yIdx - 1]].count
                count -= area[self.xList[xIdx - 1]][self.yList[yIdx - 1]].count

                point = area[self.xList[xIdx - 1]][self.yList[yIdx]].point
                point += area[self.xList[xIdx]][self.yList[yIdx - 1]].point
                point -= area[self.xList[xIdx - 1]][self.yList[yIdx - 1]].point

                ast = AreaState(count, point)
                if self.yList[yIdx] in self.xdic[self.xList[xIdx]]:
                    ast = AreaState(ast.count + 1, ast.point + self.xdic[self.xList[xIdx]][self.yList[yIdx]].point)
                area[self.xList[xIdx]][self.yList[yIdx]] = ast
        return area




    def create_x_dict(self):
        ret = {}
        for p in self.pointList:
            if p.x not in ret:
                ret[p.x] = {}
            ret[p.x][p.y] = p
        return ret

    def create_y_dict(self):
        ret = {}
        for p in self.pointList:
            if p.y not in ret:
                ret[p.y] = {}
            ret[p.y][p.x] = p
        return ret
    def Proc(self):
        ans = 0
        for xidx1 in range(0, len(self.xList)):
            for xidx2 in range(xidx1, len(self.xList)):
                yIdx2 = 0
                for yIdx1 in range(0, len(self.yList)):
                    while yIdx2 < len(self.yList):
                        yIdx2 = max(yIdx2, yIdx1)
                        area = self.getAreaState(xidx1, yIdx1, xidx2, yIdx2)
                        if area.point > self.maxTotal:
                            break
                        ans = max(ans, area.count)
                        yIdx2 += 1
                        if yIdx2 >= len(self.yList):
                            break
                    if(yIdx2 >= len(self.yList)):
                        break
        return ans

    def getAreaState(self, xIdx1, yIdx1, xIdx2, yIdx2):
        baseCount = self.area[self.xList[xIdx2]][self.yList[yIdx2]].count
        basePoint = self.area[self.xList[xIdx2]][self.yList[yIdx2]].point
        if yIdx1 > 0:
            baseCount -= self.area[self.xList[xIdx2]][self.yList[yIdx1 - 1]].count
            basePoint -= self.area[self.xList[xIdx2]][self.yList[yIdx1 - 1]].point
        if xIdx1 > 0:
            baseCount -= self.area[self.xList[xIdx1-1]][self.yList[yIdx2]].count
            basePoint -= self.area[self.xList[xIdx1 - 1]][self.yList[yIdx2]].point
        if xIdx1 > 0 and yIdx1 > 0:
            baseCount += self.area[self.xList[xIdx1-1]][self.yList[yIdx1-1]].count
            basePoint += self.area[self.xList[xIdx1-1]][self.yList[yIdx1-1]].point
        return AreaState(baseCount, basePoint)

proc = Proc()
print(proc.Proc())
0