Codeforces Round 1025 (Div. 2) B. Slice to Survive
Codeforces Round 1025 (Div. 2) B. Slice to Survive
题目
Duelists Mouf and Fouad enter the arena, which is an n × m n \times m n×m grid!
Fouad’s monster starts at cell ( a , b ) (a, b) (a,b), where rows are numbered 1 1 1 to n n n and columns 1 1 1 to m m m.
Mouf and Fouad will keep duelling until the grid consists of only one cell.
In each turn:
- Mouf first cuts the grid along a row or column line into two parts, discarding the part without Fouad’s monster. Note that the grid must have at least two cells; otherwise, the game has already ended.
- After that, in the same turn, Fouad moves his monster to any cell (possibly the same one it was in) within the remaining grid.
Visualization of the phases of the fourth test case.
Mouf wants to minimize the number of turns, while Fouad wants to maximize them. How many turns will this epic duel last if both play optimally?
Input
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 10 4 1 \le t \le 10^4 1≤t≤104). The description of the test cases follows.
The first and only line of each test case contains four integers n n n, m m m, a a a, and b b b ( 2 ≤ n , m ≤ 10 9 2 \le n, m \le 10^9 2≤n,m≤109, 1 ≤ a ≤ n 1 \le a \le n 1≤a≤n, 1 ≤ b ≤ m 1 \le b \le m 1≤b≤m) — denoting the number of rows, the number of columns, the starting row of the monster, and the starting column of the monster, respectively.
Output
For each test case, output a single integer — the number of turns this epic duel will last if both play optimally.
Example
Input
8
2 2 1 1
3 3 2 2
2 7 1 4
2 7 2 2
8 9 4 6
9 9 5 5
2 20 2 11
22 99 20 70
Output
2
4
4
3
6
8
6
10
Note
In the first test case, one possible duel sequence is as follows:
- Turn 1: Mouf cuts the grid horizontally along the line between the rows 1 1 1 and 2 2 2, removing the bottom half and leaving a 1 × 2 1 \times 2 1×2 grid.
- Turn 1: Fouad’s monster is at the cell ( 1 , 1 ) (1,1) (1,1).
- Turn 2: Mouf cuts the 1 × 2 1 \times 2 1×2 grid again, removes one column, and isolates the cell ( 1 , 1 ) (1,1) (1,1).
The duel is completed in 2 2 2 turns.
In the fourth case, one possible duel sequence is as follows:
- Turn 1: Mouf cuts the grid vertically along the line between the columns 2 2 2 and 3 3 3, splitting it into a 2 × 2 2 \times 2 2×2 and a 2 × 5 2 \times 5 2×5 field, then removes the 2 × 5 2 \times 5 2×5 part.
- Turn 1: Fouad moves the monster to the cell ( 1 , 1 ) (1,1) (1,1).
- From this point on, the duel plays out just like the first test case—two more turns trim down the grid from 2 × 2 2 \times 2 2×2 to a single 1 × 1 1 \times 1 1×1 cell.
In total, the duel is completed in 3 3 3 turns.
You can refer to the pictures mentioned in the problem statement for illustrations of the fourth test case.
题目解析及思路
题目要求输出最终只剩一格的最小操作数
每轮操作由Mouf先执行,他可以对任意一行或一列切一刀
然后Fouad可以移动他的怪兽到任意一格
Mouf的最优操作是在切完以后给Fouad留下最少的格子
Fouad的最优操作是尽量移动到中间
代码
/*
因为最后一次操作是Mouf切完就结束
所以可以考虑先让Mouf切一次,然后再执行:先移动再切 的循环
Mouf的第一刀有四种情况
对于切完第一刀之后的循环,每次一定是长或宽变为原来的[n/2],可以直接算出次数
*/
#include <bits/stdc++.h>
#define int64 long long
#define endl '\n'
using namespace std;void solve(){int n,m,a,b;cin>>n>>m>>a>>b;int ans = n + m;vector<pair<int,int>> v;//第一刀的四种情况的长和宽v.push_back(make_pair(a,m));v.push_back(make_pair(n-a+1,m));v.push_back(make_pair(n,b));v.push_back(make_pair(n,m-b+1));for(auto [l,r] : v){int t = 0;//长和宽每次都是变为原来的[n/2]while(l > 1){t ++;l = (l + 1) / 2;}while(r > 1){t ++;r = (r + 1) / 2;}ans = min(ans,t);}//加上第一刀cout<<ans+1<<endl;
}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int t;cin>>t;while(t--){solve();}
}
::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){solve();
}
}