Balanced Partition

4.7

35 votes
Algorithms, Bubble sort algorithm, Easy, Rotation, Sorting, rotations
Problem

There are n houses in the village Dholakpur. The location of each house in the village can be given as (xi,yi) in the Cartesian coordinate plane. There are hi persons living in the ith house. Central electricity authority of the village is set to built a wire line across the village. The wire line is supposed to constructed in a way such that it is the north-east direction. In other words the wire line is parallel to the line y=x. Given that the construction of such line is considered to be effective only if the number of persons living in its left and right side are equal, can you tell if the construction of such wire line is possible? 

Note: If the wire line passes through any house, the house is not considered in either half.

Constraints:

  • 1t100
  • 2n2×103
  • 103xi,yi103
  • 1hi103
  • It is guaranteed that no two houses are at the same location.

Input Format:

The first line contains a single integer t  denoting the number of test cases.

The first line of each test case contains n  i.e the number houses in the village. Next n lines contains 3 space-separated integers xi,yi and hi

Output Format:

Print t  lines each containing a "YES" or "NO" (without quotes). ith line corresponds to the answer of the ith testcase.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
  • Testcase 1: There are infinitely many wire lines which divide in 2 equal parts. Any line y=x+c,2<c<2
  • Testcase 2: No matter how hard you try, you will not get any such wire line.
  • Testcase 3: Exactly 1 line, i.e. y=x+1 exists. Note that for this line the two persons living at (0,1) are neither considered on the left half, nor on the right.
Editor Image

?