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

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.xdnf.cn/news/926857.html

相关文章:

  • PCB有铜半孔工艺——高密度电子连接的“隐形桥梁”
  • 能 ping 通网址,但是网页打不开
  • 嵌入式知识篇---Zigbee串口
  • 基于51单片机的光强控制LED灯亮灭
  • C++11 Token Bucket (令牌桶)算法的锁无实现及应用
  • 《前缀和》题集
  • 0基础破解Typora,使用正版已激活Typora
  • GIC700组件
  • 计算机组成原理-存储器的概述
  • 按字典序排列最小的等效字符串
  • Linux -- 进程信号
  • DFS(深度优先搜索)
  • 从游戏到自动驾驶:互联网时代强化学习如何让机器学会自主决策?
  • 基于STM32的DHT11温湿度远程监测LCD1602显示Proteus仿真+程序+设计报告+讲解视频
  • Global Security Markets 第 10 章衍生品知识点总结​
  • 第一章 计算机系统构成及硬件基础知识
  • 【2025】typora 安装及破解
  • < 自用文 OS有关 新的JD云主机> 国内 京东云主机 2C4G 60G 5Mb 498/36月 Ubuntu22
  • XGBoost时间序列预测之-未来销量的预测
  • 跳跃游戏 dp还是线段树优化
  • 论文调研_BCSD综述论文调研
  • IOS性能优化
  • Shell 命令及运行原理 + 权限的概念(7)
  • SpringBoot 框架实现文件上传下载分享
  • 泛型接口:允许在接口中使用类型参数
  • gis 高程影像切片地图发布geoserver
  • 深圳SMT贴片工艺优化关键步骤
  • 财务后台系统
  • Python Day44 学习(日志Day12复习)
  • 嵌入式部分BSP相关实现