Teleportation [codeforces] (贪心)

原题传送门


题面:

Teleportation

                            time limit per test : 15 seconds
                         memory limit per test : 1024 megabytes
                                  input : standard input
                                 output : standard output

Problem Description

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.

Sample Input

3 10 8 2

Sample Output

3

题意描述

John想偷懒,在两个地方设置了传送门,通过传送门移动的距离不用计入John实际走动距离,求出John能从起点到终点的最短实际走动距离.

题目分析

贪心.因为终点和起点距离可能会很小,而起点和终点距离两个门的距离可能远大于起点和终点的距离,所以只需要比较John走传送门和不走传送门的距离哪个大,然后选择距离小的即可.

具体代码

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a, b, x, y;
    scanf("%d%d%d%d", &a, &b, &x, &y);
    int ss = min(a , b);
    int ee = max(a , b);
    int ts = min(x , y);
    int te = max(x , y);
    int ans = min( abs(ts-ss)+abs(te-ee) , ee-ss );
    printf("%d\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43726593/article/details/88230523
今日推荐