P3076 [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.
输入输出样例
2 10
0 9
6 5
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的更多相关文章
- [USACO13FEB]出租车Taxi
洛谷题目链接:[USACO13FEB]出租车Taxi 题目描述 Bessie is running a taxi service for the other cows on the farm. The ...
- 出租车(taxi)
出租车(taxi) 题目描述 Bessie在农场上为其他奶牛提供出租车服务.这些奶牛已经在沿着长度为M(1<= M <= 1,000,000,000)的栅栏上不同的地点聚集等候.不幸的是, ...
- OO Summary Ⅱ
[第五次作业——多线程电梯] 类图 度量 协作图 设计分析: 多线程电梯是我第一次接触多线程,因此真的是无(瞎)从(g)下(2)手(写),感觉仿佛只是用一个调度器来调度3部电梯但又总觉得好像哪里不太对 ...
- English-旅游英语及情景对话
1.旅游英语:预订机票情景对话及常用句型 目前,越来越多的人都选择以飞机为出行方式.但是如何用一口流利的英语订机票呢?这里我们替你总结了一些情景对话,还有一些常用的句型.大家都来学一学吧~A:Good ...
- 【洛谷P3076】Taxi
这道题值得好好想一会 我们通过对一些小数据的手算,以及对于每段路程的拆分,可以发现: 1.每个st对应的ed这段路程无论如何都要算上 2.额外还要计算的一段路程,就是"切换"费用 ...
- 解题:USACO13FEB Taxi
题面 因为每次只能载一头牛,所以总路程=每头牛的距离+回头路的最短距离,于是问题变成了如何求回头路的最短距离 我们可以把起点和终点存在两个数组里,然后将两个数组排序后取对应位置相减的绝对值就是每次走回 ...
- [bzoj3062][Usaco13Feb]Taxi_贪心
Taxi bzoj-3062 Usaco13Feb 题目大意:有n个奶牛想坐出租车.第i头奶牛在起点a[i]等候,想坐出租车到b[i].Bessie从0出车,车上只能坐一头奶牛.她必须完成所有奶牛的要 ...
- 【HDU1960】Taxi Cab Scheme(最小路径覆盖)
Taxi Cab Scheme Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- A cost-effective recommender system for taxi drivers
一个针对出租车司机有效花费的推荐系统 摘要 GPS技术和新形式的城市地理学改变了手机服务的形式.比如说,丰富的出租车GPS轨迹使得出做租车领域有新方法.事实上,最近很多工作是在使用出租车GPS轨迹数据 ...
随机推荐
- 实用脚本 1 -- 安装Ctags
Ctags是vim下方便代码阅读的工具,一般VIM中已经默认安装了Ctags,它可以帮助程序员很容易地浏览源代码. 1.如果系统中没有此工具用如下方法安装: 到ctags官网下载源码,解压后 ...
- Entity Framework + WCF 远程调用出错
在使用Entity Framework中使用WCF,在程序中调用服务一直报错,我一直以为是WCF的哪个地方的配置有问题,找来找去,一直没有解决. 最后在网上找到一篇文章 ...
- 理解JAVA常量池
下面是一些String相关的常见问题: String中的final用法和理解final StringBuffer a = new StringBuffer("111");final ...
- 「题目代码」P1007~P1012(Java)
1007 C基础-计负均正 import java.util.*; import java.io.*; public class Main { public static void main(Stri ...
- LeetCode 707 ——设计链表
1. 题目 2. 解答 用一个单链表来实现,只有一个头指针.因为不能建立哨兵结点,因此要特别注意是否在头结点处操作. class MyLinkedList { public: struct ListN ...
- Week1 Team Homework #1 from Z.XML-总结学长经验教训
谭传奇学长: 我们的弯路可能是,一开始没有从最基础的部分开始迭代开发,一开始就想的太远了一些,每一步开的有点太大了,所以可能有些东西最后就连不上,也没有能够按时完成.如果可以先做出一个能用的版本,然后 ...
- Java 多态方法构造器执行方法
我们参考下面这个例子: 读者可以提前考虑一下,这段程序的输出会是什么. public class Polymorphism { /** * 创建一个类A * 该类中有一个方法draw,以及一个构造方法 ...
- Dispose的调用顺序
非托管资源的释放顺序. 这是应该先释放 reader 再释放 stream. 或者直接使用using,防止出错 .
- (转) linux I/O优化 磁盘读写参数设置
关于页面缓存的信息,可以用cat /proc/meminfo 看到.其中的Cached 指用于pagecache的内存大小(diskcache-SwapCache).随着写入缓存页,Dirty 的值会 ...
- js阻止冒泡事件和默认事件的方法
阻止默认事件 function stopDeFault(e){ if(e&&e.preventDefault){//非IE e.preventDefault(); }else{//IE ...