Codeforces Round 1025 (Div. 2) B. Slice to Survive

news2025/6/8 20:23:53

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.coloradmin.cn/o/2404527.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ubuntu中使用docker

上一篇我已经下载了一个ubuntu:20.04的镜像&#xff1b; 1. 查看所有镜像 sudo docker images 2. 基于本地存在的ubuntu:20.04镜像创建一个容器&#xff0c;容器的名为cppubuntu-1。创建的时候就会启动容器。 sudo docker run -itd --name cppubuntu-1 ubuntu:20.04 结果出…

[ElasticSearch] DSL查询

&#x1f338;个人主页:https://blog.csdn.net/2301_80050796?spm1000.2115.3001.5343 &#x1f3f5;️热门专栏: &#x1f9ca; Java基本语法(97平均质量分)https://blog.csdn.net/2301_80050796/category_12615970.html?spm1001.2014.3001.5482 &#x1f355; Collection与…

iview中的table组件点击一行中的任意一点选中本行

<Table border ref"selection" size"small" on-row-click"onClickRow"></Table>// table组件点击一行任意位置选中onClickRow(row, index) {this.$refs.selection.toggleSelect(index)}写上toggleSelect(index)方法即可&#xff0c;…

《探秘跨网段局域网IP广播:解锁网络通信的新姿势》

一、从基础出发:广播与跨网段 在计算机网络的世界中,广播域是一个至关重要的概念。简单来说,广播域是指网络中能接收任一台主机发出的广播帧的所有主机集合。当一台主机在广播域内发出一个广播帧时,同一广播域内的所有其他主机都可以收到该广播帧。在没有路由器或 VLAN 分割…

maven微服务${revision}依赖打包无法识别

1、场景描述 我现在又一个微服务项目&#xff0c;父pom的版本&#xff0c;使用<properties>定义好&#xff0c;如下所示&#xff1a; <name>ypsx-finance-center</name> <artifactId>ypsx-finance</artifactId> <packaging>pom</pack…

2025年06月07日Github流行趋势

项目名称&#xff1a;netbird 项目地址url&#xff1a;https://github.com/netbirdio/netbird项目语言&#xff1a;Go历史star数&#xff1a;14824今日star数&#xff1a;320项目维护者&#xff1a;mlsmaycon, braginini, pascal-fischer, lixmal, pappz项目简介&#xff1a;使…

WPS中将在线链接转为图片

WPS中将在线链接转为图片 文章目录 WPS中将在线链接转为图片一&#xff1a;解决方案1、下载图片&#xff0c;精确匹配&#xff08;会员功能&#xff09;2、将在线链接直接转为图片 一&#xff1a;解决方案 1、下载图片&#xff0c;精确匹配&#xff08;会员功能&#xff09; …

实战二:开发网页端界面完成黑白视频转为彩色视频

​一、需求描述 设计一个简单的视频上色应用&#xff0c;用户可以通过网页界面上传黑白视频&#xff0c;系统会自动将其转换为彩色视频。整个过程对用户来说非常简单直观&#xff0c;不需要了解技术细节。 效果图 ​二、实现思路 总体思路&#xff1a; 用户通过Gradio界面上…

vue生成二维码图片+文字说明

需求&#xff1a;点击下载图片&#xff0c;上方是二维码&#xff0c;下方显示该二维码的相关内容&#xff0c;并且居中显示&#xff0c;支持换行 解决方案步骤&#xff1a; 1. 使用qrcode生成二维码的DataURL。 2. 创建canvas&#xff0c;将二维码图片绘制到canvas的上半部分…

机器学习监督学习实战五:六种算法对声呐回波信号进行分类

本项目基于UCI的声呐目标识别数据集&#xff08;Sonar, Mines vs. Rocks&#xff09;&#xff0c;通过10种机器学习算法比较&#xff0c;发现集成学习方法表现最优。研究首先对60个声呐能量特征进行可视化分析&#xff08;分布直方图、相关性矩阵&#xff09;&#xff0c;对比了…

​React Hooks 的闭包陷阱问题

这是主包在面试中遇到的一道题目&#xff0c;面试官的问题是&#xff1a;"这个页面初次展示出来时Count和step的值是什么&#xff0c;我点击按钮count和step的值有什么变化&#xff1f;“ 这个题目主包回答的不好&#xff0c;所以想做一个总结。 题目 import React, { …

力扣面试150题--克隆图

Day 61 题目描述 思路 /* // Definition for a Node. class Node {public int val;public List<Node> neighbors;public Node() {val 0;neighbors new ArrayList<Node>();}public Node(int _val) {val _val;neighbors new ArrayList<Node>();}public N…

鸿蒙PC,有什么缺点?

点击上方关注 “终端研发部” 设为“星标”&#xff0c;和你一起掌握更多数据库知识 价格太高&#xff0c;二是部分管理员权限首先&#xff0c;三对于开发者不太友好举个例子&#xff1a;VSCode的兼容性对程序员至关重要。若能支持VSCode&#xff0c;这台电脑将成为大多数开发者…

PDF图片和表格等信息提取开源项目

文章目录 综合性工具专门的表格提取工具经典工具 综合性工具 PDF-Extract-Kit - opendatalab开发的综合工具包&#xff0c;包含布局检测、公式检测、公式识别和OCR功能 仓库&#xff1a;opendatalab/PDF-Extract-Kit特点&#xff1a;功能全面&#xff0c;包含表格内容提取的S…

《Progressive Transformers for End-to-End Sign Language Production》复现报告

摘要 本文复现了《Progressive Transformers for End-to-End Sign Language Production》一文中的核心模型结构。该论文提出了一种端到端的手语生成方法&#xff0c;能够将自然语言文本映射为连续的 3D 骨架序列&#xff0c;并引入 Counter Decoding 实现动态序列长度控制。我…

计算机视觉——相机标定

计算机视觉——相机标定 一、像素坐标系、图像坐标系、相机坐标系、世界坐标系二、坐标系变换图像坐标系 → 像素坐标系相机坐标系 → 图像坐标系世界坐标系 → 相机坐标系 ⋆ \star ⋆ 世界坐标系 → 像素坐标系 三、相机标定 一、像素坐标系、图像坐标系、相机坐标系、世界坐…

C语言中的数据类型(二)--结构体

在之前我们已经探讨了C语言中的自定义数据类型和数组&#xff0c;链接如下&#xff1a;C语言中的数据类型&#xff08;上&#xff09;_c语言数据类型-CSDN博客 目录 一、结构体的声明 二、结构体变量的定义和初始化 三、结构体成员的访问 3.1 结构体成员的直接访问 3.2 结…

C++11:原子操作与内存顺序:从理论到实践的无锁并发实现

文章目录 0.简介1.并发编程需要保证的特性2.原子操作2.1 原子操作的特性 3.内存顺序3.1 顺序一致性3.2 释放-获取&#xff08;Release-Acquire)3.3 宽松顺序&#xff08;Relaxed)3.4 内存顺序 4.无锁并发5. 使用建议 0.简介 在并发编程中&#xff0c;原子性、可见性和有序性是…

动力电池点焊机:驱动电池焊接高效与可靠的核心力量|比斯特自动化

在新能源汽车与储能设备需求激增的背景下&#xff0c;动力电池的制造工艺直接影响产品性能与安全性。作为电芯与极耳连接的核心设备&#xff0c;点焊机如何平衡效率、精度与可靠性&#xff0c;成为电池企业关注的重点。 动力电池点焊机的核心功能是确保电芯与极耳的稳固连接。…

【MySQL】10.事务管理

1. 事务的引入 首先我们需要知道CURD操作不加控制会产生什么问题&#xff1a; 为了解决上面的问题&#xff0c;CURD需要满足如下条件&#xff1a; 2. 事务的概念 事务就是一组DML语句组成&#xff0c;这些语句在逻辑上存在相关性&#xff0c;这一组DML语句要么全部成功&…