题面:

Problem F. Teleportation

Input file: standard input
Output file: standard output
Time limit: 15 second
Memory limit: 1024 megabytes
 
One of the farming chores Farmer John dislikes the most is hauling around lots of cow manure. In order to streamline this process, he comes up with a brilliant invention: the manure teleporter! Instead of hauling manure between two points in a cart behind his tractor, he can use the manure teleporter to instantly transport manure from one location to another.

Farmer John’s farm is built along a single long straight road, so any location on his farm can be described simply using its position along this road (effectively a point on the number line). A teleporter is described by two numbers x and y, where manure brought to location x can be instantly transported to location y, or vice versa.

Farmer John wants to transport manure from location a to location b, and he has built a teleporter that might be helpful during this process (of course, he doesn’t need to use the teleporter if it doesn’t help). Please help him determine the minimum amount of total distance he needs to haul the manure using his tractor.
 
Input
The first and only line of input contains four space-separated integers: a and b,describing the start and end locations, followed by x and y, describing the teleporter. All positions are integers in the range 0...100, and they are not necessarily distinct from each-other.
 
Output
Print a single integer giving the minimum distance Farmer John needs to haul manure in his tractor.
 
Example
Input
3 10 8 2
Output
3
 
Note
In this example, the best strategy is to haul the manure from position 3 to position 2, teleport it to position 8, then haul it to position 10. The total distance requiring the tractor is therefore 1 + 2 = 3.
 

题目描述:

农夫最不喜欢清理牛粪,于是发明了牛粪传送器:可以使牛粪从一个地方直接传送到另一个地方,而不是从一个地方用推车搬运牛粪到另一个地方。农夫的农场可以想象为一条长直道。现在,给出农夫要把牛粪从a运送到b,还有传送器可以从x传送到y或者y传送到x,问农夫要用推车搬运牛粪的最少距离。
 

题目分析:

这道题关键要理解这个“距离”:如果是两点通过传送器之间的距离则这个距离不算,比如传送器可以把牛粪从x传送到y,而距离y-x就不能算进答案。如果是从传送器到终点的距离则计算在内。简单的来说,假如农夫1步走1个单位的距离,就是算农夫把牛粪从a运到b的最小步数;通过传送门时,由于直接传送到另一个地点,农夫要走的步数为0。
 
接下来,我们来分情况讨论:
1.农夫不通过传送门:
2.农夫通过传送点x:
3.农夫通过传送点y:
最终,我们只需要取这三种情况的最小值就可以了。
 
 
AC代码:
 1 #include <cstdio>
2 #include <iostream>
3 #include <algorithm>
4 #include <cmath>
5 using namespace std;
6
7 int main(){
8 int a, b, x, y;
9 cin >> a >> b >> x >> y;
10
11 int res = 1e9; //设为"无穷大"
12 res = min(res, abs(a-b)); //第一种情况
13 res = min(res, abs(a-x)+abs(b-y)); //第二种情况
14 res = min(res, abs(a-y)+abs(b-x)); //第三种情况
15
16 cout << res << endl;
17 return 0;
18 }
 
 

最新文章

  1. 字符串反转----将this is good 转化为good is this 输出。
  2. RSA3:预提取数据
  3. SpellTime
  4. 单元测试实战 - 如何使用Eclipse
  5. systemstate dump 介绍
  6. 14种网页图片和文字特效的jQuery插件代码
  7. 基于jQuery带备忘录功能的日期选择器
  8. 解决nginx负载均衡的session共享问题
  9. 程序生成SiteMapPath文件
  10. Java中的异常处理(三) - 自定义异常处理
  11. Linux 套接字编程中的 5 个隐患
  12. Android_消息机制
  13. 10382 - Watering Grass
  14. ADO.Net连接模式
  15. Js中有关变量声明和函数声明提升的问题
  16. Jmeter中正则表达式提取器使用详解
  17. C# Ioc、DI、Unity、TDD的一点想法和实践
  18. 【集训队作业2018】取名字太难了 任意模数FFT
  19. Win10下python不同版本同时安装并解决pip共存问题
  20. solr查询工作原理深入内幕

热门文章

  1. mybaits(七)spring整合mybaits
  2. Spring(三) Spring IOC 初体验
  3. 使用nodejs爬取图片
  4. μC/OS-III---I笔记3---时间管理
  5. canvas画布基本知识点总结
  6. You Don&#39;t Know Chrome Features
  7. Alexa website ranking
  8. Flutter 1.17.x
  9. macOS 升级后导致 dart bug
  10. Free Serverless