add a1046

This commit is contained in:
Oliver Lew
2021-03-31 21:50:05 +08:00
parent f3cf489a01
commit 140dfdee24
+25
View File
@@ -0,0 +1,25 @@
#include <stdio.h>
int main()
{
int N, M, start, end, d, D[100001] = {0};
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%d", D + i + 1);
/* record the distance between the current and the first exits */
D[i + 1] += D[i];
}
scanf("%d", &M);
for(int i = 0; i < M; i++)
{
scanf("%d %d", &start, &end);
d = --start > --end ? D[start] - D[end] : D[end] - D[start];
/* if the distance is more than half total length, the other direction will be shorter */
printf("%d\n", d * 2 > D[N] ? D[N] - d : d);
}
return 0;
}