Taking Bus
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1274 Accepted Submission(s): 419
Problem Description
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.
Input
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.
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.
Output
For each person, you should output one integer which is the minimum time needed before arriving bus station
yi
.
Sample Input
1 7 3 2 3 4 3 4 5 1 7 4 5 5 4
Sample Output
21 10 28HintFor 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.
Source
题意:在一条很长的马路上有n个公交车站和m个人,公交车站之间相距di米(至少为1),现在有m个人想从s站到e站
第i个人的时候公交车在(i-1)%mod+1站开动,初始向右开动。 然后在s站把人接上车,再开到e站问需要多少时间?
注意公交车是来回开的,到最右会返回,到最左也会返回。
思路:用sum数组维护前缀和,表示从最左边到第i站所需要花的时间。然后对于每个询问即可在O(1)得到答案了。
注意如果人在起点左边,终点在起点右边的话,车子会开到最右,再开到最左,再开回终点。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100500
long long a[N],sum[N];
int main()
{
int T;
int s,e,t;
scanf("%d",&T);
while(T--)
{
int n,m;
sum[0]=0;
scanf("%d %d",&n,&m);
for(int i=1;i<n;i++)
{
scanf("%lld",&a[i]);
sum[i]=sum[i-1]+a[i];
}
for(int i=1;i<=m;i++)
{
long long ans=0;
scanf("%d %d",&s,&e);
int t=(i-1)%n+1;
if(t>s)
{
ans=ans+2*sum[n-1]-sum[t-1]-sum[s-1];
if(e>s) ans=ans+sum[s-1]+sum[e-1];
else ans=ans+sum[s-1]-sum[e-1];
}
else
{
ans=ans+sum[s-1]-sum[t-1];
if(e>s) ans=ans+sum[e-1]-sum[s-1];
else ans=ans+2*sum[n-1]-sum[s-1]-sum[e-1];
}
printf("%lld\n",ans);
}
}
return 0;
}