hdu 5163(前缀和+分类讨论)
2016-07-16 18:02
375 查看
Taking Bus
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1275 Accepted Submission(s): 420
[align=left]Problem Description[/align]
Bestland has a very long road and there are n 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.
[align=left]Input[/align]
There are multiple test cases. The first line of input contains an integer T (1≤T≤60), indicating the number of test cases. For each test case: The first line contains two integers n and m (2≤n,m≤105), indicating the number of bus stations and number of people. In the next line, there are n−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 the i-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 station 1,
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.
[align=left]Output[/align]
For each person, you should output one integer which is the minimum time needed before arriving bus station yi.
[align=left]Sample Input[/align]
1
7 3
2 3 4 3 4 5
1 7
4 5
5 4
[align=left]Sample Output[/align]
21
10
28
Hint
For 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.
[align=left]Source[/align]
BestCoder Round #27
题意:一辆公交车最开始从左到右行驶,路上有m个乘客,第m个乘客的起始位置是 s,目标位置是 t ,公交车对于第i个乘客的起始位置是 (i-1)%n+1,并且是向右行驶,只有到达第n个点才会返回,从左向右行驶,现在给出第 i个站到第 i+1个站的时间,问每个乘客从起始点到目标点所需时间.
题解:画个图,分6种情况考虑(s0代表公交车的起始位置,s,t代表乘客的起始位置和目标位置)
一:s0<=s&&s0<=t
1.s<t
2.s>t
二:s0>=s&&s0>=t
1.s<t
2.s>t
三:s0位于 s,t之间
1.s<t
2.s>t
处理一下前缀和,就能够在O(1)时间处理所有询问了.
#include<stdio.h> #include<iostream> #include<string.h> #include <stdlib.h> #include<math.h> #include<algorithm> #include <queue> using namespace std; typedef long long LL; const int N = 100005; int d[2*N]; LL sum[2*N]; int main() { int tcase; scanf("%d",&tcase); int t =1; while(tcase--){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<n;i++){ scanf("%d",&d[i]); } memset(sum,0,sizeof(sum)); for(int i=1;i<n;i++){ sum[i] = sum[i-1]+d[i]; } for(int i=1;i<=m;i++){ LL time = 0; int s0 = (i-1)%n+1; int s,t; scanf("%d%d",&s,&t); if(s0<=s&&s0<=t){ if(s<t){ time = sum[t-1]-sum[s0-1]; }else{ time = sum[n-1]-sum[s0-1]+sum[n-1]-sum[t-1]; } }else if(s0>=s&&s0>=t){ if(s<t){ time = sum[n-1]-sum[s0-1]+sum[n-1]+sum[t-1]; }else{ time = sum[n-1]-sum[s0-1]+sum[n-1]-sum[t-1]; } }else{ if(s<t){ time = sum[n-1]-sum[s0-1]+sum[n-1]+sum[t-1]; }else{ time = sum[n-1]-sum[s0-1]+sum[n-1]-sum[t-1]; } } printf("%lld\n",time); } } return 0; }
相关文章推荐
- 为什么要重写hashcode() 方法
- eth0,eth1,eth2,lo是什么
- 十六进制转换成十进制 二进制
- 【spring 3】AOP:静态代理
- 221. Maximal Square
- 【spring 3】AOP:静态代理
- webpack快速入门和实战
- kruskal算法求最小生成树(jungle roads的kruskal解法)
- 晶/陶振不起振及使用注意事项
- 慕课网实战—《用组件方式开发 Web App全站 》笔记七-饼图和环图组件开发
- 以后的博客可能会偏向游戏开发方向
- selenium2使用记录
- telnet时显示:允许更多到 telnet 服务器的连接。请稍候再试
- 使用贝赛尔曲线实现仿360拖动安仔清理动画
- overwrite在hive内部表及外部表特性
- POJ 1200 Crazy Search
- pager 命令
- LTS版本的解析
- [LeetCode] 13. Roman to Integer
- 线性表