1. 程式人生 > >[USACO13FEB]出租車Taxi

[USACO13FEB]出租車Taxi

line 但是 term blog code amount sport save ane

洛谷題目鏈接:[USACO13FEB]出租車Taxi

題目描述

Bessie is running a taxi service for the other cows on the farm. The cows have been gathering at different locations along a fence of length M (1 <= M <= 1,000,000,000). Unfortunately, they have grown bored with their current locations and each wish to go somewhere else along the fence. Bessie must pick up each of her friends at their starting positions and drive them to their destinations. Bessie‘s car is small so she can only transport one cow in her car at a time. Cows can enter and exit the car instantaneously.

To save gas, Bessie would like to minimize the amount she has to drive. Given the starting and ending positions of each of the N cows (1 <= N <= 100,000), determine the least amount of driving Bessie has to do. Bessie realizes that to save the most gas she may need to occasionally drop a cow off at a position other than her destination.

Bessie starts at the leftmost point of the fence, position 0, and must finish her journey at the rightmost point on the fence, position M.

長度為m的柵欄上,有n頭牛需要坐車前往別的地方,起點和終點分別為a_i和b_i。現在出租車從最左端0出發,要運送完所有牛,最後到達最右端m,求最小路程。

輸入輸出格式

輸入格式:

  • Line 1: N and M separated by a space.

  • Lines 2..1+N: The (i+1)th line contains two space separated

integers, s_i and t_i (0 <= s_i, t_i <= M), indicating the starting position and destination position of the ith cow.

輸出格式:

  • Line 1: A single integer indicating the total amount of driving Bessie must do. Note that the result may not fit into a 32 bit integer.

輸入輸出樣例

輸入樣例#1:

2 10
0 9
6 5

輸出樣例#1:

12

說明

There are two cows waiting to be transported along a fence of length 10. The first cow wants to go from position 0 (where Bessie starts) to position 9. The second cow wishes to go from position 6 to position 5.

Bessie picks up the first cow at position 0 and drives to position 6. There she drops off the first cow, delivers the second cow to her destination and returns to pick up the first cow. She drops off the first cow and then drives the remainder of the way to the right side of the fence.

首先當然是要安利一波five20巨佬的博客

一句話題意:\(n\)頭奶牛的行程,以及它們的起點和終點.每個時刻只能帶一個奶牛,現在要求從0出發,送每個奶牛從起點出發到終點,最終到\(m\)位置所需要的最少的路程.

題解: 首先可以想到,要送到一只奶牛,至少要從它的起點走到終點.那麽該如何求出除了必須走的路程以外還要額外走的路程呢?

如果我們不考慮每頭牛走到它自己的終點,只要它走到最近的終點,那麽應該怎麽做呢?顯然可以直接將0加入起點,\(m\)加入終點,將起點和終點排序,這樣算出的就是最小要走的距離.

那麽如果每頭牛都有它自己固定的起點終點呢?其實也是一樣的,因為每個時刻我們車上都可以帶一頭牛,所以可以現將每頭牛都先送到最近的終點,那麽現在的情況雖然並不是題目要求的答案,但是每次我送一頭牛到它應該到的位置的時候可以把現在在這個終點的那頭牛接上車.然後再送這頭牛回到它要去的終點.

該怎麽理解上面這段話呢?打個栗子,這裏引用一下520的圖:
技術分享圖片

假設牛\(i\)的起點為s=2,終點為t=8,牛\(j\)的起點為s=4,終點為t=6,那麽我們模擬一下送這兩頭牛的過程:

  • 從0出發,到2的位置接牛\(i\).
  • 帶著牛\(i\)到4位置,放下牛\(i\),接牛\(j\)送到8
  • 從8位置回到4接牛\(i\)
  • \(i\)送到終點後到m=10位置.

當然因為是從0出發,最後要到\(m\),所以要先將0加入終點,\(m\)加入起點,然後排序算出的結果可以發現是一樣的,實際上也可以證明這個是正確的.

最後記得結果開long long.

#include<bits/stdc++.h>
using namespace std;
const int N=100000+5;
typedef long long lol;

int n, m, a[N], b[N];
lol ans = 0;

int gi(){
    int ans = 0, f = 1; char i = getchar();
    while(i<'0'||i>'9'){if(i=='-')f=-1;i=getchar();}
    while(i>='0'&&i<='9'){ans=ans*10+i-'0';i=getchar();}
    return ans * f;
}

int main(){
    //freopen("data.in","r",stdin);
    n = gi(); m = gi();
    for(int i=1;i<=n;i++)
        a[i] = gi(), b[i] = gi(), ans += abs(b[i]-a[i]);
    a[0] = m, b[0] = 0;
    sort(a, a+n+1); sort(b, b+n+1);
    for(int i=0;i<=n;i++) ans += abs(a[i]-b[i]);
    printf("%lld\n",ans);
    return 0;
}

[USACO13FEB]出租車Taxi