题目描述
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.
输入
Each input file contains one test case. For each case, the first line contains an integer N (in [3, 105]), followed by N integer distances D1 D2 ... DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=104), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 107.
输出
For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.
样例输入
5 1 2 4 14 9
3
1 3
2 5
4 1
样例输出
3
10
7
#include <stdio.h>
int main()
{
int num;///出口的数量
int i, s;
int dis[100] = {0};///每一个出口按顺序到第一个出口的距离,开始初始化为0
int len[100] = {0};///存储两个出口的距离
int a;///要求的两出口距离的数量
int out1 ,out2;///是哪两个出口
scanf("%d", &num);
for(i = 1; i <= num; i++)
{
scanf("%d", &s);
dis[i] = s + dis[i-1];
}
int flag2 = 1;
scanf("%d", &a);
while(a >= flag2)
{
scanf("%d%d", &out1,&out2);
int min=0;///临时存储
if(out1 > out2)
{
min = dis[out1-1] - dis[out2-1];
if(min > (dis[num]-min))
{
min = dis[num]-min;
}
}
else
{
min = dis[out2-1] - dis[out1-1];
if(min > (dis[num]-min))
{
min = dis[num]-min;
}
}
len[flag2] = min;
flag2++;
}
for(i = 1; i <= a; i++)
{
printf("%d\n", len[i]);
}
}