PAT (Advanced Level) 1046. Shortest Distance (20)

处理一下前缀和。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;

const int maxn=100000+10;
int a[maxn],n,m;
int pre[maxn];
int sum=0;

int main()
{
    scanf("%d",&n); pre[0]=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        sum=sum+a[i];
        pre[i]=pre[i-1]+a[i];
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int ans;
        int li,ri; scanf("%d%d",&li,&ri);
        if(li>ri) swap(li,ri);
        ri--,ans=min(pre[ri]-pre[li-1],sum-(pre[ri]-pre[li-1]));
        printf("%d\n",ans);
    }

    return 0;
}

 

posted @ 2016-05-25 14:38  Fighting_Heart  阅读(101)  评论(0编辑  收藏  举报