Solve ABC176 E in Python

Introduction

I will do E --Bomber that I couldn't solve in ABC yesterday.

E - Bomber Keeping the grid in a two-dimensional array is cumbersome and time-consuming, so manage the number of bombs on each of the $ H and W $ axes. If you select the maximum number of each bomb on the $ H and W $ axes and return the sum of the maximum values, the answer seems to be correct, but that is not enough. This is because it cannot handle the case where there is a bomb at the intersection. If there are bombs at the intersections, the sum of their maximums is -1.

Hold the intersecting points by putting tuples in the dict. 0 is the point without a bomb and 1 is the point with a bomb.

from collections import defaultdict

h, w, m = map(int, input().split())

cnt_h = [0] * h
cnt_w = [0] * w
dic = defaultdict(int)

for _ in range(m):
    h_m, w_m = map(int, input().split())
    cnt_h[h_m - 1] += 1
    cnt_w[w_m - 1] += 1
    dic[(h_m - 1, w_m - 1)] += 1

max_h = max(cnt_h)
max_w = max(cnt_w)

mh = []
mw = []

for i in range(h):
    if max_h == cnt_h[i]:
        mh.append(i)

for i in range(w):
    if max_w == cnt_w[i]:
        mw.append(i)

flag = False
for i in mh:
    for j in mw:
        if dic[(i, j)] == 0:
            flag = True #If there is a point of 0, it will be the maximum value, so break it.
            break
    if flag:
        break

if flag:
    print(max_h + max_w)
else:
    print(max_h + max_w - 1)

Recommended Posts

Solve ABC176 E in Python
Solve ABC169 in Python
Solve ABC175 D in Python
Solve Atcoder ABC169 A-D in Python
Solve ABC036 A ~ C in Python
Solve ABC037 A ~ C in Python
Solve Atcoder ABC176 (A, B, C, E) in Python
Solve ABC146-C in Python
ABC 157 D --Solve Friend Suggestions in Python!
Solve ABC098-C in Python
Solve ABC159-D in Python
I wanted to solve ABC159 in Python
Solve ABC165 A, B, D in Python
Solve ABC160-E in Python
AtCoder ABC 177 Python (A ~ E)
Solve AtCoder ABC166 with python
AtCoder ABC 178 Python (A ~ E)
Atcoder ABC164 A-C in Python
AtCoder ABC 176 Python (A ~ E)
Atcoder ABC167 A-D in Python
Solve Wooldridge exercises in Python
Atcoder ABC165 A-D in Python
Atcoder ABC166 A-E in Python
Solve optimization problems in Python
Solve AtCoder ABC 186 with Python
Atcoder ABC169 A-E in Python
AtCoder ABC177 A-D in python
Solve ABC163 A ~ C with Python
Solve ABC166 A ~ D with Python
ABC166 in Python A ~ C problem
Solve ABC168 A ~ C with Python
Solve ABC162 A ~ C with Python
Template AtCoder ABC 179 Python (A ~ E)
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Solve ordinary differential equations in Python
Beginner ABC154 (Python)
Quadtree in Python --2
Python in optimization
Beginner ABC156 (Python)
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
AtCoder ABC 174 Python
Algorithm in Python (ABC 146 C Binary Search
AtCoder ABC187 Python
Epoch in Python
Discord in Python
I wanted to solve ABC160 with Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
How to write offline real time Solve E04 problems in Python
AtCoder ABC188 Python
Programming in python
Beginner ABC155 (Python)