CKALP08 - Editorial


Sale of Watches

Authors: Preeti Yadav and Aman Shah






Two brothers Ram and Gopal are fond of collecting watches. A sale of watches is being organized in their city for a week.

Both of them want to buy the watches but they can buy them only within certain limits given by their clever mother to reduce their possibility of getting watches.
(Each watch has some rating R_i and price P_i).

Instructions are:

  1. On an even weekday, they cannot buy a watch with odd price and odd rating.

  2. On an odd weekday, they cannot buy a watch with even price and even rating.

  3. The price of the current watch should be less than the price of the last bought watch.

  4. The rating of the current watch should be greater than the rating of the last bought watch.

  5. If all the conditions are satisfied in a particular day, then it is compulsory to buy the watch for that day (since they don’t know the prices and ratings of the watches of coming days in the sale).

Count the maximum number of watches they can buy in the sale.

Note: Weekdays start with 1.


For each test case: If (1^{st}, 3^{rd}, and 4^{th}) or (2^{nd}, 3^{rd}, and 4^{th}) conditions are satisfied increase the count (they can buy the watch).

i.e. for a particular i (1 \leq i \leq 7)

  • if (i % 2 == 0) and (p[i] % 2 == 0) and (r[i] % 2 == 0) [condition 1 satisfied]

  • if (i % 2 == 1) and (p[i] % 2 == 1) and (r[i] % 2 == 1) [condition 2 satisfied]

  • For condition 3 & 4 - Maintain 2 variables for price and rating of previously selected watch (p and q) respectively so that we can compare current price and rating with the previously selected price and rating.

For first time, we will assume the previous price i.e. p to be minimum (-1) and previous rating i.e. q to be maximum so that the 3^{rd} and 4^{th} conditions gets satisfied for the 1st time.

And then update values of p and q every time all the condition holds true and increase the count.


Setter's Solution
def solve(p, r):
	b = c = 0
	a = 1000000001

	for i in range(7):
		if ((i+1)%2 == 0):
			if (p[i]%2 == 0 and r[i]%2 == 0 and p[i]<a and r[i]>b):
				c += 1
				a = p[i]
				b = r[i]
			if (p[i]%2 == 1 and r[i]%2 == 1 and p[i]<a and r[i]>b):
				c += 1
				a = p[i]
				b = r[i]

	return c

t = int(input())

for _ in range(t):
	p = list(map(int, input().split()))
	r = list(map(int, input().split()))
	print(solve(p, r))

can someone explain the solution of the problem plz