1046. Shortest Distance (20)

来源:互联网 发布:淘宝修改中差评的链接 编辑:程序博客网 时间:2024/05/22 06:41

考察环模拟操作

#include<iostream>#define MAX 100000int g_Dis[MAX];int g_Sum[MAX+1];int Min(int a, int b){if(a < b)return a;else return b;}int main(){int n;while( scanf("%d", &n) != EOF ){int total = 0;g_Sum[0] = 0;for(int i = 0; i < n; ++i){scanf("%d", &g_Dis[i]);total += g_Dis[i];if(i == 0)g_Sum[i+1] = g_Dis[i];elseg_Sum[i+1] = g_Sum[i]+g_Dis[i];}/*for(int i = 0; i <= n; ++i)printf("%d ", g_Sum[i]);printf("\n");*///questint m;scanf("%d", &m);while(m--){int a, b;scanf("%d%d", &a, &b);if(a > b){int temp = a;a = b;b = temp;}int ans = g_Sum[b-1]-g_Sum[a-1];ans = Min(ans, total-ans);printf("%d\n", ans);}}return 0;}