#P1639. Manure Transportation Optimization

    ID: 14925 Type: Default 1000ms 256MiB

Manure Transportation Optimization

Manure Transportation Optimization

Farmer John hates transporting cow manure by tractor. To make his work easier, he invented a manure teleportation portal. The portal connects two points on his farm, which is built along a straight road. Any location on the farm can be represented by its coordinate on a number line. The portal is described by two numbers \(x\) and \(y\): manure delivered to \(x\) can be teleported to \(y\) and vice versa.

Farmer John wants to move manure from location \(a\) to location \(b\). He built one portal which may help reduce the distance that the tractor needs to travel. Of course, if using the portal does not help, he can simply transport the manure directly by tractor.

The task is to compute the minimum total distance that the tractor must travel. The approach is to compare the direct distance \(|a - b|\) with the distances obtained by using the portal in either direction:

[ \min\Big( |a - b|, ; |a - x| + |b - y|, ; |a - y| + |b - x| \Big) ]

inputFormat

The input consists of four integers \(a\), \(b\), \(x\), and \(y\) separated by spaces, where:

  • \(a\): starting location.
  • \(b\): destination location.
  • \(x\) and \(y\): the two endpoints of the portal.

outputFormat

Output a single integer representing the minimum distance the tractor must drive.

sample

5 10 2 8
5