杭电 hdu 5163 Taking Bus【模拟】

75 篇文章 0 订阅

Taking Bus

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1217    Accepted Submission(s): 386


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 (1T60) , indicating the number of test cases. For each test case: The first line contains two integers n and m (2n,m105) , indicating the number of bus stations and number of people. In the next line, there are n1 integers, d1,d2,,dn1 ( 1di109 ). The i -th integer means the distance between bus station i and i+1 is di ( 1i<n ). In the next m lines, each contains two integers xi and yi ( 1xi,yin,xiyi ), which means i -th person is in bus station xi and wants goto bus station yi . (1im)

What else you should know is that for the i -th person, the bus starts at bus station ((i1) 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 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.

我们只要考虑清楚各种情况,然后模拟写出就行了。

这里我们由推测就能知道,一共有三种乘车的方式。

1.车出发的地方,在起点左侧。构成车出发点,起点,终点一条线的结构。如题干样例1,2

2.车出发的地方,在起点右侧。这个时候车要先开到最右边,再回来接乘客,再到终点。这个忘了,WA了一发。。。

3.车出发的地方在起点左侧,但是终点在终点在起点左侧,如题干样例3.


然后就是代码:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
long long int  stop[121212];
long long int  sum[121212];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(sum,0,sizeof(sum));
        sum[0]=0;
        long long int  n,m;
        cin>>n>>m;
        for(int i=1;i<=n-1;i++)
        {
            cin>>stop[i];
            sum[i]=sum[i-1]+stop[i];
        }
        for(int i=1;i<=m;i++)
        {
            long long int begin=(i-1)%n+1;
            long long int  a,b;
            cin>>a>>b;
            long long int output=0;
            if(a>b)//情况1
            {
                output+=sum[n-1]-sum[begin-1];
                output+=sum[n-1]-sum[b-1];
                cout<<output<<endl;
            }
            else
            {
                if(begin>a)//情况2
                {
                    output+=sum[n-1]-sum[begin-1];
                    output+=sum[n-1];
                    output+=sum[b-1];
                    cout<<output<<endl;
                }
                else//情况3
                {
                     output+=sum[b-1]-sum[begin-1];
                     cout<<output<<endl;
                }
            }
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值