1046 Shortest Distance (20 分)

#include 
#include 
#include 

using namespace std;
const int maxN = 100005;
int dis[maxN], A[maxN];
int littleToBig(int left, int right);
int bigToLittle(int left, int right, int n);
int main(int argc, char *argv[]) {
    int n, sum = 0;
    scanf("%d", &n);
    for(int i=1; i<=n; i++) {
        scanf("%d", &A[i]);
        sum += A[i];
        dis[i] = sum;
    }
    int m;
    scanf("%d", &m);
    while(m--) {
        int sum = 0;
        int left, right;
        scanf("%d %d", &left, &right);
        if(left > right) {
            int temp = left;
            left = right;
            right = temp;
        }
        sum = min((dis[right-1] - dis[left-1]), bigToLittle(right, left, n));
        printf("%d", sum);
        if(m>0) printf("\n");
    }
}



int bigToLittle(int left, int right, int n) {//输入时大值为left 
    int sum = 0, a, b;
    a = dis[n] - dis[left-1];
    b = dis[right-1];
    sum = a+b;
    return sum;
}


你可能感兴趣的:(1046 Shortest Distance (20 分))