出租车(taxi)
出租车(taxi)
题目描述
Bessie在农场上为其他奶牛提供出租车服务。这些奶牛已经在沿着长度为M(1<= M <= 1,000,000,000)的栅栏上不同的地点聚集等候。不幸的是,他们已经厌倦了他们当前所在的位置并且每只奶牛都想要沿着栅栏去别的地方走走。 Bessie必须赶到这些奶牛的起始位置,并把他们带到它们的目的地。Bessie的车很小,所以她只能一次只能搭载一头奶牛。奶牛可以在同一时刻完成上车和下车。
为了节省燃气,Bessie想以尽可能少的燃料完成这次任务。N只奶牛的起始位置和结束为止都是已知的(1 <= N <=100000),请确定Bessie的最少行程。Bessie意识到,要使所得到的行程最短,Bessie可能将在沿途中让奶牛上车或下车而并不一定将一头奶牛从起点直接送到中点。
Bessie的起点是围栏的最左端,位置记为0。终点在篱笆的最右边,位置记为M。
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.
输入
从文件taxi.in中输入数据
第1行:两个整数 N和M。
第2...N+1行:第(i+1)行包含两个整数,s_i和t_i(0<=s_i,t_i<=M),表示第i只奶牛的起始位置和目的地位置。
* 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
INPUT DETAILS: 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
输出
输出到文件taxi.out中
输出仅一行,包含一个整数,表示Bessie所需的最短行程(这个行程不一定能用32bit的整型储存)。
* 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.
OUTPUT DETAILS: 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.
样例输入
2 10
0 9
6 5
样例输出
12
solution
首先我们一定要走的是每段路程(硬性)
可以用方案改变的为跑空车的路程
那么问题就是怎么跑空车最优
把S,T从小到大排序,加一条m->1的路(bessie 1开始m结束)
每次从ti走到si一定最优
思路来自神犇https://oi.men.ci/bzoj-3062/
#include
#include
#include
#include
#include
#include
#define maxn 100005
using namespace std;
int n,m,s[maxn],t[maxn];
long long ans;
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++){
scanf("%d%d",&s[i],&t[i]);
ans=ans+abs(s[i]-t[i]);
}
s[0]=m;t[0]=0;
sort(s,s+n+1);sort(t,t+n+1);
for(int i=0;i<=n;i++){
ans=ans+abs(s[i]-t[i]);
}
cout<