洛谷P4431
题意翻译
题目大意:
给定一个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.
输入输出样例
说明
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的更多相关文章
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP
题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...
- 洛谷P1710 地铁涨价
P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交 讨论 题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...
- 洛谷P1371 NOI元丹
P1371 NOI元丹 71通过 394提交 题目提供者洛谷OnlineJudge 标签云端评测 难度普及/提高- 提交 讨论 题解 最新讨论 我觉得不需要讨论O long long 不够 没有取 ...
- 洛谷P1538迎春舞会之数字舞蹈
题目背景 HNSDFZ的同学们为了庆祝春节,准备排练一场舞会. 题目描述 在越来越讲究合作的时代,人们注意的更多的不是个人物的舞姿,而是集体的排列. 为了配合每年的倒计时,同学们决定排出——“数字舞蹈 ...
- 洛谷八月月赛Round1凄惨记
个人背景: 上午9:30放学,然后因为学校举办读书工程跟同学去书城选书,中午回来开始打比赛,下午又回老家,中间抽出一点时间调代码,回家已经8:50了 也许是7月月赛时“连蒙带骗”AK的太幸运然而因同学 ...
- 洛谷 P1379 八数码难题 Label:判重&&bfs
特别声明:紫书上抄来的代码,详见P198 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给 ...
随机推荐
- 服务器配置用户信息、ssh免密码登录和防火墙等安全配置
一.登录服务器 1.回到根目录 cd ~ 2.ssh + 用户名@服务器公网地址 ssh root@47.94.208.76 3.输入密码:注意输入法大小写 二.查看服务 ...
- java日期转化,三种基本的日期格式
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...
- Html和Css学习笔记-html进阶-html5属性
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 此篇博客是我的复习笔记,html和css学的时间太久了,忘得差不多了,最近要使用一下,所以重新打开html的书略读,后记录了标签 ...
- 学习安卓开发[1] - 程序结构、Activity生命周期及页面通信
一.程序结构 Android原生应用采用了MVC的架构设计模式,因此可以将一个Android APP中的对象归为Model.View或Controller中的一种. 具体到某个实际的APP结构中,它一 ...
- PSP总结报告
此作业的要求参见[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2556] 回顾0 alpha阶段前 团队名称:可以低头,但没必要 团队项 ...
- SQL server 远程连接不成功解决
一直以来打算自己做一个博客网站,前段时间开始准备做了,正好碰上新睿云服务器免费一年的活动,赶紧拿下.装好了sqlserver ,用本地访问没有问题,但是关键是外网访问一直不行找了好多资料最终才搞定.下 ...
- 【原】Java学习笔记015 - 面向对象
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 传递 值类型参数 ...
- NSTimer 不工作 不调用方法
比如,定义一个NSTimer来隔一会调用某个方法,但这时你在拖动textVIew不放手,主线程就被占用了.timer的监听方法就不调用,直到你松手,这时把timer加到 runloop里,就相当于告诉 ...
- 【转载】【时序约束学习笔记1】Vivado入门与提高--第12讲 时序分析中的基本概念和术语
时序分析中的基本概念和术语 Basic concept and Terminology of Timing Analysis 原文标题及网址: [时序约束学习笔记1]Vivado入门与提高--第12讲 ...
- MySql 学习之路-高级1
Mysql自学之路-高级1 目录: 1.CREATE DATABASE 创建数据库 2.CREATE TABLE 创建数据表 3.INSERT INTO SELECT 把一个表中的数据拷贝到另一个表中 ...