1 7 3 2 3 4 3 4 5 1 7 4 5 5 4
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.
#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;
}
hdu 5163 Taking Bus (BestCoder Round #27)
原文地址:http://blog.csdn.net/caduca/article/details/43156023