当前位置: 首页 > article >正文

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 1t104). 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 2n,m109, 1 ≤ a ≤ n 1 \le a \le n 1an, 1 ≤ b ≤ m 1 \le b \le m 1bm) — 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();
}

}


http://www.lryc.cn/news/2404527.html

相关文章:

  • ubuntu中使用docker
  • 复制与图片文件同名的标签文件到目标路径
  • 【深度学习-Day 24】过拟合与欠拟合:深入解析模型泛化能力的核心挑战
  • [ElasticSearch] DSL查询
  • iview中的table组件点击一行中的任意一点选中本行
  • 《探秘跨网段局域网IP广播:解锁网络通信的新姿势》
  • Kafka 单机部署启动教程(适用于 Spark + Hadoop 环境)
  • maven微服务${revision}依赖打包无法识别
  • 2025年06月07日Github流行趋势
  • WPS中将在线链接转为图片
  • 实战二:开发网页端界面完成黑白视频转为彩色视频
  • 二元函数可微 切平面逼近 线性函数逼近
  • vue生成二维码图片+文字说明
  • 机器学习监督学习实战五:六种算法对声呐回波信号进行分类
  • ​React Hooks 的闭包陷阱问题
  • 力扣面试150题--克隆图
  • 【HarmonyOS 5】运动健康开发实践介绍以及详细案例
  • STM32开发中,线程启动异常问题排查简述
  • SQL进阶之旅 Day 18:数据分区与查询性能
  • 鸿蒙PC,有什么缺点?
  • 前端工具:Webpack、Babel、Git与工程化流程
  • 使用Python和Scikit-Learn实现机器学习模型调优
  • 灰狼优化算法MATLAB实现,包含种群初始化和29种基准函数测试
  • go语言学习 第7章:数组
  • PDF图片和表格等信息提取开源项目
  • 《Progressive Transformers for End-to-End Sign Language Production》复现报告
  • Haystack:AI与IoT领域的全能开源框架
  • OpenWrt:使用ALSA实现边录边播
  • ​链表题解——回文链表【LeetCode】
  • CSS6404L 在物联网设备中的应用优势:低功耗高可靠的存储革新与竞品对比