初始:
#include<stdio.h>
int d[100001];
int getdis(int a,int b,int l)
{
int i,dis=0;
a--;b--;
for(i=a;i!=b;i=(i+1)%l)
dis=dis+d[i];
return dis;
}
int main()
{
int len,n,i,sum=0;
scanf("%d",&len);
for(i=0;i<len;i++)
{
scanf("%d",&d[i]);
sum+=d[i];
}
scanf("%d",&n);
int dis1,dis2,n1,n2;
while(n--)
{
scanf("%d%d",&n1,&n2);
dis1=getdis(n1,n2,len);
dis2=sum-dis1;
printf("%d\n",(dis1<dis2)?dis1:dis2);
}
return 0;
}
但是超时了
于是改进
#include<stdio.h>
int d[100001],dis[100001];
int main()
{
int len,n,i,sum=0;
scanf("%d",&len);
for(i=1;i<=len;i++)
{
scanf("%d",&d[i]);
sum+=d[i];
dis[i]=sum;
}
scanf("%d",&n);
int dis1,dis2,n1,n2;
dis[0]=0;
while(n--)
{
scanf("%d%d",&n1,&n2);
if(n1<n2)
{int t;t=n1;n1=n2;n2=t;}
dis1=dis[n1-1]-dis[n2-1];
dis2=sum-dis1;
printf("%d\n",(dis1<dis2)?dis1:dis2);
}
return 0;
}