bzoj1658: [Usaco2006 Mar]Water Slides 滑水
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
3
1
7
10
1 2
1 2
2 3
3 1
4 5
1 5
4 1
Sample Output
HINT
贝茜先按1~2~3~1~2路径滑水.然后走2米,回到1.她再滑行到5,走到4,滑行
到5,走到4,最后滑回1(数字代表中转站号).
这样,她所走的总路程为8米.
#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 滑水的更多相关文章
- bzoj 1658: [Usaco2006 Mar]Water Slides 滑水
题解: 很神奇的做法,把点分成入度大于出度和入度小于出度两种. 然后入度大于出度的点必须走到某个点,所以排序贪心. #include<stdio.h> #include<iostre ...
- BZOJ 1658 Water Slides 滑水
Water Slides 滑水 [问题描述] It's a hot summer day, and Farmer John is letting Betsy go to the water park ...
- [BZOJ1659][Usaco2006 Mar]Lights Out 关灯
[BZOJ1659][Usaco2006 Mar]Lights Out 关灯 试题描述 奶牛们喜欢在黑暗中睡觉.每天晚上,他们的牲口棚有L(3<=L<=50)盏灯,他们想让亮着的灯尽可能的 ...
- Bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声 单调栈
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 631 Solved: 445[Submi ...
- BZOJ1657: [Usaco2006 Mar]Mooo 奶牛的歌声
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 489 Solved: 338[Submi ...
- 1657: [Usaco2006 Mar]Mooo 奶牛的歌声
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 526 Solved: 365[Submi ...
- bzoj1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 树形dp
题目链接 bzoj1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 题解 dp[i][j][0 / 1] 以i为根的子数中 相邻点对选了j个的最大价值 代码 #i ...
- 1722: [Usaco2006 Mar] Milk Team Select 产奶比赛
1722: [Usaco2006 Mar] Milk Team Select 产奶比赛 https://www.lydsy.com/JudgeOnline/problem.php?id=1722 分析 ...
- [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈裸题)
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 961 Solved: 679[Submi ...
随机推荐
- 最长上升子序列 OpenJ_Bailian - 2757 dp模板
第一种是用以前状态更新当前状态(人人为我) 第二种是用当前状态更新以后状态(我为人人) 都是n^2代码: 对于人人为我的更新方法,可以用数据结构储存“人人”对其进行线性\二分优化. ; int a[m ...
- CCCC L2-020. 功夫传人 搜索 bfs && 精度+ 特判
https://www.patest.cn/contests/gplt/L2-020 题解:给你一颗树,让你遍历一遍,顺便更新一下数据,每次到根节点时将其对应的数据加到ans上面.这里用的bfs. 坑 ...
- cordova-ios 升级到4.4.0 无法真机跑iOS8 报错: dyld`dyld_fatal_error: -> 0x120085088 <+0>: brk #0x3
项目进入测试阶段,马上要上线了,同事拿了自己的iOS8系统5s跑真机,无缘无故报错,之前跑她的手机完全没有问题的.Xcode 8.x中最低部署版本是iOS8.0,按理说完全能够跑真机的. 但是报了一个 ...
- cordova插件file使用时遇到的一个平台相关的问题
使用cordova-plugin-file可以帮助我们方便的操作app中的图片等文件,分享一下我在用该插件从图库读取图片时遇到的一个平台相关的小问题. 使用场景,我会在APP中新增一张图片,会有一个可 ...
- compile time - run-time
php.net Class member variables are called "properties". You may also see them referred to ...
- Arrow-一个最好用的日期时间Python处理库
https://www.jianshu.com/p/c878bb1c48c1 写过Python程序的人大都知道,Python日期和时间的处理非常繁琐和麻烦,主要有以下几个问题: 有众多的package ...
- flask之表单验证 2018.12.23
#flask的消息闪现依赖于flash库,用户发送的请求方式存储在request模块中 #跳转依赖于redirect模块,还可以通过url_for方法来进行方法上的寻址 from flask impo ...
- Python开发【笔记】:python程序添加到systemctl系统服务
systemctl系统服务 环境:centos7 systemctl服务使用详解 实现 正常情况下我们在/usr/lib/systemd/system/目录下,创建一个以.service 后缀的文件, ...
- JavaScript如何实现拖放功能
1.在学习ExtJs时,对其拖放功能感到很陌生,然后找了个拖放功能实现. 转载地址 2.拖拽的基本原理就是根据鼠标的移动来移动被拖拽的元素.鼠标的移动也就是x.y坐标的变化:元素的移动就是style. ...
- [django]form不清空问题解决
https://www.cnblogs.com/OldJack/p/7118396.html 有时候提交表单后,发现某个字段写错了,但是form的其他字段竟然被清空,这个万万不能接受.所有django ...