【CUGBACM15级BC第27场 B】hdu 5163 Taking Bus
来源:互联网 发布:手机淘宝自动登录 编辑:程序博客网 时间:2024/05/18 03:16
Taking Bus
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1415 Accepted Submission(s): 490
Total Submission(s): 1415 Accepted Submission(s): 490
Problem Description
Bestland has a very long road and there aren bus station along the road, which are numbered 1 to n from left to right. There are m persons wanting to take the bus to some other station. You task is to find the time needed for each person. Note: All the other information you need is below. Please read the statment carefully.
Input
There are multiple test cases. The first line of input contains an integerT (1≤T≤60) , indicating the number of test cases. For each test case: The first line contains two integersn and m (2≤n,m≤105) , indicating the number of bus stations and number of people. In the next line, there aren−1 integers, d1,d2,…,dn−1 (1≤di≤109 ). The i -th integer means the distance between bus station i and i+1 is di (1≤i<n ). In the next m lines, each contains two integers xi and yi (1≤xi,yi≤n,xi≠yi ), which means i -th person is in bus station xi and wants goto bus station yi .(1≤i≤m)
What else you should know is that for thei -th person, the bus starts at bus station ((i−1) mod n)+1 and drives to right. When the bus arrives at station n , it will turn around and drive from right to left. Similarly, When the bus arrives at station1 , it will turn around and drive from left to right. You can assume that the bus drives one meter per second. And you should only consider the time that the bus drives and ignore the others.
What else you should know is that for the
Output
For each person, you should output one integer which is the minimum time needed before arriving bus stationyi .
Sample Input
17 32 3 4 3 4 51 74 55 4
Sample Output
211028HintFor the first person, the bus starts at bus station 1, and the person takes in bus at time 0. After 21 seconds, the bus arrives at bus station 7. So the time needed is 21 seconds. For the second person, the bus starts at bus station 2. After 7 seconds, the bus arrives at bus station 4 and the person takes in the bus. After 3 seconds, the bus arrives at bus station 5. So the time needed is 10 seconds. For the third person, the bus starts at bus station 3. After 7 seconds, the bus arrives at bus station 5 and the person takes in the bus. After 9 seconds, the bus arrives at bus station 7 and the bus turns around. After 12 seconds, the bus arrives at bus station 4. So the time needed is 28 seconds.
问题描述
Bestland有一条非常长的马路,马路上设有输入描述n 个公交汽车站。公交汽车站从左到右标号为1到n 。有m 个人想要乘公交。你的任务是找出每个人到终点为止所需要的时间。注意:你需要用来解决这道题目的信息在Input里面,请仔细阅读。
输入的第一行包含一个整数输出描述T (1≤T≤60) ,表示测试数据的组数。对于每组测试数据:第一行包含两个整数n 和m (2≤n,m≤105) ,表示公交车站的数目和乘客的数目。 接下来一行包含n−1 个整数,d1,d2,…,dn−1 (1≤di≤109 ).di 表示第i 个公交站和第i+1 个公交站之间的距离。在接下来的m 行, 每行包含两个整数xi 和yi (1≤xi,yi≤n,xi≠yi ), 表示第i 个人时刻0的时候在第xi 个公交站并且想要到第yi 个公交站去。(1≤i≤m) 对于第i 个人, 公交车在第((i−1) mod n)+1 个公交站点在时刻0的时候,并且公交一开始往右开。公交到达站点n 的时候会立刻转向往左开,同样当公交到达站点1的时候也会立刻转向往右开。你可以认为公交每秒只开一个单位距离,你只需要考虑公交开的时间。
对于每个人,输出到达yi 个公交站点需要的最少时间。
输入样例17 32 3 4 3 4 51 74 55 4输出样例
211028提示:对于第一个人, 公交在站点1出发, 然后这个人在时刻0上车。21秒之后,公交到达站点7。对于第二个人,公交在站点2出发。7秒之后,公交到达站点4,这个人上车。之后又过了3秒,公交到达站点5.总共用了10秒。对于第三个人,公交在站点3出发。7秒之后,公交到达站点5,这个人上车。之后过了9秒,公交达到站点7,然后转向开往站点0。之后经过12秒,公交达到站点4。因此总共需要28秒时间。
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;int sta[100005];//存取每个公交站点之间的距离long long suml[100005];//计算每个公交站点到第一个公交站点的距离long long sum[100005];//存取每一组数据所花费的最少时间;int main(){ int T, n, m, start, end; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); long long num = 0; for (int i = 2; i <= n; i++) { scanf("%lld", &sta[i]); num += sta[i]; suml[i] = num; //处理每一个公交站点到第一个公交站点的距离; } for (int count = 1; count <= m; count++) //计数, { scanf("%d%d", &start, &end); int Staion = (count - 1) % n + 1;//第count个人要坐车时,公交车的起始站点位置; //最好画图模拟下,这是人的终点站点标号>起始站点标号,并且公交车的起始站点标号<=人的起始站点标号的情况; if (end > start && Staion <= start) { sum[count] = suml[end] - suml[Staion]; } //人的终点站点标号>起始站点标号,并且公交车的起始站点标号 > 人的起始站点标号的情况; else if (end > start && Staion > start) { sum[count] = 2 * suml[n] - suml[Staion] + suml[end]; } //人的终点站点标号<起始站点标号,并且公交车的起始站点标号 > 人的起始站点标号或者公交车的起始站点标号<=人的起始站点标号的情况;; else if (start > end) { sum[count] = 2 * suml[n] - suml[Staion] - suml[end]; } } for (int i = 1; i <= m; i++) { printf("%lld\n", sum[i]); } } return 0;}
阅读全文
0 0
- 【CUGBACM15级BC第27场 B】hdu 5163 Taking Bus
- 【CUGBACM15级BC第15场 B】hdu 5082 Instruction
- 【CUGBACM15级BC第17场 B】hdu 5101 Select
- 【CUGBACM15级BC第20场 B】hdu 5124 lines
- 【CUGBACM15级BC第26场 B】hdu 5159 Card
- 【CUGBACM15级BC第28场 B】hdu 5167 Fibonacci
- 【CUGBACM15级BC第36场 B】hdu 5199 Gunner
- 【CUGBACM15级BC第12场 B】hdu 5059 Help him
- 【CUGBACM15级BC第11场 B】hdu 5055 Bob and math problem
- 【CUGBACM15级BC第13场 B】hdu 5063 Operation the Sequence
- 【CUGBACM15级BC第18场 B】hdu 5105 Math Problem
- 【CUGBACM15级BC第22场 B】hdu 5143 NPY and arithmetic progression
- 【CUGBACM15级BC第23场 B】hdu 5147 Sequence II
- 【CUGBACM15级BC第25场 B】hdu 5155 Harry And Magic Box
- 【CUGBACM15级BC第10场 B】hdu 5019 Revenge of GCD
- 【CUGBACM15级BC第8场 B】hdu 4990 Reading comprehension
- 【CUGBACM15级BC第7场 B】hdu 4986 Little Pony and Alohomora Part I
- 【CUGBACM15级BC第29场 B】hdu 5171 GTY's birthday gift
- git设置用户名密码
- Maven
- Spark2.x学习笔记:3、 Spark核心概念RDD
- 微信授权登录_代码可以直接使用
- kafka+Structured Streaming+s3+dynamodb
- 【CUGBACM15级BC第27场 B】hdu 5163 Taking Bus
- Android中AndroidManifest ARSC 二进制文件修改器AXMLEditor
- 简述数据库查询优化
- springmvc文件上传
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
- pandas
- 如何在一个可变参数函数中调用另一个可变参数函数
- Mybatis+Mysql批量插入返回自增主键
- 面试-SDWebImage原理