[Usaco2013 Feb]Taxi
来源:互联网 发布:阿里云搭建wordpress 编辑:程序博客网 时间:2024/06/05 02:00
Description
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
Bessie在农场上为其他奶牛提供出租车服务。这些奶牛已经在沿着长度为
为了节省燃气,Bessie想以尽可能少的燃料完成这次任务。
Bessie的起点是围栏的最左端,位置记为0。终点在篱笆的最右边,位置记为
Input
Line 1:
Lines 2..
Output
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.
Sample Input
2 10
0 9
6 5
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 9. The second cow wishes to go from position 6 to position 5.
Sample Output
12
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.
HINT
在0号站接第1个客户,从0号站出发到6号站(7站),放下第1个客户,接第2个客户,送到5号站再回来(2站),再接第1个客户,送到9号点(3站),共12站。(因为送完所有客户之后Bessie已在终点,故无需再走)
Source
Gold
思路
总共要走的路程=必须要走的路程+空车路程,必须要走的路程已经确定了,关键就是空车的路程。
而空车的路程总是从一个终点走到另一个起点,毕竟在一个站点抛下一头牛比在一个不是站点的地方抛下一头牛要更优。
所以这个问题就变成了如何行驶使得路程最短而且每一路段都经过了有且仅有一遍。
这样看上去多像欧拉路啊。
事实上这就是欧拉回路,先读入每一路段,再加上一段从m到0的路(为了便于构造欧拉回路)。
然后计算出所有要走的路程,再将起点升序排序,终点升序排序,最后将排序后的对应起点和终点相减的绝对值计入答案。
这样做的原因是:顺序和>乱序和>反序和,那么顺序和<乱序和<反序和,那么可以保证总共要走的路程最短。
这样就完美的解决了。
代码
#include <cstdio>#include <cmath>#include <algorithm>const int maxn=100000;int n,m,s[maxn+10],t[maxn+10];long long ans;const bool cmp(int a,int b){ return a>b;}int main(){ scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) { scanf("%d%d",&s[i],&t[i]); ans+=abs(t[i]-s[i]); } s[n+1]=m; t[n+1]=0; std::sort(s+1,s+n+2); std::sort(t+1,t+n+2); for(int i=1; i<=n+1; i++) { ans+=abs(t[i]-s[i]); } printf("%lld\n",ans); return 0;}
- [Usaco2013 Feb]Taxi
- Taxi
- Taxi
- Feb
- FEB
- 讨厌taxi
- B. Taxi
- Taxi Fare
- H - Taxi
- Lesson29 Taxi!
- Coderforces Taxi
- Taxi Fare
- [Usaco2013 Jan] Square Overlap
- [Usaco2013 Nov]Crowded Cows
- [Usaco2013 Nov]Crowded Cows
- USACO2013 island travels
- [Usaco2013 Jan]Island Travels
- bzoj3315: [Usaco2013 Nov]Pogo-Cow
- 开始使用 Spark
- Hibernate中GET和LOAD方法的区别
- 第四周-项目二—建设“单链表”算法库
- JAVA中关于修饰符的使用
- Leetcode#88. Merge Sorted Array
- [Usaco2013 Feb]Taxi
- 简单自动判断访问设备
- web开发-网络请求实战
- Git 使用规范流程
- Android Light Sensor
- 第4周 【项目二】 建设“单链表”算法库
- Eclipse部署tomcat项目不完整
- 第四周项目三(3) 判断链表是否递增
- STL中序列容器的选择