포럼
문제 COCI00457

Kosnja

설명

Mirko wants to buy land on which he will build a house for his family. So far, he’s seen \(K\)
pieces of land. Each of them is in the shape of a rectangle and we can think of it as a matrix
with \(N\) rows and \(M\) columns, \(N \times M\) fields in total.
Mirko is aware that, before construction begins, the property needs to be regularly
maintained and the lawn needs to be mowed. Because of this, Mirko bought a lawn mower.
In order to mow the entire lawn of \(N\) rows and \(M\) columns, he needs to go over each field at
least once. He can start from any field facing one of the four main directions (up, down, left,
and right). His lawn mower can only go forwards (to the adjacent field facing the current
direction) or make a 90 degree turn. Additionally, because of his own safety, Mirko can only
use the lawn mower on his land, so he cannot leave the matrix.
Since making the lawn mower turn isn’t simple, Mirko wants to mow the lawn with the
minimal amount of turns. For each piece of land he saw so far, Mirko wants to know the
minimal number of turns he can make so that the entire lawn is mowed. Help Mirko solve this
problem.

제약

In test cases worth 50% of total points, Mirko will see only one piece of land. The dimensions
of this piece of land will be smaller than 500.

입력 형식

The first line of input contains the positive integer \(K\) (\(1 \le K \le 50\,000\)), the number from the
task.
Each of the following \(K\) lines contains two positive integers \(N\) and \(M\) (\(1 \le N\), \(M \le 1\,000\,000\)),
the numbers from the task.

출력 형식

For each piece of land Mirko saw so far, output in a separate line the minimal amount of
turns he can take so that the entire lawn is mowed.

예제 1
입력
2
1 10
10 1
출력
0
0
예제 2
입력
3
1 1
3 3
3 4
출력
0
4
4
예제 3
입력
2
5 8
6 4
출력
8
6
문제 정보

생성자가 기록되지 않았습니다.

출처 COCI 2017/2018 Contest 2

평가 및 의견

Kosnja

개요
출제자 난이도 Unrated 레이팅 미적용 의견 0 / 1 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Kosnja

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8