洛谷题目链接:[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的更多相关文章

  1. P3076 [USACO13FEB]出租车Taxi

    题目描述 Bessie is running a taxi service for the other cows on the farm. The cows have been gathering a ...

  2. 出租车(taxi)

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

  3. OO Summary Ⅱ

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

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

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

  5. 解题:USACO13FEB Taxi

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

  6. [bzoj3062][Usaco13Feb]Taxi_贪心

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

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

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

  8. A cost-effective recommender system for taxi drivers

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

  9. poj 2060 Taxi Cab Scheme (二分匹配)

    Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 D ...

随机推荐

  1. 关于ES6-{块级作用域 let const 解构赋值 数组 字符串 函数的扩展 箭头函数}

    关于ES6 块级作用域 任何一对花括号({})中的语句集都属于一个块,在块中声明的变量在代码块外都是不可访问的,称之为块级作用域,ES5以前没有块级作用域 let let 是ES6新增的声明变量的一种 ...

  2. Thunder团队第六周 - Scrum会议2

    Scrum会议2 小组名称:Thunder 项目名称:i阅app Scrum Master:宋雨 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...

  3. c#笔记整理 关于继承与多态等

    [ 塔 · 第 二 条 约 定 ] c#面向对象基础 整理private.protected.public.abstract等的异同 public 公有访问.不受任何限制. private 私有访问. ...

  4. 【week2】四人小组项目(WBS、NABCD)

    项目选题:东北师范大学论坛 小组名称:nice! 项目组长:李权 组员:于淼 刘芳芳 杨柳 本周任务:要求给出需求概述.功能列表.痛点或亮点.NABCD及WBS模型在此项目中的应用. 作为东北师范大学 ...

  5. centOS 6.5命令方式配置静态IP

    想自己做个centOS玩一下,然后通过FTP访问操作,首先查看是否开启了SSH,命令如下: rpm -qa | grep ssh 这个时候看到的是centOS的ssh已经打开!要是通过FTP工具访问还 ...

  6. centos升级python(从2.6.6升级到2.7.8)

    ***先安装readline,否则升级后python回退和方向键不能使用 yum install readline-devel.x86_64   1.#wget www.python.org/ftp/ ...

  7. dev_queue_xmit 发生了什么?skb还会在哪里缓存

    见 codebox/net/qdisk/xmit.log中保存了一份记录 调用关系 sch_direct_xmit --> dev_hard_start_xmit --> xmit_one ...

  8. 【bzoj1441】Min 扩展裴蜀定理

    题目描述 给出n个数(A1...An)现求一组整数序列(X1...Xn)使得S=A1*X1+...An*Xn>0,且S的值最小 输入 第一行给出数字N,代表有N个数 下面一行给出N个数 输出 S ...

  9. sql批量更新关系型数据库

    更改gb_groupd里的chargingrulesname的值UPDATE   tb_group SET tb_group.chargingrulesname =tb_chargingrules.c ...

  10. hdu 2768 Cat vs. Dog (二分匹配)

    Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...