1658: [Usaco2006 Mar]Water Slides 滑水

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 236  Solved: 157
[Submit][Status][Discuss]

Description

It's a hot summer day, and Farmer John is letting Betsy go to the water park where she intends to ride every single slide. The water park has N (1 <= N <= 10,000) platforms (numbered 1..N) from which to enter the M (1 <= M <= 10,000) water slides. Each water slide starts at the top of some platform and ends at the bottom of some platform (possibly the same one). Some platforms might have more than one slide; some might not have any. The park is very thin, so the platforms lie along a straight line, each platform at a position Xi (0 <= Xi <= 100,000) meters from one end of the park. One walks from one platform to the next via a sidewalk parallel to the line of platforms.The platforms of the water park are weakly connected; that is, the park cannot be divided into two sets of platforms with no slides running between the two sets. Both the entrance and exit to the park are at platform 1, so Betsy will start and end there. In order to spend more time on the slides, Betsy wants to walk as little as possible. Find the minimum distance Betsy must travel along the ground in order to try every slide in the park exactly once without repeating.

炎热的夏日里,约翰带贝茜去水上乐园滑水.滑水是在一条笔直的人工河里进行的,沿河设有N(1≤N≤10000)个中转站,并开通了M(1≤M≤10000)条滑水路线.路线的起点和终点总在某个中转站上,起点和终点可能相同.有些中转站可能是许多条路线的起点或终点,而有些站则可能没有在任何路线里被用上.贝茜希望能把所有的路线都滑一遍.所有中转站排成一条直线,每个中转站位于离河的源头Xi(0≤Xi≤100000)米处.沿着河边的人行道,贝茜可以从任意位置走到任意一个中转站.    中转站与滑水路线的布局满足下述的性质:任意两个中转站之间都有滑水路线直接成间接相连.水上乐园的入口与出口都在1号中转站旁,也就是说,贝茜的滑水路线的起点和终点都是1号中转站.

为了更好地享受滑水的快乐,贝茜希望自己花在走路上的时间越少越好.请你帮她计算一下,如果按她的计划,把所有的路线都不重复地滑一遍,那她至少要走多少路.

Input

* Line 1: Two integers, N and M.

* Lines 2..N+1: Line i+1 contains one integer, Xi, the position of platform i. * Lines N+2..M+N+1: Line i+N+1 contains two integers, Si and Di, respectively the start and end platforms of a slide.

第1行:两个整数N和M,用空格隔开.

第2到N+1行:第i+l行包括一个整数Xi,表示i号中转站距河源头的距离.

第N+2到M+N+1行:第i+N+1行包括两个整数Si和Di,分别表示了一条滑水路线的起点和终点.

Output

* Line 1: One integer, the minimum number of meters Betsy must walk.

输出一个整数,即贝茜要走的路程长度至少为多少米

Sample Input

5 7
5
3
1
7
10
1 2
1 2
2 3
3 1
4 5
1 5
4 1

Sample Output

8

val[x]表示x点的入度与出度差(这个x是坐标,不是中转站(0<=x<=100000))

如果val[x]>0那么肯定要通过步行到达x点val[x]次

如果val[x]<0那么肯定要从x点步行出发val[x]次

很显然∑val[i]==0,最优解肯定是对于每个val[x]>0的x,找目前最近的val[x']<0的x',

从x'走到x,然后val[x]--, val[x']++

统计下val[x]然后拿双指针遍历一下就好

复杂度O(n)

#include<stdio.h>
#include<stdlib.h>
int val[10005], in[100005];
int main(void)
{int n, m, i, x, y, p, q, ans;scanf("%d%d", &n, &m);for(i=1;i<=n;i++)scanf("%d", &val[i]);for(i=1;i<=m;i++){scanf("%d%d", &x, &y);in[val[x]]++, in[val[y]]--;}p = q = ans = 0;while(p<=100000){if(in[p]<=0)p++;else if(in[q]>=0)q++;else{ans += abs(p-q);in[p]--, in[q]++;}}printf("%d\n", ans);return 0;
}

bzoj 1658: [Usaco2006 Mar]Water Slides 滑水(贪心)相关推荐

  1. bzoj 1659: [Usaco2006 Mar]Lights Out 关灯(IDA*)

    1659: [Usaco2006 Mar]Lights Out 关灯 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 298  Solved: 73 [S ...

  2. bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈)

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 848  Solved: 594 [Sub ...

  3. 【USACO2006 Mar】滑雪缆车 skilift

    [USACO2006 Mar] 滑雪缆车 skilift Time Limit 1000 ms Memory Limit 131072 KBytes Description 科罗拉多州的罗恩打算为奶牛 ...

  4. BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数(数位DP+恶心细节)

    BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 Time Limit: 5 Sec  Memory Limit: 64 MB Description 正如你所知 ...

  5. bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】

    这个题方法还挺多的,不过洛谷上要输出方案所以用堆最方便 先按起始时间从小到大排序. 我用的是greater重定义优先队列(小根堆).用pair存牛棚用完时间(first)和牛棚编号(second),每 ...

  6. BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )

    MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...

  7. BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板

    题目 1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer ...

  8. bzoj 3399: [Usaco2009 Mar]Sand Castle城堡

    3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 145  Solved: 119 ...

  9. bzoj 1682: [Usaco2005 Mar]Out of Hay 干草危机(最小生成树)

    1682: [Usaco2005 Mar]Out of Hay 干草危机 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 626  Solved: 436 ...

最新文章

  1. Docker学习(三)-----Docker镜像常用命令
  2. python 闭包变量不允许write,要使用nonlocal
  3. 在IIS6上部署WebService
  4. Android开发之Retrofit小试牛刀
  5. python取当前时间前后一定间隔的时间点
  6. 阿里云能耗宝新品发布
  7. eclipse +python 修改 各种颜色 +字体
  8. 这个时代最重要的技能之一(数据分析)
  9. PHP操作MongoDB GridFS 存储文件
  10. matlab snr eb n0,snr ber Eb/N0之间的区别与联系
  11. Nginx实现HTTP反向代理配置
  12. 华为P50系列下月见:珍惜,备货量前所未有的少...
  13. layui时间选择30分钟为单位_如何集中注意力,不妨试试番茄工作法 | 五色时间管理法...
  14. vue 定时器:setInterval和setTimeout使用实例及区别
  15. 管家婆支持mysql_开放多接口,支持对接管家婆等第三方应用
  16. Android 长按Button出现一个菜单
  17. SpringMVC jdbcTemplate中queryForObject以及queryForList返回映射实体使用
  18. python 的for与while 的i改变
  19. 网页设计(二)——HTML与BOX
  20. 程序员的自我修养之数学基础11:期望、方差、常见分布(均匀分布、二项分布、泊松分布、正态分布)

热门文章

  1. 开课吧学python靠谱吗-开课吧9.9元学Python课程适合哪些人?开课吧靠谱吗?
  2. python入门教程pdf-Python基础教程-第3版 PDF 下载
  3. python中国官网-中蟒 (中文 Python) 編程語言網站 chinesepython
  4. 自学python免费教材-Python 有哪些入门学习方法和值得推荐的经典教材?
  5. linux shell eval,【shell】bash shell 中 set 和 eval 命令的使用
  6. net472无法建立到信任_是否还会信任,那个曾经背叛过自己的人
  7. win10一直正在检查更新_win10一直存在的烦人问题,终于被彻底解决!你会选择更新么?...
  8. cas → 注销登录后跳转到登录页
  9. cas5.3 → 连接mysql数据库
  10. element-UI:el-table 表格排序