出租车(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<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#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<<ans<<endl;
return 0;
}

出租车(taxi)的更多相关文章

  1. [USACO13FEB]出租车Taxi

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

  2. P3076 [USACO13FEB]出租车Taxi

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

  3. OO Summary Ⅱ

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

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

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

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

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

  6. A cost-effective recommender system for taxi drivers

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

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

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

  8. [ACM_数学] Taxi Fare [新旧出租车费差 水 分段函数]

    Description Last September, Hangzhou raised the taxi fares. The original flag-down fare in Hangzhou ...

  9. poj 2060 Taxi Cab Scheme (最小路径覆盖)

    http://poj.org/problem?id=2060 Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submi ...

随机推荐

  1. 如何从SqlDataReader中获取DataRow

    1.前言使用SqlDataReader较之SqlDataAdapter速度上快,因为读取记录是一行一行的来,但是往往也感觉那么蹩脚.例如经常用到的如何从SqlDataReader中获取DataRow呢 ...

  2. cf492E. Vanya and Field(扩展欧几里得)

    题意 $n \times n$的网格,有$m$个苹果树,选择一个点出发,每次增加一个偏移量$(dx, dy)$,最大化经过的苹果树的数量 Sol 上面那个互素一开始没看见,然后就GG了 很显然,若$n ...

  3. 第三篇:彻底解决ssh.invoke_shell() 返回的中文问题

    接上一篇,前两篇解决中文的问题主要是在字符集上做的手脚,即将中文转成英文,但是有一种情况我们都来不及做转换,即登录时服务器直接返回了中文内容: 此时程序报了如下错误,其实还是字符集问题: 为此:我们可 ...

  4. (原创)task和function语法的使用讨论(Verilog,CPLD/FPGA)

    1. Abstract function和task语句的功能有很多的相似之处,在需要有多个相同的电路生成时,可以考虑使用它们来实现.因为个人使用它们比较少,所以对它们没有进行更深的了解,现在时间比较充 ...

  5. [Poj3281]Dining(最大流)

    Description 有n头牛,f种食物,d种饮料,每头牛有nf种喜欢的食物,nd种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种食物了,饮料也同理,问最多有多少头牛可以吃到它喜欢的 ...

  6. 字符编码,ASCII、Unicode与UTF-8的理解

    首先我们先要明白的两点是:1.计算机中的信息都是由二进制的0和1储存的:2.我们再计算机屏幕上看到的各种字符都是计算机系统按照一定的规则将二进制数字转换而来的. 一.基本概念. 1.字符集(chars ...

  7. HDU 2087 HDU 1867 KMP标准模板题

    贴两道题,其中HDU2087是中文题,故不解释题目, 思路是,一发KMP,但是特别处理最后一位的失配边为0,这样就可以保证“判断完成但是不多判断”. 第二题,很毒瘤的题,要求求出,给定字符串A,B能够 ...

  8. callback回调函数【转】

    请给作者点赞--> 原文链接 什么是回调函数? 我们绕点远路来回答这个问题. 编程分为两类:系统编程(system programming)和应用编程(application programmi ...

  9. POJ 3041 Asteroids (二分图最小点覆盖集)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24789   Accepted: 13439 Descr ...

  10. python上数据存储 .h5格式或者h5py

    最近在做城市计算的项目,数据文件是以.h5的格式存储的,总结下其用法和特点 来自百度百科的简介: HDF(Hierarchical Data Format),可以存储不同类型的图像和数码数据的文件格式 ...