1539E - Game with Cards - CodeForces Solution


binary search constructive algorithms data structures dp greedy implementation *2500

Please click on ads to support us..

Python Code:

def solve():
	(n, m) = map(int, input().split())
	arr = []
	ql = []
	qr = []
	for i in range(n):
		arr.append(int(input()))
		ql.append(list(map(int, input().split())))
		qr.append(list(map(int, input().split())))
	al = [[m + 1, 0] for i in range(n + 1)]
	ar = [[m + 1, 0] for i in range(n + 1)]
	al[n] = [0, m]
	ar[n] = [0, m]
	for i in range(n - 1, -1, -1):
		if ql[i][0] <= arr[i] <= ql[i][1]:
			if al[i + 1][0] <= arr[i] <= al[i + 1][1]:
				ar[i] = qr[i][:]
			else:
				ar[i][0] = max(ar[i + 1][0], qr[i][0])
				ar[i][1] = min(ar[i + 1][1], qr[i][1])
		if qr[i][0] <= arr[i] <= qr[i][1]:
			if ar[i + 1][0] <= arr[i] <= ar[i + 1][1]:
				al[i] = ql[i][:]
			else:
				al[i][0] = max(al[i + 1][0], ql[i][0])
				al[i][1] = min(al[i + 1][1], ql[i][1])
	if al[0][0] and ar[0][0]:
		print('NO')
		return
	(x, y) = (0, 0)
	ans = []
	for i in range(n):
		if ar[i][0] <= y <= ar[i][1]:
			ans.append(0)
			x = arr[i]
		else:
			ans.append(1)
			y = arr[i]
	print('YES')
	print(*ans)
import sys
input = lambda : sys.stdin.readline().rstrip()
solve()


Comments

Submit
0 Comments
More Questions

1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations
677C - Vanya and Label
1583B - Omkar and Heavenly Tree
1703C - Cypher
1511C - Yet Another Card Deck
1698A - XOR Mixup