hdu 5163 Taking Bus (BestCoder Round #27)

Taking Bus

                                                              Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768
K (Java/Others)

Total Submission(s): 501    Accepted Submission(s): 203

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.

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.

Source

BestCoder Round #27

四种情况考虑一下就可以了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+100;
long long sum[maxn];
int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        memset(sum,0,sizeof(sum));
        long long x;
        scanf("%d%d",&n,&m);
        sum[1]=0;
        for(int i=2;i<=n;i++)
        {
            scanf("%I64d",&x);
            sum[i]=sum[i-1]+x;
        }
        int u,v;
        long long ans;
        for(int w=1;w<=m;w++)
        {
            int i=((w-1)%n)+1;
            scanf("%d%d",&u,&v);
            if(u>=i)
            {
                if(v>u)
                {
                    ans=sum[v]-sum[i];
                }
                else
                {
                    ans=sum[n]-sum[i]+sum[n]-sum[v];
                }
            }
            else
            {
                if(v>u)
                {
                    ans=sum[n]-sum[i]+sum[n]+sum[v];
                }
                else
                {
                    ans=sum[n]-sum[i]+sum[n]-sum[v];
                }
            }
            printf("%I64d\n",ans);
        }
    }
    return 0;
}
时间: 2024-08-04 13:29:44

hdu 5163 Taking Bus (BestCoder Round #27)的相关文章

hdu 5163 Taking Bus(模拟)

hdu 5163 Taking Bus 问题描述 Bestland有一条非常长的马路,马路上设有n个公交汽车站.公交汽车站从左到右标号为1到n.有m个人想要乘公交.你的任务是找出每个人到终点为止所需要的时间.注意:你需要用来解决这道题目的信息在Input里面,请仔细阅读. 输入描述 输入的第一行包含一个整数T (1≤T≤60),表示测试数据的组数.对于每组测试数据:第一行包含两个整数n和m (2≤n,m≤105),表示公交车站的数目和乘客的数目. 接下来一行包含n?1个整数, d1,d2,-,d

hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7    Accepted Submission(s): 4 Problem Description Hanamichi is taking part in

HDU 5671 Matrix (BestCoder Round #81 (div.2) 1002)

传送门 Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 311    Accepted Submission(s): 142 Problem Description There is a matrix M that has n rows and m columns (1≤n≤1000,1≤m≤1000).Then we

BestCoder Round #27

Jump and Jump... Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description There are n kids and they want to know who can jump the farthest. For each ki

HDU 5124 lines(BestCoder Round #20)

Problem Description: John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A. Input: The first line contains a single integer T(1≤T≤100)(the data for 

HDU 5101 Select(BestCoder Round #17)

Problem Description: One day, Dudu, the most clever boy, heard of ACM/ICPC, which is a very interesting game. He wants to take part in the game. But as we all know, you can't get good result without teammates.So, he needs to select two classmates as

hdu 5082 Love(Bestcoder Round #15)

Love Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 78    Accepted Submission(s): 57 Problem Description There is a Love country with many couples of Darby and Joan in it. In order to commemor

HDU 5167 Fibonacci(BestCoder Round #28)

Problem Description: Following is the recursive definition of Fibonacci sequence: Fi=???01Fi−1+Fi−2i = 0i = 1i > 1 Now we need to check whether a number can be expressed as the product of numbers in the Fibonacci sequence. Input: There is a number T 

HDU 5904 - LCIS (BestCoder Round #87)

HDU 5904 - LCIS [ DP ]    BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式: dp[a[i]] = max(dp[a[i]], dp[a[i]-1] + 1); 发现其实可以简化为 dp[a[i]] = dp[a[i]-1] + 1:因为计算过程中dp[a[i]]不会降低 对两个序列都求一遍,然后取两者最小值的最大值 1 #include <cstdio> 2 #inc