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

HINT

贝茜先按1~2~3~1~2路径滑水.然后走2米,回到1.她再滑行到5,走到4,滑行

到5,走到4,最后滑回1(数字代表中转站号).

这样,她所走的总路程为8米.

 

 
一个神奇的思路
我们只关心每个中转站的入度和出度之差
显然,当一个点的入度$-$出度$=a<0$时,我们需要走到这个点$a$次
而当一个点的入度$-$出度$=a>0$时,我们需要这个点向其他点走$a$次
于是我们就贪心取最近的点。可以用双指针法方便的实现。
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int abs(int a){return a<?-a:a;}
#define N 100005
int n,m,a[N],id[N],ans;
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i) scanf("%d",&id[i]);
for(int i=,q1,q2;i<=m;++i){
scanf("%d%d",&q1,&q2);
++a[id[q1]];--a[id[q2]];
}
for(int l=,r=;l<=;){//指针指向的是实际坐标
if(a[l]<=) ++l;
else if(a[r]>=) ++r;
else ans+=abs(l-r),--a[l],++a[r];
}printf("%d",ans);
return ;
}

bzoj1658: [Usaco2006 Mar]Water Slides 滑水的更多相关文章

  1. bzoj 1658: [Usaco2006 Mar]Water Slides 滑水

    题解: 很神奇的做法,把点分成入度大于出度和入度小于出度两种. 然后入度大于出度的点必须走到某个点,所以排序贪心. #include<stdio.h> #include<iostre ...

  2. BZOJ 1658 Water Slides 滑水

    Water Slides 滑水 [问题描述] It's a hot summer day, and Farmer John is letting Betsy go to the water park ...

  3. [BZOJ1659][Usaco2006 Mar]Lights Out 关灯

    [BZOJ1659][Usaco2006 Mar]Lights Out 关灯 试题描述 奶牛们喜欢在黑暗中睡觉.每天晚上,他们的牲口棚有L(3<=L<=50)盏灯,他们想让亮着的灯尽可能的 ...

  4. Bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声 单调栈

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 631  Solved: 445[Submi ...

  5. BZOJ1657: [Usaco2006 Mar]Mooo 奶牛的歌声

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 489  Solved: 338[Submi ...

  6. 1657: [Usaco2006 Mar]Mooo 奶牛的歌声

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 526  Solved: 365[Submi ...

  7. bzoj1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 树形dp

    题目链接 bzoj1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 题解 dp[i][j][0 / 1] 以i为根的子数中 相邻点对选了j个的最大价值 代码 #i ...

  8. 1722: [Usaco2006 Mar] Milk Team Select 产奶比赛

    1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 https://www.lydsy.com/JudgeOnline/problem.php?id=1722 分析 ...

  9. [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈裸题)

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 961  Solved: 679[Submi ...

随机推荐

  1. 小程序中的block

    <block/> 并不是一个组件,它仅仅是一个包装元素,不会在页面中做任何渲染,只接受控制属性. 因为 wx:if 是一个控制属性,需要将它添加到一个标签上.如果要一次性判断多个组件标签, ...

  2. POJ 1964&HDU 1505&HOJ 1644 City Game(最大0,1子矩阵和总结)

    最大01子矩阵和,就是一个矩阵的元素不是0就是1,然后求最大的子矩阵,子矩阵里的元素都是相同的. 这个题目,三个oj有不同的要求,hoj的要求是5s,poj是3秒,hdu是1秒.不同的要求就对应不同的 ...

  3. Python的全局和局部变量

    global1 = " def o(): #global global1; global1=2; print(global1) def p(): print(global1) if __na ...

  4. CSU 1804 - 有向无环图 - [(类似于)树形DP]

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 ...

  5. JDBC及Filter

    JNDI容器:Java Naming Directory Interface,java命名目录接口EJB:javaEE服务器端组件模型,Enterprise JavaBean,设计目标与核心应用是建立 ...

  6. iOS多线程编程之GCD的基本使用(转载)

    一.主队列介绍 主队列:是和主线程相关联的队列,主队列是GCD自带的一种特殊的串行队列,放在主队列中得任务,都会放到主线程中执行. 提示:如果把任务放到主队列中进行处理,那么不论处理函数是异步的还是同 ...

  7. The Unique MST----poj1679次小生成树

    题目链接:http://poj.org/problem?id=1679 判断最小生成数是否唯一:如果唯一这权值和次小生成树不同,否则相同: #include<stdio.h> #inclu ...

  8. CentOS 7设置Samba共享目录

    1. 安装Samba服务 yum -y install samba # 查看yum源中Samba版本 yum list | grep samba # 查看samba的安装情况 rpm -qa | gr ...

  9. javascript 调用cookies

    cookie   用来保存信息,并且与服务器互动,因此前端后端都可以使用cookie     值得一提的是:虽然cookie是以文件的形式存储在客户端的电脑上, 但浏览器只会让创建cookie的网站访 ...

  10. MySQL多线程备份工具:mydumper

    MySQL多线程备份工具:mydumper http://www.orczhou.com/index.php/2011/12/how-to-split-mysqldump-file/ Mydumper ...