Cattle off the holiday season team 1B. Poo Portal (a)

Links: https://ac.nowcoder.com/acm/contest/918/B

Meaning of the questions:

Farmer John's farm was the most annoying transport of cow dung. To streamline the process, he created a great invention: poo Portal! With the use of a tractor towing carts filled with cow dung compared from one location to another location, he can use the portal to poo dung from one location to another location instantly.
Farmer John's farm was built along a long straight road, so each point on the farm he could simply be represented (the equivalent of a point on the number line) with the location of the place on the road. A portal can use two numbers x and y, x Drag manure is transferred to the place instantaneously y location, and vice versa.

Farmer John wants to place a cow from the transport to the location b, he built a might be helpful to the process portal (Of course, if there is no help, he can not). Please help him find his tractor requires a minimum total distance transport of cow dung.

Ideas:

A total of three kinds of moves, a-> x-> y-> ba- > y-> x-> b, a-> b.
The minimum you can take the walk.

Code:

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long LL;
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
int n, m, k, t;
 
int main()
{
    int a, b, x, y;
    cin >> a >> b >> x >> y;
    if (a > b)
        swap(a, b);
    if (x > y)
        swap(x, y);
    int len1 = b-a;
    int ax = abs(a-x), ay = abs(a-y), bx = abs(b-x), by = abs(b-y);
    int len2 = min(ax+by, ay+bx);
    cout << min(len1, len2) << endl;
 
    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/10995664.html
Recommended