Table Split
Time limit1sMemory limit512 MB
Given a table filled row by row with 1 to n*m, find the single vertical or horizontal cut that makes the two part sums as close as possible.
- Level
Medium6 of 10
- Topics
- Math, Binary search, Implementation
- Solved
- No attempts yet
Problem
A table has rows and columns. The cells are filled row by row with the numbers from 1 to . In other words, cell contains .
The figure below shows such a table for , .
Split the table with one vertical or horizontal cut along the cell sides, so that the sums of the numbers in the two parts differ as little as possible. In this problem, one test contains several queries, and you must find the optimal cut for each one.
Input
The first line contains the number of queries (). Each of the next lines contains two integers and (, ).
Output
Print lines, one answer per line. Each answer has the form D $x$, where D is V for a vertical cut and H for a horizontal cut, and is the number of the column or row before which the cut is made. Rows are numbered from 1 to and columns from 1 to . If several answers are optimal, print a vertical cut if one exists. If there are still several, print the one with the smallest .