From 140dfdee2460ff0121129784925da7fe8c912613 Mon Sep 17 00:00:00 2001 From: Oliver Lew Date: Wed, 31 Mar 2021 21:50:05 +0800 Subject: [PATCH] add a1046 --- PATAdvanced/1046.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 PATAdvanced/1046.c diff --git a/PATAdvanced/1046.c b/PATAdvanced/1046.c new file mode 100644 index 0000000..3bc8db4 --- /dev/null +++ b/PATAdvanced/1046.c @@ -0,0 +1,25 @@ +#include + +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; +}