题目描述

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.

Solution:

  本题是一道很值得做的贪心。

  首先,每一段的出发点到目标点的距离是一定要走的,所以$ans$可以先累加每一段的距离。

  然后,我们不难想到尽可能的走有用的路(即尽可能的让牛在车上),但是由于最多载一个牛,所以一定有路程是不载牛的。

  我们画画图,不难发现,每次回头的路程最少的情况,是从上一个点的终点到离他最近的起点的距离。

  转换思路(类似于蚂蚁那道题的思路),每头牛都要从它所在起点走到一个终点然后消失同时该终点失效,相遇停下来等同于不停而是继续走到下一个最近的终点。

  因为牛都是一样的,我们可以理解为当两头牛相遇在某一起点,另一头牛代替原牛走到最近的终点(然后就消失了,此终点失效),再回头载上个起点的牛往前走。发现每次回头的是当前最小的终点到当前最小的起点的距离,又由于从$0$出发要走到$m$,于是我们将$0$加入终点中,$m$加入起点中,从小到大排序,$ans$累加起点减去终点(取绝对值)。

  贴张图理解一下:

  

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
using namespace std;
const int N=;
int gi(){
int a=;char x=getchar();
while(x<''||x>'')x=getchar();
while(x>=''&&x<='')a=a*+x-,x=getchar();
return a;
}
int n,m,x[N],y[N];
ll ans;
int main(){
n=gi(),m=gi();
For(i,,n)x[i]=gi(),y[i]=gi(),ans+=abs(x[i]-y[i]);
x[++n]=m,y[n]=;
sort(x+,x+n+),sort(y+,y+n+);
For(i,,n)ans+=abs(x[i]-y[i]);
cout<<ans;
return ;
}

P3076 [USACO13FEB]出租车Taxi的更多相关文章

  1. [USACO13FEB]出租车Taxi

    洛谷题目链接:[USACO13FEB]出租车Taxi 题目描述 Bessie is running a taxi service for the other cows on the farm. The ...

  2. 出租车(taxi)

    出租车(taxi) 题目描述 Bessie在农场上为其他奶牛提供出租车服务.这些奶牛已经在沿着长度为M(1<= M <= 1,000,000,000)的栅栏上不同的地点聚集等候.不幸的是, ...

  3. OO Summary Ⅱ

    [第五次作业——多线程电梯] 类图 度量 协作图 设计分析: 多线程电梯是我第一次接触多线程,因此真的是无(瞎)从(g)下(2)手(写),感觉仿佛只是用一个调度器来调度3部电梯但又总觉得好像哪里不太对 ...

  4. English-旅游英语及情景对话

    1.旅游英语:预订机票情景对话及常用句型 目前,越来越多的人都选择以飞机为出行方式.但是如何用一口流利的英语订机票呢?这里我们替你总结了一些情景对话,还有一些常用的句型.大家都来学一学吧~A:Good ...

  5. 【洛谷P3076】Taxi

    这道题值得好好想一会 我们通过对一些小数据的手算,以及对于每段路程的拆分,可以发现: 1.每个st对应的ed这段路程无论如何都要算上 2.额外还要计算的一段路程,就是"切换"费用 ...

  6. 解题:USACO13FEB Taxi

    题面 因为每次只能载一头牛,所以总路程=每头牛的距离+回头路的最短距离,于是问题变成了如何求回头路的最短距离 我们可以把起点和终点存在两个数组里,然后将两个数组排序后取对应位置相减的绝对值就是每次走回 ...

  7. [bzoj3062][Usaco13Feb]Taxi_贪心

    Taxi bzoj-3062 Usaco13Feb 题目大意:有n个奶牛想坐出租车.第i头奶牛在起点a[i]等候,想坐出租车到b[i].Bessie从0出车,车上只能坐一头奶牛.她必须完成所有奶牛的要 ...

  8. 【HDU1960】Taxi Cab Scheme(最小路径覆盖)

    Taxi Cab Scheme Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. A cost-effective recommender system for taxi drivers

    一个针对出租车司机有效花费的推荐系统 摘要 GPS技术和新形式的城市地理学改变了手机服务的形式.比如说,丰富的出租车GPS轨迹使得出做租车领域有新方法.事实上,最近很多工作是在使用出租车GPS轨迹数据 ...

随机推荐

  1. 【Keras案例学习】 sklearn包装器使用示范(mnist_sklearn_wrapper)

    import numpy as np from keras.datasets import mnist from keras.models import Sequential from keras.l ...

  2. Django-Content-type用法

    from django.db import models from django.contrib.contenttypes.models import ContentType from django. ...

  3. 可用率map处理

    total_data =[ {'event_current_dealer': '陈铁', 'id__count': 66}, {'event_current_dealer': '丁凯', 'id__c ...

  4. abo dto属性验证的坑

    问题回现: public class ShipmentRequestDto { public string FromPhoneNumber { get; set; } /// <summary& ...

  5. 6.0 实现app登录

    1.0.0:学习ui自动化准备工作 待测app,我这里有准备两个apk,这两个都是我曾经做过的项目,后续的文章都是基于这两个app! 链接:https://pan.baidu.com/s/1I0vR9 ...

  6. linux学习总结---web前端③

    css3: border-radius opacity: transform: 旋转 rotate 平移translate 缩放scale 扭曲skew transition:转变 强调:开始和结束 ...

  7. Sublime Text 3配置 Python3 开发环境

    来自 https://www.cnblogs.com/zhangqinwei/p/6886600.html Sublime Text作为一款支持多种编程语言的文本编辑神器,深受广大开发者的喜爱.通过简 ...

  8. truffle的调用nodeJs的问题

    Truffle3.0集成NodeJS并完全跑通(附详细实例,可能的错误) 升级到Truffle3.0 如果之前安装的是Truffle2.0版本,需要主动升级到Truffle3.0,两者的语法变化有点大 ...

  9. MongoDB的复制一:复制的原理

    1.复制的角色 复制有三种角色: primay:主库,执行所有的写操作,并把日志写入oplog里. secondary:复制主库的所有操作.读取主库的oplog,并执行日志里的内容.默认情况下,客户端 ...

  10. [leetcode-651-4 Keys Keyboard]

    Imagine you have a special keyboard with the following keys: Key 1: (A): Prints one 'A' on screen. K ...