题目描述

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. (转载)深入super,看Python如何解决钻石继承难题

    1.   Python的继承以及调用父类成员 python子类调用父类成员有2种方法,分别是普通方法和super方法 假设Base是基类 class Base(object): def __init_ ...

  2. java使用java.util.Properties读取properties文件的九种方法

    直接上代码: package com.test.test; import java.io.BufferedInputStream; import java.io.FileInputStream; im ...

  3. Spring MVC - URL路径映射

    1. 普通映射 A. @RequestMapping("/test1") B. @RequestMapping(value={"/test1", "/ ...

  4. PHP批量替换MySql数据库中的数据内容

    <?php //替换数据库内容类 class replace{ public $dbAddress; //数据库地址 public $dbUser; //数据库用户名 public $dbPwd ...

  5. (转) Unreal Engine 4 Custom Shaders Tutorial

    说明: 1.这里的Custom Shaders 为且仅为 Custom Node的使用和USF的包含.并非全局Shader和Material Shader. 2.原文来源:https://www.ra ...

  6. LeetCode - 67. Add Binary(4ms)

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  7. Python3 初识Python

    一 Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  8. BZOJ 3597 SCOI2014 方伯伯送椰子 网络流分析+SPFA

    原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3597 Description 四川的方伯伯为了致富,决定引进海南的椰子树.方伯伯的椰子园十 ...

  9. Linux SPI总线和设备驱动架构之三:SPI控制器驱动

    通过第一篇文章,我们已经知道,整个SPI驱动架构可以分为协议驱动.通用接口层和控制器驱动三大部分.其中,控制器驱动负责最底层的数据收发工作,为了完成数据的收发工作,控制器驱动需要完成以下这些功能:1. ...

  10. mysql 数据包太小会引发错误信息

    Error querying database.  Cause: com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for quer ...