题意
给一个数组a,下标 1 1 1~ N N N,a[i]表示i到i+1的距离,数组的最后一个元素表示最后一个到第一个的距离。现在给出 M M M对点,求每对点之间的最小距离
输入
每组包含一个测试用例
第一行第一个数是
N
N
N范围在
[
3
,
1
0
5
]
[3, 10^5]
[3,105], 之后给出
N
N
N个数,表示所给数组
第二行是一个正整数
M
≤
1
0
4
M\leq10^4
M≤104
之后有
M
M
M行,每行给出一对点(编号
1
1
1~
N
N
N)
保证总距离不超过
1
0
7
10^7
107
输出
对每个测试用例,以 M M M行输出结果
样例输入
5 1 2 4 14 9
3
1 3
2 5
4 1
样例输出
3
10
7
解析
所给数组看成是一个循环数组即可,但是直接模拟相加会超时,就将所给的数组变成一个和数组即可
# -*- coding: utf-8 -*-
# @Time : 2019/6/6 11:15
# @Author : ValarMorghulis
# @File : 1046.py
def solve():
a = list(map(int, input().split()))
n = a[0]
a[0] = 0
for i in range(1, n + 1):
a[i] += a[i - 1]
m = int(input())
for i in range(m):
s, e = map(int, input().split())
if s > e:
s, e = e, s
print(min(a[e - 1] - a[s - 1], a[n] - (a[e - 1] - a[s - 1])))
if __name__ == "__main__":
solve()