1236D - Alice and the Doll - CodeForces Solution


brute force data structures greedy implementation *2300

Please click on ads to support us..

Python Code:

import bisect
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

n, m, k = map(int, input().split())
u = [[0, m + 1] for _ in range(n + 1)]
v = [[0, n + 1] for _ in range(m + 1)]
for _ in range(k):
    x, y = map(int, input().split())
    u[x].append(y)
    v[y].append(x)
for i in range(1, n + 1):
    u[i].sort()
for i in range(1, m + 1):
    v[i].sort()
c = 1
i, j = 1, 1
l = 0
if len(u[1]) > 1 and u[1][1] == 2:
    l = 1
inf = pow(10, 9) + 1
z = [inf, inf, -inf, -inf]
while True:
    if l == 0:
        x = min(u[i][bisect.bisect_left(u[i], j)], z[0])
        d = x - j - 1
        j = x - 1
        z[3] = i
    elif l == 1:
        x = min(v[j][bisect.bisect_left(v[j], i)], z[1])
        d = x - i - 1
        i = x - 1
        z[0] = j
    elif l == 2:
        x = max(u[i][bisect.bisect_left(u[i], j) - 1], z[2])
        d = j - x - 1
        j = x + 1
        z[1] = i
    else:
        x = max(v[j][bisect.bisect_left(v[j], i) - 1], z[3])
        d = i - x - 1
        i = x + 1
        z[2] = j
    if not d:
        break
    c += d
    l += 1
    l %= 4
ans = "Yes" if n * m - c == k else "No"
print(ans)


Comments

Submit
0 Comments
More Questions

1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math
1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility