题目描述

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. TensorLayer 中文文档

    TensorLayer 中文文档 好消息 我们获得了 ACM Multimedia (MM) 年度最佳开源软件奖. TensorLayer 是为研究人员和工程师设计的一款基于Google Tensor ...

  2. 常用js方法合集

    var Default = { init: function () { }, addCookie: function (name,data) { var expdate = new Date(); / ...

  3. Qt用委托绘制需要的图形的步骤

    1.拷贝一份option: QStyleOptionViewItemV4 opt = option; 2.获取到widget,也是通过QStyleOptionViewItem &option ...

  4. PHP管理供下载的APK文件

    当我们开发的APP多的时候,把所有的APK文件统一放到一个目录中管理,是一个不错的选择: 管理的方法有很多,这里说一种: 1..创建目录结构,先创建根目录download,在根目录中创建项目目录,在项 ...

  5. 接口测试工具postman(二)创建新项目

    1.此次添加一个request,可以点击左上角的New的下拉选择Request,或者点击New弹出选项框点击Request 2.弹出新增request页面 3.添加请求的参数等 4.也可以直接添加新请 ...

  6. 类和实例属性的查找顺序 mro查找

    如果多个类继承父类,然后又被多个类继承这种复杂的问题,可以使用 mro方法 例如: class A: pass class C(D): pass class B(D): pass class A(B, ...

  7. 【SpringCloud】第一篇: 服务的注册与发现(Eureka)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  8. Docker安装Zabbix全记录

    零.Zabbix架构设计 一.docker安装mysql 查找Docker Hub上的mysql镜像: [root@10e131e69e15 ~]# docker search mysql INDEX ...

  9. GraphSAGE 代码解析(一) - unsupervised_train.py

    原创文章-转载请注明出处哦.其他部分内容参见以下链接- GraphSAGE 代码解析(二) - layers.py GraphSAGE 代码解析(三) - aggregators.py GraphSA ...

  10. python基础训练营03——字典、集合、判断、循环

    一.字典dict: 相比列表list而言,列表list像一本书,如果要查书中的某一个内容,需要把书从前往后翻一遍,直到找到想要获取的东西:而字典dict,就像现实中的字典一样,通过查找特定的字或者词( ...