Sample Input
5 1 2 4 14 9
3
1 3
2 5
4 1
Sample Output
3
10
7
思路: 计算两个出口的最短距离,注意这是一个环,可以从两边计算。
代码:
#include <bits/stdc++.h>
using namespace std;
int a[10001];
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
int x;
scanf("%d", &x);
a[i] += (a[i - 1] + x);
}
int k;
scanf("%d", &k);
while (k--)
{
int x, y;
scanf("%d %d", &x, &y);
if (x > y) swap(x, y);
int ans = 0;
if (a[y - 1] - a[x - 1] > (a[5] - a[y - 1]) + a[x - 1])
ans = (a[5] - a[y - 1]) + a[x - 1];
else
ans = a[y - 1] - a[x - 1];
printf("%d\n", ans);
}
return 0;
}