PAT A1046 Shortest Distance
标签(空格分隔): PAT
TIPS: 最后一个数据点可能会超时
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int d[maxn], dist[maxn];
int main() {
int total = 0, n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &d[i]);
total += d[i];
dist[i] = total;
}
int m;
int start, end;
scanf("%d", &m);
for(int i = 0; i < m; i++) {
scanf("%d%d", &start, &end);
if(start > end) swap(start, end);
int temp = dist[end - 1 ] - dist[start - 1];
printf("%d
", min(temp, total - temp));
}
return 0;
}