题意翻译

题目大意:

给定一个n∗m的矩阵,每次你可以选择前进一格或转弯(90度),求在不出这个矩阵的情况下遍历全部格点所需最少转弯次数。有多组数据

输入格式:

第一行一个整数k,表示数据组数

以下k行,每行两个整数n,m,表示矩阵大小

输出格式:

输出一个整数,即最少转弯次数

感谢@守望 提供翻译

题目描述

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 × 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.

输入输出格式

输入格式:

The first line of input contains the positive integer K (1 ≤ K ≤ 50 000), the number from the task.

Each of the following K lines contains two positive integers N and M (1 ≤ N, M ≤ 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
输出样例#1: 复制

0
0
输入样例#2: 复制

3
1 1
3 3
3 4
输出样例#2: 复制

0
4
4
输入样例#3: 复制

2
5 8
6 4
输出样例#3: 复制

8
6

说明

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.

Clarification​ ​of​ ​the​ ​first​ ​test​ ​case:

The first piece of land can be mowed without making any turns if he starts from the field in the first column of the table, faced to the right and only going forwards. A similar idea applies for the second piece of land.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std; int main()
{
int n;
cin>>n;
for(int i=1;i<=n;++i)
{
int a,b;
cin>>a>>b;
if(a==1||b==1)
{
cout<<0<<endl;
continue;
}
else
{
int js=1;
int total=0;
if(a>b)
{
while(a!=0&&b!=0)
{
if(js%2==0) a--;
else b--;
total++;
js++;
}
}
else
{
while(a!=0&&b!=0)
{
if(js%2==0) b--;
else a--;
total++;
js++;
}
}
cout<<total-1<<endl;
}
}
return 0;
}

因为没有障碍并且求最少转弯次数所以一次走完一整行或一整列就是最优解法。。。所以可以模拟为给行数或列数减一。。。

然后分析一下,画个图看一下发现当行数大于列数时先走一整行(即先给行数减一再给列数减一直至有一数为零)当行数小与列数时先走一整列(即先给列数减一再给行数减一直至有一数为零)。但上面这个代码会TLE!!

当我尝试着改成如下代码时,还是TLE!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std; int main()
{
int n;
cin>>n;
for(int i=1;i<=n;++i)
{
int a,b;
cin>>a>>b;
if(a==1||b==1)
{
cout<<0<<endl;
continue;
}
else
{
int total=0;
if(a>b)
{
while(a!=0&&b!=0)
{
a--;
if(a==0)
{
total+=1;
break;
}
b--;
if(b==0)
{
total-=1;
}
total+=2;
}
}
else
{
while(a!=0&&b!=0)
{
b--;
if(b==0)
{
total+=1;
break;
}
a--;
if(a==0)
{
total-=1;
}
total+=2;
}
}
cout<<total-1<<endl;
}
}
return 0;
}

两个的耗时好像没啥区别。。。(真是太失败了,然后我去翻了一下题解。***竟然这么简单,竟然有公式!!!)

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std; int main()
{
int n;
cin>>n;
for(int i=1;i<=n;++i)
{
int a,b;
cin>>a>>b;
cout<<2*min(a,b)-2<<endl;//公式
}
return 0;
}

洛谷P4431的更多相关文章

  1. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  2. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  3. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

  4. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

  5. 洛谷P1710 地铁涨价

    P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交  讨论  题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...

  6. 洛谷P1371 NOI元丹

    P1371 NOI元丹 71通过 394提交 题目提供者洛谷OnlineJudge 标签云端评测 难度普及/提高- 提交  讨论  题解 最新讨论 我觉得不需要讨论O long long 不够 没有取 ...

  7. 洛谷P1538迎春舞会之数字舞蹈

    题目背景 HNSDFZ的同学们为了庆祝春节,准备排练一场舞会. 题目描述 在越来越讲究合作的时代,人们注意的更多的不是个人物的舞姿,而是集体的排列. 为了配合每年的倒计时,同学们决定排出——“数字舞蹈 ...

  8. 洛谷八月月赛Round1凄惨记

    个人背景: 上午9:30放学,然后因为学校举办读书工程跟同学去书城选书,中午回来开始打比赛,下午又回老家,中间抽出一点时间调代码,回家已经8:50了 也许是7月月赛时“连蒙带骗”AK的太幸运然而因同学 ...

  9. 洛谷 P1379 八数码难题 Label:判重&&bfs

    特别声明:紫书上抄来的代码,详见P198 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给 ...

随机推荐

  1. ppt制作元素采集

    原文链接 https://www.zhihu.com/question/52157612/answer/247501754?utm_source=qq&utm_medium=social 1动 ...

  2. bash shell第一课

    自学shell,又被老师说教,上英语课不好好学习英语四级怎么过,哈哈,那也没有数据对我的吸引力大啊,为了爱与梦想!!! 回忆一下文件格式:   文件名的扩展名为 .sh 文件内容开头必为 #!bin/ ...

  3. 打造自己的.NET Core项目模板

    前言 每个人都有自己习惯的项目结构,有人的喜欢在项目里面建解决方案文件夹:有的人喜欢传统的三层命名:有的人喜欢单一,简单的项目一个csproj就搞定.. 反正就是萝卜青菜,各有所爱. 可能不同的公司对 ...

  4. 谈谈.NET Core中基于Generic Host来实现后台任务

    目录 前言 什么是Generic Host 后台任务示例 控制台形式 消费MQ消息的后台任务 Web形式 部署 IHostedService和BackgroundService的区别 IHostBui ...

  5. Java读取Excel指定列的数据详细教程和注意事项

    本文使用jxl.jar工具类库实现读取Excel中指定列的数据. jxl.jar是通过java操作excel表格的工具类库,是由java语言开发而成的.这套API是纯Java的,并不依赖Windows ...

  6. #12 Python函数

    前言 矩形的面积 S = ab,只要知道任一矩形的的长和宽,就可以带入上式求得面积.这样有什么好处呢?一个公式,适用于全部矩形,一个公式,重复利用,减少了大脑的记忆负担.像这类用变量代替不变量的思想在 ...

  7. Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现

    自Spring Cloud Alibaba发布第一个Release以来,就备受国内开发者的高度关注.虽然Spring Cloud Alibaba还没能纳入Spring Cloud的主版本管理中,但是凭 ...

  8. [转]Gitlab-CI持续集成之Runner配置和CI脚本

    本文转自:https://www.cnblogs.com/jiukun/p/7481287.html 一.简介 1. 为实现持续集成,需为该项目准备以下两样东西: 1)软件集成脚本.(gitlab-c ...

  9. .NET [MVC] 利用特性捕捉异常

    声明:本代码只适用于.NET MVC. 先创建一个类继承ActionFilterAttribute这个抽象类以及实现IExceptionFilter接口,并实现它的方法OnException. 代码如 ...

  10. 图解HTTP(1)之WEB及网络基础

    HTTP协议访问Web 当你在浏览器地址栏中键入有效URL(统一资源定位符)相应的网页页面就会展示出来,那么它是如何展示出来的, 首先这存在两个概念 ------客户端.服务器端 客户端(client ...