欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PAT Advanced 1046 Shortest Distance

程序员文章站 2022-06-07 09:46:40
...

PAT Advanced 1046 Shortest Distance

#include<stdio.h>
#include<vector>
using namespace std;

int main(){
    int n;
    scanf("%d",&n);
    vector<int> dis(n);
    int sum=0;
    vector<int> porder(n);
    vector<int> rorder(n);
    for(int i=0;i<n;i++){
        porder[i] = sum;
        int v;
        scanf("%d",&v);
        sum+=v;
        dis[i]=v;
    }
    int rsum=0;
    for(int i=n-1;i>=0;i--){
        rsum+=dis[i];
        rorder[i]=rsum;
    }
    int m;
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        int st,ed;
        scanf("%d %d",&st,&ed);
        int little = st<ed?st:ed;
        int large = st<ed?ed:st;
        int partSum= porder[little-1] + rorder[large-1];
        int res = partSum<sum-partSum?partSum:sum-partSum;
        // printf("sum:%d psum:%d sum-psum:%d res:%d\n",sum,psum,sum-psum,res);
        printf("%d\n",res);
    }
}
相关标签: 刷题笔记#PTA