Luo Gu P3076 [USACO13FEB] Taxi Taxi

topic:

M is the length of the fence, an n cows need car to other places, the beginning and end, respectively a_i and b_i. Now a taxi starting from the leftmost 0, to complete delivery of all cattle, and finally reaches the rightmost m, the minimum required distance. Taxi carrying a cow only once.

Problem-solving ideas:

1. Each st corresponding ed this journey anyway count

2. Additional journey also calculated, is the "switching" costs

What is the cost to switch it?

We know that there may be such a position st, when the position is reached, to have been thrown down, and carrying on where the cow in the cattle car i j and transported it to its destination and come back the cattle ed i upload

Then we need to be added to each and abs (ed-st) of

The specific implementation, we need to be added at the end of m st, ed, the starting point 0,

It is to be noted that the added 0 of ed, m added in st

The reason is that we need to go to the nearest st from the starting point 0, m come from the farthest ed

Therefore, 0 is the corresponding min_st, 0 a first bound of ed; m is the same

Finally st, ed sorting, enumeration add up to a

A c c e p t e d   c O d e : Accepted\ code:

#include<cstdio>
#include<algorithm>

int n, m;
int s[100005], t[100005];
long long ans;

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i) {
        scanf("%d %d", &s[i], &t[i]);
        ans += abs(t[i] - s[i]);
    }
    s[n+1] = m;
    t[n+1] = 0;
    std::sort(s+1, s+n+2);
    std::sort(t+1, t+n+2);
    for (int i = 1; i <= n + 1; ++i)
        ans += abs(t[i] - s[i]);
    printf("%lld\n", ans);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_39798042/article/details/89380777